Line buffered serial input

前端 未结 4 1166
轮回少年
轮回少年 2020-12-11 17:46

I have a serial device that I\'m trying to read input from. I sent it a string \"ID\\r\", and it returns \"ID XX\\r\" (where \\r is an ASCII carriage return, hex 0x0d).

4条回答
  •  醉梦人生
    2020-12-11 17:53

    Thanks for the code Keith, but I wanted to keep this code somewhat portable, so I'd like to stick with the default "serial" package.

    Plus, since I'm still learning Python, I wanted to try to learn how to use the TextIOWrapper in the way it was intended.

    I gave up trying to make serial.readline() work, so for now I'll just use a simple "readLine" function to read a character at a time and look for a carriage return terminator. Though if I run into more serial quirkyness, I may revisit using your code.

    Thanks!

    def readLine(ser):
        str = ""
        while 1:
            ch = ser.read()
            if(ch == '\r' or ch == ''):  
                break
            str += ch
    
        #"print "str = " + str
    
        return str
    

提交回复
热议问题