-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial_port.py
77 lines (67 loc) · 2.3 KB
/
serial_port.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
from PySide2.QtCore import QObject , Signal
import serial
CLOSED = 2
NOT_OPEN = 1
UNKNOWN_ERROR = 9
class QPySerial(QObject):
errorOccurred = Signal(int)
def __init__(self,parent=None):
super(QPySerial,self).__init__(parent)
self.ser = None
def open(self, port, baudrate):
if self.ser != None:
self.ser.close()
self.ser = serial.Serial(port=port, baudrate=baudrate)
def close(self):
if self.ser != None:
self.ser.close()
self.ser = None
self.errorOccurred.emit(CLOSED)
def is_open(self):
if self.ser != None:
try:
if self.ser.readable() and self.ser.writable():
return True
except serial.serialutil.SerialException:
pass
self.errorOccurred.emit(UNKNOWN_ERROR)
self.ser = None
return False
def in_waiting(self):
if self.ser != None:
try:
return self.ser.in_waiting
except serial.serialutil.SerialException:
self.errorOccurred.emit(UNKNOWN_ERROR)
self.ser = None
return 0
def out_waiting(self):
if self.ser != None:
try:
return self.ser.out_waiting
except serial.serialutil.SerialException:
self.errorOccurred.emit(UNKNOWN_ERROR)
self.ser = None
return 0
def read(self, n):
if self.ser != None:
try:
return self.ser.read(n)
except serial.serialutil.SerialException:
self.errorOccurred.emit(UNKNOWN_ERROR)
self.ser = None
return bytes()
def write(self, data, timeout=None):
if self.ser != None:
try:
self.ser.write_timeout = None
if timeout != None:
self.ser.write_timeout = timeout
n = self.ser.write(data)
# if timeout != None:
# self.ser.write_timeout = None
return n
except serial.serialutil.SerialException:
self.errorOccurred.emit(UNKNOWN_ERROR)
self.ser = None
return 0