module:micropython:esp32udpclientserver:esp32udpclientserver02v01
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| module:micropython:esp32udpclientserver:esp32udpclientserver02v01 [2021/12/13 11:25] – [Übersicht] omdevelop | module:micropython:esp32udpclientserver:esp32udpclientserver02v01 [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Esp32UdpClientServer | + | ====== Esp32UdpClientServer |
| [[http:// | [[http:// | ||
| [ | [ | ||
| Line 14: | Line 14: | ||
| ===== Benutzung ===== | ===== Benutzung ===== | ||
| + | **Esp32UdpClientServer.py** | ||
| + | <code python> | ||
| + | # | ||
| + | # Syntax-Arguments: | ||
| + | # | ||
| + | # WindowsPC[192.168.178.33] <-> # WindowsPC[192.168.178.33]: | ||
| + | # WinPC: python CheckUdpClientServer.py Windows 4321 192.168.178.33 4321 | ||
| + | |||
| + | # WindowsPC[192.168.178.33] <-> UbuntuPC[192.168.178.48] | ||
| + | # WinPC: python CheckUdpClientServer.py Windows 5001 192.168.178.48 5000 | ||
| + | # LinPC: python CheckUdpClientServer.py Ubuntu | ||
| + | # | ||
| + | import sys | ||
| + | import time | ||
| + | import socket as SKT | ||
| + | # | ||
| + | import Thread as THR | ||
| + | import UdpClientServer as UCS | ||
| + | # | ||
| + | UDP_HEADERID = ' | ||
| + | # | ||
| + | # UDP - TxD - TransmitData | ||
| + | # | ||
| + | UDP_IPADDRESS_TX = ' | ||
| + | UDP_IPPORT_TX = 5000 | ||
| + | # | ||
| + | # | ||
| + | # UDP - RxD - ReceiveData | ||
| + | # | ||
| + | UDP_IPADDRESS_LOCAL = ' | ||
| + | UDP_IPPORT_RX = 5000 | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def CBOnTxData(udpclientserver, | ||
| + | Line = ' | ||
| + | print(Line) | ||
| + | return | ||
| + | # | ||
| + | def CBOnRxData(udpclientserver, | ||
| + | Line = ' | ||
| + | print(Line) | ||
| + | return | ||
| + | # | ||
| + | def CBOnStart(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnBusy(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnAbort(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | def CBOnEnd(udpclientserver): | ||
| + | # debug print(' | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | # Main | ||
| + | # | ||
| + | if (' | ||
| + | # | ||
| + | print(' | ||
| + | if (5 <= len(sys.argv)): | ||
| + | # Analyse Arguments für Tx/ | ||
| + | UDP_HEADERID = sys.argv[1] | ||
| + | # RX | ||
| + | UDP_IPPORT_RX = int(sys.argv[2]) | ||
| + | # TX | ||
| + | UDP_IPADDRESS_TX = sys.argv[3] | ||
| + | UDP_IPPORT_TX = int(sys.argv[4]) | ||
| + | # | ||
| + | UdpCS = UCS.CUdpClientServer(UDP_HEADERID, | ||
| + | | ||
| + | | ||
| + | | ||
| + | # | ||
| + | print(' | ||
| + | | ||
| + | | ||
| + | print(' | ||
| + | | ||
| + | | ||
| + | UdpCS.Open() | ||
| + | for I in range(0, 6): | ||
| + | time.sleep(5.0) | ||
| + | UdpCS.Transmit(' | ||
| + | time.sleep(1.0) | ||
| + | UdpCS.Close() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | **UdpClientServer.py** | ||
| <code python> | <code python> | ||
| + | # | ||
| + | import time | ||
| + | import enum as ENU | ||
| + | import socket as SKT | ||
| + | # | ||
| + | import Thread as THR | ||
| + | # | ||
| + | class CUdpClientServer(): | ||
| + | # | ||
| + | def __init__(self, | ||
| + | | ||
| + | | ||
| + | | ||
| + | self.HeaderID = headerid | ||
| + | self.IPAddressRXLocal = '' | ||
| + | self.IPPortRX = ipportrx | ||
| + | self.IPAddressTX = ipaddresstx | ||
| + | self.IPPortTX = ipporttx | ||
| + | self.OnTxData = ontxdata | ||
| + | self.OnRxData = onrxdata | ||
| + | self.OnStart = onstart | ||
| + | self.OnBusy = onbusy | ||
| + | self.OnAbort = onabort | ||
| + | self.OnEnd = onend | ||
| + | self.Thread = THR.CThread(self.CBOnStart, | ||
| + | self.CBOnAbort, | ||
| + | self.RxSocket = None | ||
| + | self.TxSocket = None | ||
| + | # find local IPAddress: | ||
| + | S = SKT.socket(SKT.AF_INET, | ||
| + | S.connect((' | ||
| + | self.IPAddressRXLocal = S.getsockname()[0] | ||
| + | S.close() | ||
| + | # | ||
| + | return | ||
| + | # | ||
| + | def GetHeaderID(self): | ||
| + | return self.HeaderID | ||
| + | def GetIPAddressTX(self): | ||
| + | return self.IPAddressTX | ||
| + | def GetIPPortTX(self): | ||
| + | return self.IPPortTX | ||
| + | def GetIPAddressRXLocal(self): | ||
| + | return self.IPAddressRXLocal | ||
| + | def GetIPPortRX(self): | ||
| + | return self.IPPortRX | ||
| + | # | ||
| + | def IsBusy(self): | ||
| + | return THR.EStateThread.stBusy == self.Thread.State | ||
| + | # | ||
| + | def Open(self): | ||
| + | # Rx | ||
| + | if (THR.EStateThread.stBusy == self.Thread.State): | ||
| + | self.Thread.Abort() | ||
| + | self.RxSocket = SKT.socket(SKT.AF_INET, | ||
| + | # FORWARD Socket.bind !!! | ||
| + | self.RxSocket.setsockopt(SKT.SOL_SOCKET, | ||
| + | self.RxSocket.bind((self.IPAddressRXLocal, | ||
| + | self.RxSocket.setblocking(0) | ||
| + | self.Thread.Start() | ||
| + | # Tx | ||
| + | self.TxSocket = SKT.socket(SKT.AF_INET, | ||
| + | self.TxSocket.connect((self.IPAddressTX, | ||
| + | return | ||
| + | # | ||
| + | def Close(self): | ||
| + | self.Thread.Abort() | ||
| + | self.RxSocket.close() | ||
| + | self.TxSocket.close() | ||
| + | # | ||
| + | def Abort(self): | ||
| + | self.Thread.Abort() | ||
| + | return | ||
| + | # | ||
| + | def Transmit(self, | ||
| + | if (None != self.OnTxData): | ||
| + | self.OnTxData(self, | ||
| + | self.TxSocket.sendto(text.encode(' | ||
| + | | ||
| + | # | ||
| + | def CBOnStart(self, | ||
| + | if (None != self.OnStart): | ||
| + | self.OnStart(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnBusy(self, | ||
| + | while self.IsBusy(): | ||
| + | RxData = '' | ||
| + | try: | ||
| + | Data, Address = self.RxSocket.recvfrom(1024) | ||
| + | RxData = Data.decode(' | ||
| + | except SKT.error: | ||
| + | pass | ||
| + | else: | ||
| + | # print(' | ||
| + | if (None != self.OnRxData): | ||
| + | self.OnRxData(self, | ||
| + | finally: | ||
| + | time.sleep(0.1) | ||
| + | if (None != self.OnBusy): | ||
| + | self.OnBusy(self) | ||
| + | self.RxSocket.close() | ||
| + | return | ||
| + | # | ||
| + | def CBOnAbort(self, | ||
| + | self.RxSocket.close() | ||
| + | if (None != self.OnAbort): | ||
| + | self.OnAbort(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnEnd(self, | ||
| + | self.RxSocket.close() | ||
| + | if (None != self.OnEnd): | ||
| + | self.OnEnd(self) | ||
| + | return | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| + | **Thread.py** | ||
| + | <code python> | ||
| + | import time | ||
| + | import _thread as THR | ||
| + | # States | ||
| + | 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 | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # Check Thread | ||
| + | # | ||
| + | def CBOnStart(thread): | ||
| + | print(' | ||
| + | return | ||
| + | def CBOnBusy(thread): | ||
| + | print(' | ||
| + | Counter = 0 | ||
| + | while (stBusy == thread.State): | ||
| + | Counter += 1 | ||
| + | print(Counter) | ||
| + | if (3 <= Counter): | ||
| + | thread.Abort() | ||
| + | else: | ||
| + | time.sleep(1.0) | ||
| + | return | ||
| + | def CBOnAbort(thread): | ||
| + | print(' | ||
| + | return | ||
| + | def CBOnEnd(thread): | ||
| + | print(' | ||
| + | return | ||
| + | # | ||
| + | if (' | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | Thread = CThread(CBOnStart, | ||
| + | Thread.Start() | ||
| + | time.sleep(5.0) | ||
| + | Thread.Abort() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | MicroPython-Terminal-Commands (Comments in Helper.h): | ||
| + | <code python> | ||
| + | > import os | ||
| + | > print(os.listdir()) | ||
| + | </ | ||
| + | |||
| + | **Ampy**-Commands: | ||
| + | <code python> | ||
| + | > ampy -p COM22 ls | ||
| + | </ | ||
| + | <code python> | ||
| + | > ampy -p COM22 rm / | ||
| + | </ | ||
| <code python> | <code python> | ||
| + | > ampy -p COM22 reset | ||
| </ | </ | ||
| + | !!! NOCH ERGÄNZEN !!! | ||
| Ausgabe in Terminal: | Ausgabe in Terminal: | ||
| <code python> | <code python> | ||
module/micropython/esp32udpclientserver/esp32udpclientserver02v01.1639391111.txt.gz · Last modified: 2021/12/13 12:25 (external edit)