Created
September 17, 2014 03:00
-
-
Save Protoneer/8498142c5e2aa56b224c to your computer and use it in GitHub Desktop.
Serial connect with custom end of line character
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial,time | |
ser = serial.Serial('COM1') | |
ser.write("$$\r\n") | |
def readline(): | |
eol = b'\r' | |
leneol = len(eol) | |
line = bytearray() | |
while True: | |
c = ser.read(1) | |
if c: | |
if ord(c) != 17 and c != 13: | |
line += c | |
if line[-leneol:] == eol: | |
break | |
else: | |
break | |
return bytes(line) | |
time.sleep(0.1) | |
print(readline()) | |
while ser.inWaiting(): | |
print(readline()) | |
time.sleep(0.01) | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment