forked from birkenfeld/ads_forwarder_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbh_sim.py
More file actions
40 lines (37 loc) · 1.32 KB
/
bh_sim.py
File metadata and controls
40 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import socket
import struct
BECKHOFF_TCP_PORT = 48898 # 0xBF02
BECKHOFF_UDP_PORT = 48899 # 0xBF03
BECKHOFF_UDP_PORT2 = 48847 # 0xBECF
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', BECKHOFF_TCP_PORT))
s.listen(5)
while True:
print('accepting...')
(sock, addr) = s.accept()
print('connected!')
while 1:
print('read message...')
try:
data = b''
while len(data) < 6:
newdata = sock.recv(6 - len(data))
if not newdata:
raise ValueError("connection broken")
data += newdata
print(f'data: {data!r}')
_zero, _size = struct.unpack('<HI', data)
if _zero != 0:
raise ValueError('Wrong Format (zero != 0)')
while len(data) < _size + 6:
data += sock.recv(_size + 6 - len(data))
if len(data) > _size + 6:
print(f'Too much data received: {len(data)} {_size + 6} '
f'{data!r}')
except Exception as e:
print('Exception reading from master:', e)
break
print('return message')
data = data[:6] + data[14:22] + data[6:14] + data[22:]
sock.sendall(data)