- # this code turns micropython into a AT command set interpreter on uart1
- import os
- import time
- import machine
- import random
- swVersion = "+AT Commands on MicroPython v0.001"
- print("uart1 9600 bps test")
- print(os.uname())
- uart = machine.UART(1, 9600) # init with given baudrate
- #uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
- uart.write('I am pi pico running '+str(os.uname())+"\r\n") # write the 3 characters
- serialBuffer = bytearray(512)
- serialBufferPointer = 0
- serialBufferLen = len(serialBuffer)
- incomingCommand = ""
- knownCommands = {
- "+ID":"id",
- "+INFO":"info",
- "+CCLK":"cclk",
- "+NVRAM_":"nvram", #at+nvram22=01 02 03 04 nv ram starting at byte 22 will be 1,2,3,4
- "+NVRAM__":"nvram", #at+nvram22? or at+nvram22=? should return value at byte 22
- "+NVRAM___":"nvram",
- "+NVDUMP":"nvdump",
- "+RAND":"make_rand",
- "?":"help"
- }
- def help(arguments):
- uartWriter("+Known Commands")
- for oneCommand in knownCommands:
- uartWriter(oneCommand)
- def make_rand(arguments):
- uartWriter("+Arguments["+arguments+"]")
- uartWriter(arguments[1:])
- maxValue = getInt(arguments)
- uartWriter("+MaxValue = "+str(maxValue))
- uartWriter("+MinValue = 0")
- uartWriter("+Rand = "+ str(random.randint(0, maxValue)))
- return True
- def id(arguments):
- global uart, swVersion
- print("id requested"+str(arguments))
- uartWriter(swVersion)
- return True
- def info(arguments):
- print("info requested")
- uartWriter("Is this the info you want?")
- return True
- def cclk(arguments):
- print("time requested")
- timeNow = "+" + str(time.ticks_us())
- uartWriter(timeNow)
- return True
- #####################
- def hexDumpStr(theStr):
- #print("hexdump_type",type(theStr))
- for character in theStr:
- print(hex(ord(character)),end='')
- print()
- def getInt(inStrVal):
- #hexDumpStr(inStrVal)
- retInt = -1
- try:
- retInt = int((inStrVal))
- except:
- retInt = -1
- print("Error converting Int["+str(inStrVal)+"]")
- return retInt
- def serialBufferToString():
- global serialBuffer, serialBufferPointer
- returnValue = ""
- i = 0
- for i in range(0,serialBufferPointer):
- # print("i",i,"=",chr(serialBuffer[i]))
- returnValue += chr(serialBuffer[i])
- return returnValue.upper()
- def serialBufferFlush():
- global serialBuffer, serialBufferPointer
- i = 0
- while i < len(serialBuffer):
- serialBuffer[i] = 0x00
- i += 1
- serialBufferPointer = 0
- def parseIncomingTry(theCommand):
- retV = False
- try:
- retV = parseIncoming(theCommand)
- except:
- print("Error processing incoming")
- return False
- return retV
- def parseIncoming(theCommand):
- global knownCommands
- #print("type_theCommand",type(theCommand))
- if (len(theCommand)<3):
- return False
- #print ("theCommand["+theCommand+"]")
- aMatch = 0
- doCommand=""
- arguments=""
- for i in knownCommands:
- #print("i=",i,"func=",knownCommands[i])
- aMatch = compareCommand(i,theCommand)
- if (aMatch >= 1):
- doCommand = knownCommands[i]
- if (len(i)+1 >= len(theCommand) ):
- arguments=""
- else:
- arguments = theCommand[len(i)+3:]
- arguments = arguments.rstrip()
- if (aMatch == 2):
- arguments = theCommand
- #print("command Match",i,"doCommand",doCommand,"aMatch",aMatch)
- if len(doCommand) > 0:
- globals()[doCommand](arguments)
- else:
- print("ERROR")
- return False
- return True
- def compareCommand(needle,haystack):
- # _ underscore is a wildcard for numeric values
- retV = 0
- wildCardMatch = 0
- #print("needle:",needle)
- #print("haystack:",haystack)
- startPos = 2 # ignore the AT and any space
- #print("startPos:",startPos)
- compareLen = len(needle)
- #print("compareLen",compareLen)
- for i in range(0,compareLen):
- #print("i=",i)
- if (i+startPos >= len(haystack)):
- haystackChar = "*"
- else:
- haystackChar = haystack[i+startPos]
- needleChar = needle[i]
- #print("needleChar["+needleChar+"] haystackChar["+haystackChar+"]")
- if ( needleChar == haystackChar):
- retV = retV +1
- #print("hit")
- else:
- #print("hs["+haystack[i+startPos]+"]")
- if (needle[i]=='_' and haystackChar.isdigit() ): # >='0' and haystackChar <='9'):
- retV = retV + 1
- wildCardMatch = 1
- #print("compareLen",compareLen,"retV",retV)
- if (compareLen==retV):
- retV=1
- else:
- retV=0
- if (retV == 1) and (wildCardMatch ==1):
- retV = 2
- return retV
- #def processCommand(inCommand):
- # procCmdRet = "OK\r\n"
- # return procCmdRet
- def uartWriter(inStr):
- global uart
- uart.write(str(inStr))
- uart.write("\r\n")
- print(swVersion)
- uartWriter("+BOOT")
- uartWriter(swVersion)
- uartWriter("OK")
- while True:
- while uart.any():
- inByte = uart.read(1)
- serialBuffer[serialBufferPointer] = inByte[0]
- serialBufferPointer += 1
- if (inByte[0] == 0x0D or serialBufferPointer >= serialBufferLen):
- uart.write("\r\n") # flush output to terminal
- print("") # flush local output
- #print("==============CR============")
- #incomingCommand = serialBuffer.decode()
- incomingCommand = serialBufferToString()
- serialBufferFlush()
- #print("incoming Command["+incomingCommand+"]")
- #procCmdRet = processCommand(incomingCommand)
- #procCmdRet = parseIncoming(incomingCommand)
- procCmdRet = parseIncomingTry(incomingCommand)
- if (procCmdRet == 1):
- uart.write("OK\r\n")
- else:
- uart.write("ERROR\r\n")
- print(chr(inByte[0]),end='')
- #print("type inByte",type(inByte),"hex",hex(inByte[0]))
- uart.write(chr(inByte[0]))
- #print("serialBuffer",serialBuffer)
AT Commands v0.001
Posted by Anonymous on Sat 28th May 2022 02:33
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.