#====================================== def recvFromArduino(): global startMarker, endMarker ck = "" x = "z" # any value that is not an end- or startMarker byteCount = -1 # to allow for the fact that the last increment will be one too many # wait for the start character while ord(x) != startMarker: x = ser.read() # save data until the end marker is found while ord(x) != endMarker: if ord(x) != startMarker: ck = ck + x.decode("utf-8") # change for Python3 byteCount += 1 x = ser.read() return(ck) #============================ def waitForArduino(): # wait until the Arduino sends 'Ok' - allows time for Arduino reset # it also ensures that any bytes left over from a previous message are discarded global startMarker, endMarker msg = "" while msg.find("ok") == -1: while ser.inWaiting() == 0: pass msg = recvFromArduino() print (msg) # python3 requires parenthesis print () #====================================== import serial import time print () print () # NOTE the user must ensure that the serial port and baudrate are correct # serPort = "/dev/ttyS80" serPort = "COM4" baudRate = 9600 ser = serial.Serial(serPort, baudRate) print ("Serial port " + serPort + " opened Baudrate " + str(baudRate)) startMarker = 60 endMarker = 62 fileName = "mygcode.txt" # edit to file name to read file = open(fileName) with open(fileName) as f: line_count = 0 for line in f: line_count += 1 print ("Lines to execute " + str(line_count)) waitForArduino() waitingForReply = False # meaning it is clear to send line to Arduino while 1: line = file.readline() if not line: break if waitingForReply == False: waitingForReply = True ser.write(str.encode(line)) print ("reading file and writing line to arduino " + line) print ("Lines remaining to write " + str(line_count)) line_count = line_count - 1 if waitingForReply == True: print ("waiting for reply") while ser.inWaiting() == 0: pass dataRecvd = recvFromArduino() print ("Reply Received " + dataRecvd) if 'ok' in dataRecvd: print ('Okay found in data received') waitingForReply = False print ("===========") file.close print ('file closed') print ser.close print ('serial closed')