Skip to content

Instantly share code, notes, and snippets.

@romilly
Last active August 7, 2022 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romilly/66db6afa88e5a113b3804308e8c4c37c to your computer and use it in GitHub Desktop.
Save romilly/66db6afa88e5a113b3804308e8c4c37c to your computer and use it in GitHub Desktop.
import serial
class Talker:
TERMINATOR = '\r'.encode('UTF8')
def __init__(self, timeout=1):
self.serial = serial.Serial('/dev/ttyACM0', 115200, timeout=timeout)
def send(self, text: str):
line = '%s\r\f' % text
self.serial.write(line.encode('utf-8'))
reply = self.receive()
reply = reply.replace('>>> ','') # lines after first will be prefixed by a propmt
if reply != text: # the line should be echoed, so the result should match
raise ValueError('expected %s got %s' % (text, reply))
def receive(self) -> str:
line = self.serial.read_until(self.TERMINATOR)
return line.decode('UTF8').strip()
def close(self):
self.serial.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment