#!/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
然后发送'我'然后是N和C,然后是文本文件中的9个十六进制数字。下面的代码读了一行并进入了写作部分并阅读了整个文本文件;
X7_SEM_V3_6
000062240
000062241
ICAP
000062240
000062241
so on
对所有七条读取行执行此操作后,发送I。我希望它读取所有七行,而不是发送I并开始工作。这是在while循环中。如果我使用其他东西,它只是读取第一行并卡住。请一些帮助。
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 "