0

As a newbie to python, I'm trying to use it to read a file and write each line of the file to the RS-232 port. My code bellow seems to work for the most part, except for my listen and react segments. From poking around, it seems that my if statements can't read if I've received a "Start\r", or "End\r" string from my device (RS-232). Can anyone provide feedback on what is missing?

import serial
import time

port = "/dev/ttyS0"
speed = 9600

print("\n\n\n\nScript Starting\n\n\n")

ser = serial.Serial(port, speed, timeout=0) 

ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output and discard all that is in buffer

text_file = open("my.file", "r")
lines = text_file.read().split('\n')
i = 0
counter = 0


while i<len(lines):

    response = ser.readline()

    if (counter == 0):
        print("\n\nProbing With Off Data\n")
        ser.write('FFF')
        ser.write('\r')
        counter+=1

    if (response == 'Start'):

        ser.write('FFF')
        ser.write('\r')

    if (response == 'End'):
        print("\nString Transmitted:")
        print lines
        make_list_a_string = ''.join(map(str, lines))
        ser.write(make_list_a_string)
        ser.write('\r')
        print("\n")
        i+=1




text_file.close()
exit(0)
4

1 回答 1

0

尝试使用 strip() 删除任何尾随或前面的 '\r':

if (response.strip() == 'Start'):
于 2013-10-03T00:49:21.623 回答