我正在使用运行在树莓派上的Python3。我有一个串口设备(max232/PiC16F84)通过USB转串口适配器连接到了树莓派上。 我试图向设备发送两个字节(例如0000 0011),这将被PIC解释为命令。 USB-串口适配器已正确配置,参数如波特率应该没问题。我猜测我的代码没有向串口发送正确的字节。
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=1200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
xonxoff=serial.XOFF,
rtscts=False,
dsrdtr=False
)
ser.open()
ser.isOpen()
print("Initializing the device ..")
ser.write(bytes(0x00))
print("Write command")
ser.write (bytes(0x04))
print('Done')