-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialPort.py
110 lines (88 loc) · 3 KB
/
SerialPort.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
__author__ = 'DeRaaf'
# TODO Clean up comments. Fix bugs. On going project!
import serial
class SerialPort(object):
def __init__(self,
serial_port,
baud_rate,
time_out,
serial_port_id):
self.serial_port = serial_port
self.baud_rate = baud_rate
self.time_out = time_out
self.serial_port_id = serial_port_id
self.serializer = serial.Serial(port=self.serial_port,
baudrate=self.baud_rate,
timeout=self.time_out,
writeTimeout=self.time_out)
def __str__(self):
return '\n\nSerial port : {0}\n' \
'Baud Rate : {1}\n' \
'Time Out : {2}' \
'Serial Port Id : {3}' \
'\n\n'.format(self.serial_port,
self.baud_rate,
self.time_out,
self.serial_port_id)
def __getattr__(self):
return 'Not found'.format()
def connect_serial_port(self):
return serial.Serial(port=self.serial_port,
baudrate=self.baud_rate,
timeout=self.time_out,
writeTimeout=self.time_out)
def open_serial_port(self):
"""
Opens the Serial port
:return:
"""
# Method to open the serial port. Not needed in normal use cases. push_data/pull_data do this automatic
return 'open_serial_port'
#self.serializer.open()
def close_serial_port(self):
"""
Closes the Serial port
:return:
"""
# Method to close the serial port. Not needed in normal use cases. push_data/pull_data do this automatic
return 'close_serial_port'
#self.serializer.close()
def push_data(self,
serial_tx):
"""
serial_tx -> The data you want to send to the Arduino as a quoted string (i.e 'Test message')
Most of the time msg is used from other functions
:param serial_tx:
:return:
"""
# Write serial data to receiver (Arduino)
#return 'push_data'
self.serializer.write(serial_tx)
def pull_data(self):
"""
Pulls data from the Arduino.
:return:
"""
# Read serial data to receiver (Arduino)
return 'pull_data'
#return self.serializer.readline()
def serial_flush(self):
"""
Flush serial buffers after file transfer
:return:
"""
return 'serial_flush'
#self.serializer.flush()
def serial_flush_in(self):
"""
Flush the incoming (rx) serial port
:return:
"""
return 'serial_flush_in'
#self.serializer.flushInput()
def serial_flush_out(self):
"""
Flush the outgoing (tx) serial port
:return:
"""
return 'serial_flush_out'