#56-Delay in Arduino communication

Wait for input from the arduino before sending next GCode command
This commit is contained in:
Marc-Antoine Lafreniere
2019-03-20 13:25:30 -04:00
parent 736230eb48
commit 2ec7984da2

View File

@@ -1,5 +1,6 @@
import serial import serial
import argparse import argparse
from time import sleep
from serial import SerialException from serial import SerialException
@@ -9,20 +10,26 @@ if __name__ == "__main__":
parser.add_argument('-c', required = True, help = 'Arduino com port') parser.add_argument('-c', required = True, help = 'Arduino com port')
parser.add_argument('-t', required = False, type = int, help = 'Communication timout') 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('-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() args = parser.parse_args()
timeoutCom = 2 timeoutCom = 2
baudRate = 9600 baudRate = 9600
commandTimeout = 0
waitTime = 1
if args.t: if args.t:
timeoutCom = int(args.t) timeoutCom = int(args.t)
if args.b: if args.b:
baudRate = int(args.b) baudRate = int(args.b)
if args.waittime:
waitTime = int(args.waittime)
ser = None ser = None
try: try:
ser = serial.Serial(args.c, baudRate, timeout = timeoutCom) ser = serial.Serial(args.c, baudRate, timeout = timeoutCom)
sleep(waitTime)
file = open(args.g, 'r') file = open(args.g, 'r')
lines = file.readlines() lines = file.readlines()
file.close() file.close()
@@ -30,10 +37,25 @@ if __name__ == "__main__":
for line in lines: for line in lines:
if not line == '\n': if not line == '\n':
ser.write(line.encode('UTF-8')) 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') 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() ser.close()
except SerialException as errSE: except SerialException as errSE:
print('Serial Exception' + str(errSE)) print('Serial Exception' + str(errSE))
except RuntimeError as errRE: except RuntimeError as errRE: