我想在python中使用pyserial将2字节数据写入串口。因为pyserial让我一次发送1个字节我写了下面的代码来完成我的问题,但这似乎没有用,你有意见帮我这个吗?代码;
import serial # import the module
ComPort = serial.Serial('/dev/ttyUSB0') # open ttyUSB0
ComPort.baudrate = 9600 # set Baud rate
ComPort.bytesize = 8 # Number of data bits = 8
ComPort.parity = 'N' # No parity
ComPort.stopbits = 1 # Number of Stop bits = 1
position = 324 # integer to be send to port
get_bin = lambda x, n: format(x, 'b').zfill(n) # binary format
data = (get_bin(position,16)) # 16 digit binary format
data_first_part = data[0:8] #first 8 digits
data_second_part = data[8:16] #second 8 digit
try:
data1 = bytearray(data_first_part)
data2 = bytearray(data_second_part)
No = ComPort.write(data1+data2)
print data1+data2
print data1
print data2
ComPort.close() #Close the Serial port
except IndexError:
print "no success"