-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.py
67 lines (57 loc) · 2.2 KB
/
connection.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
import datetime
import threading
from threading import Thread
import serial
class Connection:
def __init__(self, name:str = "", address:str = "", socketio = None, isvirtual: bool=False):
self.name = name
self.address = address
self.isvirtual = isvirtual
self.socketio_reference = socketio
self.serialconnection = None
self.logfile = None
self.rxthread = None
self.rx_buffer = []
self._stopevent = threading.Event()
if isvirtual:
#This is a dummy connection instantiate a new file
self.logfile = open(name+'.log','w')
else:
#This is a real connection do the serial connection
self.connect_serial()
def connect_serial(self):
#instatntiate the socket object
self.serialconnection = serial.Serial(port=self.address, baudrate=1152000)
#TODO - Need to create a async serial read/flush system
self.rxthread = Thread(target=self.serial_rx_thread)
self.rxthread.start()
def send_data(self, data):
#send data through the socket object
if self.isvirtual:
#Write to the file
ts = datetime.datetime.now().timestamp()
self.logfile.write(ts, ' - ')
self.logfile.write(data, '\n')
else:
#Check if connection is open
if not self.serialconnection.isOpen():
print(self.serialconnection.isOpen())
print("Error - ", self.address, " - Connection is closed")
return False
#Send the serial data
#TODO - Check to see if the write is happening correctly with an arudino echo script
self.serialconnection.write(data)
def stop(self):
#Clean up the object
if self.isvirtual:
self.logfile.close()
else:
self._stopevent.set()
def serial_rx_thread(self):
if self._stopevent.is_set():
print("Stopping the RX thread")
self.serialconnection.close()
self.rxthread.join()
else:
buf = self.serialconnection.read(100)
self.socketio_reference.emit(self.name, buf)