-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.py
More file actions
46 lines (34 loc) · 1.5 KB
/
Copy pathSerial.py
File metadata and controls
46 lines (34 loc) · 1.5 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
41
42
43
44
45
46
import serial
class SerialPort:
def __init__(self, config_serial : dict):
'''`Класс для работы с последовательным портом'''
self.check_connect = False
self.logi = config_serial['logger']
self.serial_port = serial.Serial(
port=config_serial['port'],
baudrate=config_serial['bitrate'],
timeout=config_serial['timeout']
)
self.check_cor = False
self.logi.info(f'Serial port init: {config_serial}')
def receiver_data(self):
#прием информации с аппарата
data = None
try:
while data == None or data == b'':
data = str(self.serial_port.readline())[2:-5]
self.logi.debug(f'Receiver data: {data}')
return data
except:
self.logi.warning('Error converting data')
return None
def send_data(self, data : list):
#отправка массива на аппарат
try:
data = (f'{str(data)[1:-1]}\n').replace(', ', ' ').encode()
self.serial_port.write(data)
self.logi.debug(f'Send data: {data}')
except:
self.logi.warning('Error send data')
def close(self):
self.serial_port.close()