-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataExchange.py
42 lines (37 loc) · 1.05 KB
/
dataExchange.py
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
41
import socket
import numpy as np
def _recvdata(tcp, _row, _colum):
string = b''
bytes_read = 0
bytes_n = _row * _colum * 2
while bytes_read < bytes_n:
try:
chunk = tcp.recv(bytes_n - bytes_read)
except socket.timeout:
print("Receiver timeout")
return None
bytes_read += len(chunk)
string += chunk
array = np.fromstring(string, dtype=np.uint16)
array.shape = (_row, _colum)
return array
def _sendmsg(tcp, _msg):
msg_len = len(_msg)
total_sent = 0
_msg += "\0" * (50 - msg_len)
while total_sent < 50:
chunk_len = tcp.send(_msg[total_sent:].encode())
total_sent += chunk_len
def _recvhex(tcp, _len):
string = b''
bytes_read = 0
while bytes_read < _len:
try:
chunk = tcp.recv(_len - bytes_read)
except socket.timeout:
print("Receiver timeout")
return None
bytes_read += len(chunk)
string += chunk
array = np.fromstring(string, dtype=np.ubyte)
return array