-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_writer.py
More file actions
42 lines (37 loc) · 1.23 KB
/
serial_writer.py
File metadata and controls
42 lines (37 loc) · 1.23 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
import serial
import time
from threading import Thread
class Serial_Writer(Thread):
def __init__(self, com_port, baud_rate):
Thread.__init__(self)
self.com_port = com_port
self.baud_rate = baud_rate
self.serial_port = serial.Serial(self.com_port, self.baud_rate, timeout=0)
self.last_output = ""
def run(self):
self.serial_port.open()
while True:
try:
self.write_serial_data()
except (Exception) as e:
print(e)
def write_serial_data(self, message):
if self.serial_port.isOpen():
output = message
if output != "self.last_output":
try:
out = output.encode('utf-8')
print(out)
self.serial_port.write(out)
self.last_output = output
self.serial_port.close()
except (Exception) as e:
print(e)
else:
self.serial_port.open()
print('Serial port not open : ' + str(self.com_port))
if __name__ == "__main__":
ser = Serial_Writer('com8', 4800)
while True:
ser.write_serial_data("test:1")
time.sleep(1)