Skip to content

Instantly share code, notes, and snippets.

@Protoneer
Created September 17, 2014 03:00
Show Gist options
  • Save Protoneer/8498142c5e2aa56b224c to your computer and use it in GitHub Desktop.
Save Protoneer/8498142c5e2aa56b224c to your computer and use it in GitHub Desktop.
Serial connect with custom end of line character
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