-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.py
86 lines (72 loc) · 2.66 KB
/
Serial.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
#
# Serial COM Port receive message event handler
# may/2019, Thomas Sosio
# When a line of text arrives from the COM port terminated by a \n character, this module will pass the message to
# the function specified by the instantiator of this class.
#
import serial
import sys
import _thread
class SerialPort:
def __init__(self):
self.comportName = ""
self.baud = 0
self.timeout = None
self.ReceiveCallback = None
self.isopen = False
self.receivedMessage = None
self.serialport = serial.Serial()
def __del__(self):
try:
if self.serialport.is_open():
self.serialport.close()
except:
print("Destructor error closing COM port: ", sys.exc_info()[0] )
def RegisterReceiveCallback(self,aReceiveCallback):
self.ReceiveCallback = aReceiveCallback
try:
_thread.start_new_thread(self.SerialReadlineThread, ())
except:
print("Error starting Read thread: ", sys.exc_info()[0])
def SerialReadlineThread(self):
while True:
try:
if self.isopen:
self.receivedMessage = self.serialport.readline()
if self.receivedMessage != "":
self.ReceiveCallback(self.receivedMessage)
except:
print("Error reading COM port: ", sys.exc_info()[0])
def IsOpen(self):
return self.isopen
def Open(self,portname,baudrate):
if not self.isopen:
# serialPort = 'portname', baudrate, bytesize = 8, parity = 'N', stopbits = 1, timeout = None, xonxoff = 0, rtscts = 0)
self.serialport.port = portname
self.serialport.baudrate = baudrate
try:
self.serialport.open()
self.isopen = True
except:
print("Error opening COM port: ", sys.exc_info()[0])
def Close(self):
if self.isopen:
try:
self.serialport.close()
self.isopen = False
except:
print("Close error closing COM port: ", sys.exc_info()[0])
def Send(self,message):
if self.isopen:
try:
# Ensure that the end of the message has both \r and \n, not just one or the other
newmessage = message.strip()
newmessage += '>\r\n'
newmessage = '<' + newmessage
self.serialport.write(newmessage.encode('utf-8'))
except:
print("Error sending message: ", sys.exc_info()[0] )
else:
return True
else:
return False