我正在使用在Raspberry上运行的Python3.我有一个串口设备(max232/PiC16F84)通过USB转串口适配器连接到Raspberry.我尝试向设备发送两个字节(例如0000 0011),然后PIC将其解释为命令.USB - 串行适配器配置正确,参数如bauderate应该没问题.我猜我的代码没有将正确的字节发送到串口.
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')
Run Code Online (Sandbox Code Playgroud)