-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unior.py
79 lines (70 loc) · 2.56 KB
/
Unior.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
# Unior.py
"""CLass Unior(), use for chating with PAK UNIOR module"""
import serial
import serial.tools.list_ports
from time import process_time
from struct import unpack
import keyboard
from ComConnect import ComConnect
STD_SPEED = 57600 # Скорость COM порта
COM_PORT = 'COM6'
class Unior(ComConnect):
"""Create connection with PAK UNIOR"""
def __init__(self, channel, com_port=COM_PORT, std_speed=STD_SPEED):
print('... | Unior | ...', end='')
self.channel = channel
super().__init__(com_port, std_speed)
def begin(self, com_port=COM_PORT):
"""Setup COM-port and connect to PAK UNIOR"""
self.com_port = com_port
if keyboard.is_pressed('q'):
self.piSerial.close()
print('CLOSING... | EXIT')
try:
if self.status == 2:
print(f'INITIALIZE... | unior_begin {self.channel}', end='')
self.begin_initialization()
if self.status == 0:
self.begin_connection(self.channel)
except (OSError, serial.SerialException,
serial.serialutil.SerialException):
try:
self.piSerial.close()
except (OSError, serial.SerialException):
self.status = 2
return "NO CONN"
self.status = 2
return "NO CONN"
return self.status
def read(self):
"""Read data from PAK UNIOR"""
try:
if self.piSerial.inWaiting() > 0:
self.piSerial.flushInput()
self.piSerial.write((str(self.channel) + '\r\n\0').encode())
beg_time = process_time()
while self.piSerial.inWaiting() < 4:
if process_time() - beg_time > 1:
return 0
try:
tmp = unpack('<f', self.piSerial.read(4))[0]
if tmp != tmp:
return 0
else:
return float("%.2f" % tmp)
except ValueError:
return 0
except (OSError, serial.SerialException,
serial.serialutil.SerialException):
try:
self.piSerial.close()
except (OSError, serial.SerialException):
self.status = 2
return "NO CONN"
self.status = 2
return "NO CONN"
def set_status(self, sts):
"""Set status for reconnection or read data"""
self.status = sts
if self.status == 2:
self.__init__(self.channel, self.com_port, self. std_speed)