From baeecfb47b6f75c71f9f547a577adf6dfac3befb Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lafreniere Date: Wed, 27 Feb 2019 09:52:05 -0500 Subject: [PATCH 1/2] Send all GCode command Read a gcode file and send it to the arduino --- arduinosender/CommunicationMain.py | 43 ++++++++++++++++++++++++++++++ arduinosender/__init__.py | 0 2 files changed, 43 insertions(+) create mode 100644 arduinosender/CommunicationMain.py create mode 100644 arduinosender/__init__.py diff --git a/arduinosender/CommunicationMain.py b/arduinosender/CommunicationMain.py new file mode 100644 index 0000000..1d845e7 --- /dev/null +++ b/arduinosender/CommunicationMain.py @@ -0,0 +1,43 @@ +import serial +import argparse + +from serial import SerialException + +if __name__ == "__main__": + parser = argparse.ArgumentParser(prog = 'main.py') + parser.add_argument('-g', required = True, help = 'GCode file path') + parser.add_argument('-c', required = True, help = 'Arduino com port') + parser.add_argument('-t', required = False, type = int, help = 'Communication timout') + parser.add_argument('-b', required = False, type = int, help = 'Communication baudrate') + args = parser.parse_args() + + timeoutCom = 2 + baudRate = 9600 + + if args.t: + timeoutCom = int(args.t) + if args.b: + baudRate = int(args.b) + + ser = None + try: + ser = serial.Serial(args.c, baudRate, timeout = timeoutCom) + + file = open(args.g, 'r') + lines = file.readlines() + file.close() + + for line in lines: + if not line == '\n': + ser.write(line.encode('UTF-8')) + if not ser.readline().decode('UTF-8').startswith('1'): + raise RuntimeError('Communication lost') + + ser.close() + except SerialException as errSE: + print('Serial Exception' + str(errSE)) + except RuntimeError as errRE: + print(str(errRE)) + finally: + if ser is not None: + ser.close() \ No newline at end of file diff --git a/arduinosender/__init__.py b/arduinosender/__init__.py new file mode 100644 index 0000000..e69de29 From 2ec7984da260ffbf9baf1ea75945125c218e17fd Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lafreniere Date: Wed, 20 Mar 2019 13:25:30 -0400 Subject: [PATCH 2/2] #56-Delay in Arduino communication Wait for input from the arduino before sending next GCode command --- arduinosender/CommunicationMain.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/arduinosender/CommunicationMain.py b/arduinosender/CommunicationMain.py index 1d845e7..d7b9db0 100644 --- a/arduinosender/CommunicationMain.py +++ b/arduinosender/CommunicationMain.py @@ -1,5 +1,6 @@ import serial import argparse +from time import sleep from serial import SerialException @@ -9,20 +10,26 @@ if __name__ == "__main__": parser.add_argument('-c', required = True, help = 'Arduino com port') parser.add_argument('-t', required = False, type = int, help = 'Communication timout') parser.add_argument('-b', required = False, type = int, help = 'Communication baudrate') + parser.add_argument('-waittime', required = False, type = int, help = 'Sleep time before sending (s)') args = parser.parse_args() timeoutCom = 2 baudRate = 9600 + commandTimeout = 0 + waitTime = 1 if args.t: timeoutCom = int(args.t) if args.b: baudRate = int(args.b) + if args.waittime: + waitTime = int(args.waittime) ser = None try: ser = serial.Serial(args.c, baudRate, timeout = timeoutCom) - + sleep(waitTime) + file = open(args.g, 'r') lines = file.readlines() file.close() @@ -30,10 +37,25 @@ if __name__ == "__main__": for line in lines: if not line == '\n': ser.write(line.encode('UTF-8')) - if not ser.readline().decode('UTF-8').startswith('1'): + + if not ser.readline().decode('UTF-8').startswith('2'): raise RuntimeError('Communication lost') + + while True: + received = ser.readline().decode('UTF-8') + if not received.startswith('1') or received.startswith('-1'): + commandTimeout += 1 + if commandTimeout > timeoutCom * 10: + raise RuntimeError('Command not executed') + if received.startswith('-1'): + raise RuntimeError('Command error') + else: + break + + print('Done') ser.close() + except SerialException as errSE: print('Serial Exception' + str(errSE)) except RuntimeError as errRE: