====== MicroPython: Esp32Uart ====== [[http://www.openhardsoftware.de/ | Open Hard- & Software]] [ [[http://www.openhardsoftware.de/dokuwiki | DokuWiki]] [[http://www.openhardsoftware.de/websites | WebSites]] [[http://www.openhardsoftware.de/mediawiki | MediaWiki]] [[http://www.openhardsoftware.de/nextcloud | NextCloud]] ] ===== Übersicht ===== ==== Links ==== {{https://docs.micropython.org/en/latest/esp32/quickref.html#uart-serial-bus | Esp32-Reference}} \\ {{https://docs.micropython.org/en/latest/library/machine.UART.html#machine-uart | Esp32-Machine-Uart}} \\ {{https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/uart#uartreadlinemax_len | Machine Class UART}} \\ ===== Version ===== {{:module:micropython:Esp32Uart:2112191237_Esp32Uart_01V01.zip | 2112191237_Esp32Uart_01V01.zip}} ===== Beschreibung ===== **Ausgabe MicroPython-Terminal Esp32CheckUart.py** >>>Running ...Esp32CheckUart.py >>> *** Esp32CheckUart: begin CBUartOnTxLine[000 ] CBUartOnRxLine[000 ] CBUartOnTxLine[111 ] CBUartOnTxLine[222 ] CBUartOnRxLine[111 ] CBUartOnTxLine[333 ] CBUartOnRxLine[222 ] CBUartOnTxLine[444 ] CBUartOnRxLine[333 ] CBUartOnTxLine[555 ] CBUartOnRxLine[444 ] CBUartOnTxLine[666 ] CBUartOnRxLine[555 ] CBUartOnTxLine[777 ] CBUartOnRxLine[666 ] CBUartOnTxLine[888 ] CBUartOnRxLine[777 ] CBUartOnTxLine[999 ] CBUartOnRxLine[888 ] CBUartOnRxLine[999 ] *** Esp32CheckUart: end >>> **Main-Program: Esp32CheckUart.py** # import time as TIM # import Define as DEF import Uart as UART # #------------------------------------------------------------------- # Callback - Uart #------------------------------------------------------------------- def CBUartOnRxLine(rxline): print('CBUartOnRxLine[{}]'.format(rxline)) # def CBUartOnTxLine(txline): print('CBUartOnTxLine[{}]'.format(txline)) # #------------------------------------------------------------------- # Main - Uart #------------------------------------------------------------------- if ('__main__' == __name__): print('*** Esp32CheckUart: begin') # Uart = UART.CUart(DEF.ID_UART1, DEF.PIN_UART1_TX, DEF.PIN_UART1_RX) Uart.SetOnRxLine(CBUartOnRxLine) Uart.SetOnTxLine(CBUartOnTxLine) Uart.Open() # Uart.TxLine('000 ') Uart.TxLine('111 ') Uart.TxLine('222 ') Uart.TxLine('333 ') Uart.TxLine('444 ') Uart.TxLine('555 ') Uart.TxLine('666 ') Uart.TxLine('777 ') Uart.TxLine('888 ') Uart.TxLine('999 ') TIM.sleep(2.0) # Uart.Close() # print('*** Esp32CheckUart: end') # # **Library-Module: Uart.py** # import time from machine import UART # import Define import Lines as LIN import Thread as THR # class CUart(): #--------------------------------------------------------- # Constructor #--------------------------------------------------------- def __init__(self, uartid, txpin, rxpin): self.Uart = UART(uartid, tx=txpin, rx=rxpin) self.TxLines = LIN.CLines() self.RxLines = LIN.CLines() self.OnRxLine = None self.OnTxLine = None self.Thread = THR.CThread(self.CBOnStart, self.CBOnBusy, self.CBOnAbort, self.CBOnEnd) self.RxLine = '' #--------------------------------------------------------- # Property #--------------------------------------------------------- def SetOnRxLine(self, onrxline): self.OnRxLine = onrxline def SetOnTxLine(self, ontxline): self.OnTxLine = ontxline #--------------------------------------------------------- # Callback Thread #--------------------------------------------------------- def CBOnStart(self, thread): return def CBOnBusy(self, thread): while (THR.stBusy == thread.State): # first: RxData while (0 < self.Uart.any()): C = self.Uart.read(1) if (b'\r' == C) or (b'\n' == C): if (0 < len(self.RxLine)): self.RxLines.Push(self.RxLine) self.RxLine = ''; else: self.RxLine += C.decode("utf-8") if (0 < self.RxLines.Count()): Line = self.RxLines.Pop() if (None != self.OnRxLine): self.OnRxLine(Line) # second: TxData if (0 < self.TxLines.Count()): Line = self.TxLines.Pop() self.Uart.write(Line + '\r\n') if (None != self.OnTxLine): self.OnTxLine(Line) def CBOnAbort(self, thread): return def CBOnEnd(self, thread): return #--------------------------------------------------------- # Management #--------------------------------------------------------- def Open(self): self.Uart.init(baudrate=115200)#, bits=8, parity=None, stop=1, cts=-1, rts=-1) self.Thread.Start() self.RxLine = '' return # def Close(self): self.Thread.Abort() self.Uart.deinit() return # def TxLine(self, line): self.TxLines.Push(line) # # #------------------------------------------------------------- # Uart - Main - Check Library #------------------------------------------------------------- def CBOnRxLine(rxline): print('CBOnRxLine[{}]'.format(rxline)) # def CBOnTxLine(txline): print('CBOnTxLine[{}]'.format(txline)) # if ('__main__' == __name__): print('*** CheckUart: begin') # Uart = CUart(Define.ID_UART1, Define.PIN_UART1_TX, Define.PIN_UART1_RX) Uart.SetOnRxLine(CBOnRxLine) Uart.SetOnTxLine(CBOnTxLine) Uart.Open() # Uart.TxLine('000asdf') Uart.TxLine('111asdf') time.sleep(2.0) # Uart.Close() # print('*** CheckUart: end') # **Library-Module: Thread.py** # import time import _thread as THR # # States - EStateThread : stIdle = 0 stBusy = 1 stEnd = 2 # class CThread(): # def __init__(self, onstart, onbusy, onabort, onend): self.State = stIdle self.Thread = None self.OnStart = onstart self.OnBusy = onbusy self.OnAbort = onabort self.OnEnd = onend return # def Start(self): self.State = stBusy if (None != self.OnStart): self.OnStart(self) self.ThreadID = THR.start_new_thread(self.CBOnExecute, [self]) return # def Abort(self): self.State = stEnd if (None != self.OnAbort): self.OnAbort(self) return # def CBOnExecute(self, thread): if (None != self.OnBusy): self.OnBusy(self) if (None != self.OnEnd): self.OnEnd(self) return # #------------------------------------------------------------- if ('__main__' == __name__): print('*** Check Thread: begin') # # Thread = CThread(CBOnStart, CBOnBusy, CBOnAbort, CBOnEnd) # Thread.Start() # time.sleep(5.0) # Thread.Abort() # print('*** Check Thread: end') # **Library-Module: Lines.py** # # CLines : FirstIn-FirstOut class CLines(list): # def __init__(self): self = [] # def Count(self): return len(self) # def Push(self, line): self.append(line) return # def Pop(self): Line = '' if (0 < self.Count()): Line = self.pop(0) return Line **Library-Module: Define.py** # ID_UART0 = 0 PIN_UART0_TX = 1 # USB PIN_UART0_RX = 3 # USB # ID_UART1 = 1 PIN_UART1_RX = 25 # 9 - not usable PIN_UART1_TX = 26 # 10 - not usable # ID_UART2 = 2 PIN_UART2_RX = 16 PIN_UART2_TX = 17 # ===== Entwicklung ===== ==== 211219 : Esp32Uart ==== * Version {{:module:micropython:Esp32Uart:2112191209_Esp32Uart_00V02.zip | 2112191209_Esp32Uart_00V02.zip}} ==== 211218 : Esp32Uart ==== * Uart0 (UsbPC) : Programmier-/Debug-Schnittstelle VSCode * Uart1 (PIN_UART1_RX(25) rückgekoppelt mit PIN_UART1_TX(26)) * Version {{:module:micropython:Esp32Uart:2112181850_Esp32Uart_00V01.zip | 2112181850_Esp32Uart_00V01.zip}} ** Esp32Uart.py : Senden/Empfangen bei Tx/Rx-Kabel-Rückkopplung:** >>> Running c:\Downloads\python\Esp32Uart\Uart.py >>> *** CheckUart: begin CBOnTxLine[111asdf] CBOnRxLine[111asdf] CBOnTxLine[222asdf] CBOnRxLine[222asdf] CBOnTxLine[333asdf] CBOnRxLine[333asdf] *** CheckUart: end >>> ==== 211217 : Erste Versuche ==== * Ausgliederung von [[:Module:MicroPython:CommandLineDispatcher:CommandLineDispatcher | CommandLineDispatcher]] **Uart-Pin-Assignment Esp32** in **Define.py** ID_UART0 = 0 PIN_UART0_TX = 1 # USB PIN_UART0_RX = 3 # USB # ID_UART1 = 1 PIN_UART1_RX = 25 # 9 - not usable PIN_UART1_TX = 26 # 10 - not usable # ID_UART2 = 2 PIN_UART2_RX = 16 PIN_UART2_TX = 17 ----- [[http://www.openhardsoftware.de/ | Open Hard- & Software]] [ [[http://www.openhardsoftware.de/dokuwiki | DokuWiki]] [[http://www.openhardsoftware.de/websites | WebSites]] [[http://www.openhardsoftware.de/mediawiki | MediaWiki]] [[http://www.openhardsoftware.de/nextcloud | NextCloud]] ]