1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import serial, fcntl, struct
ser = serial.Serial(
port='/dev/ttyS2',
baudrate=8000000,
timeout=0.25,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# RS485 mode (use kernel driver)
fd=ser.fileno()
# RS485 feature flags (first short) =
# SER_RS485_ENABLED (1 << 0)
# SER_RS485_RTS_ON_SEND (1 << 1)
# SER_RS485_RTS_AFTER_SEND (1 << 2)
# No documentation about it (see serial.h)
serial_rs485 = struct.pack('hhhhhhhh', 1, 0, 0, 0, 0, 0, 0, 0)
#fcntl.ioctl(fd,0x542F,serial_rs485) #User space
fcntl.ioctl(fd,0x542E,serial_rs485) #Kernel space
# Flush the port (sometimes '\x00' chars are created)
ser.flushInput()
ser.flushOutput()
# Write Mode (it may be reversed for other circuits)
ser.setRTS(0)
# Send the request
if request is not None:
ser.write(request)
s = ser.read(len(request)) # read the sent request (loop)
# Read mode
ser.setRTS(1)
# Read the answer line
r.readline()
... |
Partager