Python串行端口读取,直到完成
然后发送“I”,后跟N和C,然后从文本文件中发送9个十六进制数字。下面的代码读了一行,进入了编写部分,阅读了整个文本文件Python串行端口读取,直到完成,python,python-3.x,serial-port,pyserial,Python,Python 3.x,Serial Port,Pyserial,然后发送“I”,后跟N和C,然后从文本文件中发送9个十六进制数字。下面的代码读了一行,进入了编写部分,阅读了整个文本文件 #!/usr/bin/python I am struggling with quite the simple problem. I want to read some data from the serial port first than start writing the data. The data reading and writing works well. T
#!/usr/bin/python
I am struggling with quite the simple problem. I want to read some data from the serial port first than start writing the data. The data reading and writing works well. The problem is I need to read first around 7 lines like
X7_SEM_V3_6
ICAP OK
SA OK
IC OK
RBDK OK
status OK
S OK
对所有七行执行此操作后,读取并发送I。我希望它在发送I之前读取所有七行并开始工作。这是while循环。如果我用别的东西,它只会读第一行,然后卡住。请找个人帮忙
X7_SEM_V3_6
000062240
000062241
ICAP
000062240
000062241
so on
import serial, time
import binascii
ser = serial.Serial()
ser.port = "/dev/ttyUSB1"
ser.baudrate = 38400
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.xonxoff = False
ser.rtscts = False
ser.dsrdtr = False
number_address = 1341602
number_char = 9
timeout=1
f=open('lut.txt','r')
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
# reading
max_packet = 20
lines = 0
while True:
byteData = ser.read_until('\r',max_packet)
newdata=str(byteData)
print newdata.strip()
#ser.write('I')
ser.write('I')
time.sleep(0.01)
for line in f.readlines():
print line
ser.write('N')
time.sleep(0.01)
ser.write(' ')
time.sleep(0.01)
ser.write('C')
time.sleep(0.01)
for i in line:
newdata=i
ser.write(newdata)
time.sleep(0.01)
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "