import os import utime from machine import UART, Pin #by D@CC #Date: 2021FJun19 #Note: as of today, partially tested ########################################################################################## # 1. serRx_pico() receives a full serial text message # 2. show() shows (displays) an object or variable # 3. mpyread() reads a character (borrowed routine) # testRx receives all test messages (TimeOut=t1000c) progName="testRxD_pico.py" print("progName:"+progName) if True: bps=600 txPin=5 rxPin=4 rxtxEndChar="]" # #print sys info print(os.uname()) #os, utime and machine were imported at top #end if #every parameter has a default setting except the uartObject (which needs to be defined). t5c=50/bps # 50 bit times = 5 character times t10c=100/bps # 100 bit times = 10 character times t1000c=10000/bps # 10000 bit times = 1000 character times t9999c=10000/bps # 100000 bit times t60=60 # 60 seconds t10=10 # requires a uart object (below) to be created before this function is called #uart = machine.UART(1, 300, parity=None, stop=1, bits=8, tx=Pin(txPin), rx=Pin(rxPin)) print("entering serTx_pico.py") if rxtxEndChar=="]": som = "[" eom = "]" #end if #create uart object sample code if False : #self.uart = UART(1, 115200, parity=None, stop=1, bits=8, rx=Pin(rxPin), tx=Pin(txPin)) pass #end if #create actual uart object uart = machine.UART(1,bps, parity=None, stop=1, bits=8, rx=Pin(5), tx=Pin(4)) #print uart info #uart = machine.UART(1) print(uart) # during testing def mpyRead(uart): print("in mpyRead") #keep read1 character packet until not any characters txtRead="" bRead="" while uart.any(): aByte=str(uart.read(1)) #show("aByte:",aByte) bRead=bChar(aByte) #show("bRead:",bRead) txtRead=txtRead+bRead #end while return txtRead #end def # serRx_pico() receives a full serial text message #serRx_pico() def serRx_pico(uartObject, bps=600, timeOut=-1, eom="]"): #uart = machine.UART(1,bps, parity=None, stop=1, bits=8, rx=Pin(5), tx=Pin(4)) print("my uart:",uart) #print("timeOut is set to:",timeOut) if timeOut==-1: t100c = 1000/bps timeOutUse=int(t100c+1.0) #at least 1 sec #show("timeOutUse:",timeOutUse) else: timeOutUse=timeOut #in seconds #end if #timeOutUse=t100c time=utime.time() timeNow=time #show("timeNow:",timeNow) timeFail=timeNow+timeOutUse # defaults to a 3 second TimeOut #print("waiting my:",timeOutUse," seconds for data receipt. . . . .") oP="" # reset input buffer #show("time:",time) #show("timeFail:",timeFail) k=0 while time < timeFail : k=k+1 #keep reading any 1 character packets until timeOut print(k,"waiting for any characters to arrive") strA=mpyRead(uart) #show("strA:",strA) oP=oP+strA #show("oP:",oP) #end if time=utime.time() utime.sleep(.5) #end while #show("oP:",oP) lenOp=len(oP) if oP=="": print("ERROR T in serRx (): timeOut after",timeOutUse,"seconds") lenOp=len(oP) #end if if lenOp > 0: if oP.find("?")==-1: pass # ? not found else: print("WARNING '?' seen in message received.") #end if #end if #print("serRx() received:"+oP+":") #during testing return oP #end def serRx_pico(uart,300,timeOut(in secs)) def show(strA,parmA): # show print(strA,parmA) return #end def print("receiving my routine") timeNow=utime.time() tOut=60 print("waiting", tOut,"seconds to receive data, time:",0) #msgIn=serRx_pico(uart,bps=600) #times out after t100c seconds (100 char-times) msgIn=serRx_pico(uart,bps=600,timeOut=tOut) #times out after t10 seconds (100 char-times) print("msgIn:",msgIn) time=utime.time()-timeNow print("time:",time) print("end of serRX()") #end /testRxD_pico.py