-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommunicator.py
365 lines (325 loc) · 11.3 KB
/
communicator.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*
"""
Created on 2019-05-07
@author: Ocean
"""
import sys
import os
import socket
import select
import time
import datetime
import json
import glob
import serial
import serial.tools.list_ports
class Communicator():
'''
'''
def __init__(self):
self.setting_folder = os.path.join(os.getcwd(), r'setting') # use to store some configuration files.
self.connection_file = os.path.join(self.setting_folder, 'connection.json')
self.read_size = 0
pass
def find_device(self):
pass
def open(self):
pass
def close(self):
pass
def write(self,data):
pass
def read(self,size):
pass
class SerialPort(Communicator):
def __init__(self):
Communicator.__init__(self)
self.ser = None # the active UART
self.port = None
self.baud = None
self.read_size = 100
pass
def find_device(self):
''' Finds active ports and then autobauds units
'''
try:
if self.try_last_port():
pass
else:
while not self.autobaud(self.find_ports()):
time.sleep(0.1)
return True
except KeyboardInterrupt: # response for KeyboardInterrupt such as Ctrl+C
print('User stop this program by KeyboardInterrupt! File:[{0}], Line:[{1}]'.format(__file__, sys._getframe().f_lineno))
return False
def find_ports(self):
''' Lists serial port names. Code from
https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python
Successfully tested on Windows 8.1 x64, Windows 10 x64, Mac OS X 10.9.x / 10.10.x / 10.11.x and Ubuntu 14.04 / 14.10 / 15.04 / 15.10 with both Python 2 and Python 3.
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
'''
print('scanning ports')
# port_list = list(serial.tools.list_ports.comports())
# ports = [ p.device for p in port_list]
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
print('Trying: ' + port)
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def autobaud(self, ports):
'''Autobauds unit - first check for stream_mode / continuous data, then check by polling unit
Converts resets polled unit (temporarily) to 100Hz ODR
:returns:
true when successful
'''
for port in ports:
for baud in [230400, 115200]:
self.open_serial_port(port, baud, 1.0) # Assume at least one whole frame can be grabed in 1 seconds.
if self.ser:
serial_data = bytearray(self.ser.read(300)) # Assume max_len of a frame is less than 300 bytes.
self.ser.close()
if self.find_header(serial_data):
self.port = port
self.baud = baud
print('Connected: {0} {1}'.format(self.port, self.baud))
self.save_last_port()
return True
return False
def try_last_port(self):
'''try to open serial port based on the port and baud read from connection.json.
try to find frame header in serial data.
returns: True if find header
False if not find header.
'''
connection = None
try:
with open(self.connection_file) as json_data:
connection = json.load(json_data)
if connection:
self.open_serial_port(port=connection['port'], baud=connection['baud'], timeout=1)
if self.ser:
serial_data = bytearray(self.ser.read(300)) # Assume max_len of a frame is less than 300 bytes.
self.ser.close()
if self.find_header(serial_data):
self.port = connection['port']
self.baud = connection['baud']
print('Connected: {0} {1}'.format(self.port, self.baud))
return True
else:
return False
else:
return False
except:
return False
def save_last_port(self):
if not os.path.exists(self.setting_folder):
try:
os.mkdir(self.setting_folder)
except:
return
connection = {"port" : self.ser.port, "baud" : self.ser.baudrate }
try:
with open(self.connection_file, 'w') as outfile:
json.dump(connection, outfile)
except:
pass
def open_serial_port(self, port=None, baud=115200, timeout=0.1):
''' open serial port
returns: true when successful
'''
try:
self.ser = serial.Serial(port, baud, timeout=timeout)
return True
except Exception as e:
print('serial port open exception, port:{0} baud:{1} timeout:{2}'.format(port, baud, timeout) )
self.ser = None
return False
def close_serial_port(self):
'''close serial port
'''
if self.ser is not None:
if self.ser.isOpen():
self.ser.close()
def find_header(self, data):
'''
sync.
return:
True: Successful.
False: Failed.
'''
rev = True if (data.find(b'\xAF\x20\x05') > -1 \
or data.find(b'\xAF\x20\x06') > -1 \
or data.find(b'\xAF\x20\x07') > -1) else False
return rev
def write(self,data):
'''
write the bytes data to the port
return:
length of data sent via serial port.
False: Exception when sending data, eg. serial port hasn't been opened.
'''
try:
return self.ser.write(data)
except Exception as e:
# print(e)
raise
def read(self,size):
'''
read size bytes from the serial port.
parameters: size – number of bytes to read.
returns: bytes read from the port.
return type: bytes
'''
try:
return self.ser.read(size)
except serial.SerialException:
print('Serial Exception! Please check the serial port connector is stable or not.')
raise
except Exception as e:
# print(e)
raise
def open(self):
return self.open_serial_port(self.port, self.baud, timeout=0.1)
def close(self):
return self.close_serial_port()
class TCPIP(Communicator):
def __init__(self, host ='127.0.0.1', port=8888):#'127.0.0.1' '192.168.31.223'
Communicator.__init__(self)
self.host = host
self.port = port
self.sock = None
self.read_size = 1024
pass
def find_device(self):
try:
while (True):
try:
print(datetime.datetime.now().strftime('[%Y_%m_%d %H_%M_%S]:') + 'connecting {0}:{1} ...'.format(self.host,self.port))
self.open()
print(datetime.datetime.now().strftime('[%Y_%m_%d %H_%M_%S]:') + 'connect {0}:{1} successfully.'.format(self.host,self.port))
# self.close()
return True
except socket.error :
time.sleep(1)
except Exception as e:
print(e)
except KeyboardInterrupt: # response for KeyboardInterrupt such as Ctrl+C
print('User stop this program by KeyboardInterrupt! File:[{0}], Line:[{1}]'.format(__file__, sys._getframe().f_lineno))
return False
def write(self,data):
'''
write the bytes data to host.
return:
length of data sent via TCPIP.
False: Exception when sending data, eg. host has closed.
'''
try:
return self.sock.send(data)
except socket.error :
print ("socket error,do reconnect.")
raise
except Exception as e:
print(e)
raise
def read(self,size):
'''
read size bytes via TCPIP.
parameters: size – number of bytes to read.
returns: bytes read from the port.
return type: bytes
'''
try:
flag = True
to_read, to_write, in_error = select.select([self.sock], [self.sock], [], 0.001)
if self.sock not in to_read:
flag = False
data = self.sock.recv(size)
if not flag and len(data) == 0:
raise socket.error ('Can not connect to server[{0}:{1}]'.format(self.host, self.port))#server closed.
else:
return data
except socket.error as e:
print(e)
print ("socket error,do reconnect.")
raise
except Exception as e:
print(e)
raise
except :
raise
def open(self):
if self.sock:
return True
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.connect((self.host, self.port))
return True
except socket.error :
self.sock = None
raise
except socket.timeout as e:
print(e)
except Exception as e:
self.sock = None
raise
def close(self):
if self.sock:
self.sock.close()
self.sock = None
class DataFile(Communicator):
def __init__(self, file_name):
Communicator.__init__(self)
self.file_name = file_name
self.file = None
self.read_size = 1024
pass
def find_device(self):
return False
def write(self,data):
'''
'''
pass
def read(self,size):
'''
read size bytes via TCPIP.
parameters: size – number of bytes to read.
returns: bytes read from the port.
return type: bytes
'''
try:
data = self.file.read(size)
return data
# if(len(data) != 0):
# return data
# else: # notice 'receiver' thread to exit.
# raise Exception("Finish reading data file.")
except Exception as e:
print(e)
raise
def open(self):
try:
self.file = open(self.file_name, 'rb')
return True
except Exception as e:
print(e)
def close(self):
if self.file:
self.file.close()
self.file = None