module:micropython:esp32uart:esp32uart
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| module:micropython:esp32uart:esp32uart [2021/12/19 12:13] – [Entwicklung] omdevelop | module:micropython:esp32uart:esp32uart [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 14: | Line 14: | ||
| {{https:// | {{https:// | ||
| {{https:// | {{https:// | ||
| - | ==== Beschreibung ==== | + | ===== Version ===== |
| + | {{: | ||
| + | |||
| + | ===== Beschreibung ===== | ||
| + | |||
| + | **Ausgabe MicroPython-Terminal Esp32CheckUart.py** | ||
| + | <code python> | ||
| + | >>> | ||
| + | >>> | ||
| + | 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: | ||
| + | >>> | ||
| + | </ | ||
| - | ==== Quell-Code==== | ||
| **Main-Program: | **Main-Program: | ||
| <code python> | <code python> | ||
| + | # | ||
| + | import time as TIM | ||
| + | # | ||
| + | import Define as DEF | ||
| + | import Uart as UART | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def CBUartOnRxLine(rxline): | ||
| + | print(' | ||
| + | # | ||
| + | def CBUartOnTxLine(txline): | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| + | # Main - Uart | ||
| + | # | ||
| + | if (' | ||
| + | print(' | ||
| + | # | ||
| + | Uart = UART.CUart(DEF.ID_UART1, | ||
| + | Uart.SetOnRxLine(CBUartOnRxLine) | ||
| + | Uart.SetOnTxLine(CBUartOnTxLine) | ||
| + | Uart.Open() | ||
| + | # | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | TIM.sleep(2.0) | ||
| + | # | ||
| + | Uart.Close() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| **Library-Module: | **Library-Module: | ||
| <code python> | <code python> | ||
| + | # | ||
| + | import time | ||
| + | from machine import UART | ||
| + | # | ||
| + | import Define | ||
| + | import Lines as LIN | ||
| + | import Thread as THR | ||
| + | # | ||
| + | class CUart(): | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def __init__(self, | ||
| + | self.Uart = UART(uartid, | ||
| + | self.TxLines = LIN.CLines() | ||
| + | self.RxLines = LIN.CLines() | ||
| + | self.OnRxLine = None | ||
| + | self.OnTxLine = None | ||
| + | self.Thread = THR.CThread(self.CBOnStart, | ||
| + | self.RxLine = '' | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def SetOnRxLine(self, | ||
| + | self.OnRxLine = onrxline | ||
| + | def SetOnTxLine(self, | ||
| + | self.OnTxLine = ontxline | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def CBOnStart(self, | ||
| + | return | ||
| + | def CBOnBusy(self, | ||
| + | while (THR.stBusy == thread.State): | ||
| + | # first: RxData | ||
| + | while (0 < self.Uart.any()): | ||
| + | C = self.Uart.read(1) | ||
| + | if (b' | ||
| + | if (0 < len(self.RxLine)): | ||
| + | self.RxLines.Push(self.RxLine) | ||
| + | self.RxLine = ''; | ||
| + | else: | ||
| + | self.RxLine += C.decode(" | ||
| + | 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 + ' | ||
| + | if (None != self.OnTxLine): | ||
| + | self.OnTxLine(Line) | ||
| + | def CBOnAbort(self, | ||
| + | return | ||
| + | def CBOnEnd(self, | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def Open(self): | ||
| + | self.Uart.init(baudrate=115200)#, | ||
| + | self.Thread.Start() | ||
| + | self.RxLine = '' | ||
| + | return | ||
| + | # | ||
| + | def Close(self): | ||
| + | self.Thread.Abort() | ||
| + | self.Uart.deinit() | ||
| + | return | ||
| + | # | ||
| + | def TxLine(self, | ||
| + | self.TxLines.Push(line) | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # Uart - Main - Check Library | ||
| + | # | ||
| + | def CBOnRxLine(rxline): | ||
| + | print(' | ||
| + | # | ||
| + | def CBOnTxLine(txline): | ||
| + | print(' | ||
| + | # | ||
| + | if (' | ||
| + | print(' | ||
| + | # | ||
| + | Uart = CUart(Define.ID_UART1, | ||
| + | Uart.SetOnRxLine(CBOnRxLine) | ||
| + | Uart.SetOnTxLine(CBOnTxLine) | ||
| + | Uart.Open() | ||
| + | # | ||
| + | Uart.TxLine(' | ||
| + | Uart.TxLine(' | ||
| + | time.sleep(2.0) | ||
| + | # | ||
| + | Uart.Close() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| </ | </ | ||
| + | |||
| **Library-Module: | **Library-Module: | ||
| <code python> | <code python> | ||
| + | # | ||
| + | import time | ||
| + | import _thread as THR | ||
| + | # | ||
| + | # States - EStateThread : | ||
| + | stIdle = 0 | ||
| + | stBusy = 1 | ||
| + | stEnd = 2 | ||
| + | # | ||
| + | class CThread(): | ||
| + | # | ||
| + | def __init__(self, | ||
| + | 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, | ||
| + | return | ||
| + | # | ||
| + | def Abort(self): | ||
| + | self.State = stEnd | ||
| + | if (None != self.OnAbort): | ||
| + | self.OnAbort(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnExecute(self, | ||
| + | if (None != self.OnBusy): | ||
| + | self.OnBusy(self) | ||
| + | if (None != self.OnEnd): | ||
| + | self.OnEnd(self) | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | if (' | ||
| + | print(' | ||
| + | # | ||
| + | # Thread = CThread(CBOnStart, | ||
| + | # Thread.Start() | ||
| + | # time.sleep(5.0) | ||
| + | # Thread.Abort() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| </ | </ | ||
| + | |||
| + | **Library-Module: | ||
| + | <code python> | ||
| + | # | ||
| + | # 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: | ||
| + | <code python> | ||
| + | # | ||
| + | ID_UART0 | ||
| + | PIN_UART0_TX = 1 # USB | ||
| + | PIN_UART0_RX = 3 # USB | ||
| + | # | ||
| + | ID_UART1 | ||
| + | PIN_UART1_RX = 25 # 9 - not usable | ||
| + | PIN_UART1_TX = 26 # 10 - not usable | ||
| + | # | ||
| + | ID_UART2 | ||
| + | PIN_UART2_RX = 16 | ||
| + | PIN_UART2_TX = 17 | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| ===== Entwicklung ===== | ===== Entwicklung ===== | ||
| ==== 211219 : Esp32Uart ==== | ==== 211219 : Esp32Uart ==== | ||
| + | * Version {{: | ||
| ==== 211218 : Esp32Uart ==== | ==== 211218 : Esp32Uart ==== | ||
| * Uart0 (UsbPC) : Programmier-/ | * Uart0 (UsbPC) : Programmier-/ | ||
module/micropython/esp32uart/esp32uart.1639912403.txt.gz · Last modified: 2021/12/19 13:13 (external edit)