Created
May 26, 2020 12:26
-
-
Save juanpagfe/de7520fa63ba4dbaa17097b0f59223e1 to your computer and use it in GitHub Desktop.
Simple chat over serial
This file contains hidden or 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 argparse | |
import os | |
import serial | |
import thread | |
def file_path(string): | |
if os.path.exists(string): | |
return string | |
else: | |
raise Exception('%s does not exists' % string) | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-p', '--port', type=file_path, required=True) | |
args = parser.parse_args() | |
return args | |
def write(ser): | |
while True: | |
ser.write(raw_input('')) | |
def read(ser): | |
while True: | |
x=ser.readline() | |
if x != "": | |
print "> %s" % (x) | |
def run(): | |
args = parse_args() | |
ser = serial.Serial( | |
port=args.port, | |
baudrate = 9600, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
bytesize=serial.EIGHTBITS, | |
timeout=0.1 | |
) | |
thread.start_new_thread( read, (ser,) ) | |
write(ser) | |
if __name__ == "__main__": | |
run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment