works, but license issues due to bcm2835 lib
This commit is contained in:
13
pyserialext/Makefile
Normal file
13
pyserialext/Makefile
Normal file
@ -0,0 +1,13 @@
|
||||
CFLAGS=-I/opt/bcm2835/include
|
||||
|
||||
writec.so: writec.o /opt/bcm2835/lib/libbcm2835.a
|
||||
$(LD) -shared -o $@ $^
|
||||
|
||||
writec.o: writec.c
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm -f *.so *.o
|
19
pyserialext/RS485Ext.py
Normal file
19
pyserialext/RS485Ext.py
Normal file
@ -0,0 +1,19 @@
|
||||
import serial.rs485
|
||||
import serial.serialutil
|
||||
import ctypes
|
||||
|
||||
|
||||
class RS485Ext(serial.rs485.RS485):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(RS485Ext, self).__init__(*args, **kwargs)
|
||||
self.writec = ctypes.cdll.LoadLibrary('writec.so')
|
||||
fd = self.fileno()
|
||||
r = self.writec.set_rs485_mode(fd)
|
||||
|
||||
def write(self, b):
|
||||
d = serial.serialutil.to_bytes(b)
|
||||
l = len(d)
|
||||
fd = self.fileno()
|
||||
r = self.writec.writec(fd, d, l)
|
||||
return r
|
||||
|
BIN
pyserialext/__pycache__/RS485Ext.cpython-35.pyc
Normal file
BIN
pyserialext/__pycache__/RS485Ext.cpython-35.pyc
Normal file
Binary file not shown.
31
pyserialext/writec.c
Normal file
31
pyserialext/writec.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdio.h>
|
||||
#include <linux/serial.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <bcm2835.h>
|
||||
|
||||
|
||||
const uint8_t DE_PIN = RPI_V2_GPIO_P1_11;
|
||||
|
||||
int set_rs485_mode(int fd) {
|
||||
bcm2835_init();
|
||||
bcm2835_gpio_fsel(DE_PIN, BCM2835_GPIO_FSEL_OUTP);
|
||||
bcm2835_gpio_write(DE_PIN, LOW);
|
||||
}
|
||||
|
||||
ssize_t writec(int fd, char *buf, size_t count) {
|
||||
bcm2835_gpio_write(DE_PIN, LOW);
|
||||
ssize_t r = write(fd, buf, count);
|
||||
uint8_t lsr;
|
||||
do {
|
||||
int r = ioctl(fd, TIOCSERGETLSR, &lsr);
|
||||
} while (!(lsr & TIOCSER_TEMT));
|
||||
bcm2835_gpio_write(DE_PIN, HIGH);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
BIN
pyserialext/writec.o
Normal file
BIN
pyserialext/writec.o
Normal file
Binary file not shown.
BIN
pyserialext/writec.so
Executable file
BIN
pyserialext/writec.so
Executable file
Binary file not shown.
Reference in New Issue
Block a user