module:python:udpclientserver:udpclientserver02v03
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| module:python:udpclientserver:udpclientserver02v03 [2021/12/13 20:00] – [Class-Library: Thread.py] omdevelop | module:python:udpclientserver:udpclientserver02v03 [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| ===== Benutzung ===== | ===== Benutzung ===== | ||
| - | + | PC-Python-Software: | |
| - | **...py** | + | {{: |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | **...py** | + | |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | **...py** | + | |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | **...py** | + | |
| - | < | + | |
| - | </ | + | |
| Line 33: | Line 18: | ||
| ===== Hauptprogramm: | ===== Hauptprogramm: | ||
| <code python> | <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 = 5001 | ||
| + | # | ||
| + | # | ||
| + | # 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/ | ||
| + | # | ||
| + | # # RX | ||
| + | # | ||
| + | # # TX | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | UdpCS = UCS.CUdpClientServer(UDP_HEADERID, | ||
| + | | ||
| + | | ||
| + | | ||
| + | # | ||
| + | print(' | ||
| + | | ||
| + | | ||
| + | print(' | ||
| + | | ||
| + | | ||
| + | UdpCS.Open() | ||
| + | while UdpCS.IsBusy(): | ||
| + | UdpCS.Transmit(' | ||
| + | time.sleep(3.0) | ||
| + | UdpCS.Close() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| ===== Class-Library: | ===== Class-Library: | ||
| <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 | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| ===== Class-Library: | ===== Class-Library: | ||
| <code python> | <code python> | ||
| + | # | ||
| + | import time | ||
| + | import threading as THD | ||
| + | import enum as ENU | ||
| + | # | ||
| + | class EStateThread(ENU.Enum): | ||
| + | stIdle = 0 | ||
| + | stBusy = 1 | ||
| + | stEnd = 2 | ||
| + | # | ||
| + | def CBOnExecute(thread): | ||
| + | if (None != thread.OnBusy): | ||
| + | thread.OnBusy(thread) | ||
| + | if (None != thread.OnEnd): | ||
| + | thread.OnEnd(thread) | ||
| + | return | ||
| + | # | ||
| + | class CThread(): | ||
| + | # | ||
| + | def __init__(self, | ||
| + | self.State = EStateThread.stIdle | ||
| + | self.Thread = THD.Thread(target=CBOnExecute, | ||
| + | self.OnStart = onstart | ||
| + | self.OnBusy = onbusy | ||
| + | self.OnAbort = onabort | ||
| + | self.OnEnd = onend | ||
| + | return | ||
| + | # | ||
| + | def Start(self): | ||
| + | self.State = EStateThread.stBusy | ||
| + | if (None != self.OnStart): | ||
| + | self.OnStart(self) | ||
| + | self.Thread.start() | ||
| + | return | ||
| + | # | ||
| + | def Abort(self): | ||
| + | self.State = EStateThread.stEnd | ||
| + | if (None != self.OnAbort): | ||
| + | self.OnAbort(self) | ||
| + | return | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| Line 50: | Line 285: | ||
| ===== Version ===== | ===== Version ===== | ||
| - | {{: | + | {{: |
| ----- | ----- | ||
module/python/udpclientserver/udpclientserver02v03.1639422041.txt.gz · Last modified: 2021/12/13 21:00 (external edit)