-
Notifications
You must be signed in to change notification settings - Fork 1
/
com_module.py
68 lines (54 loc) · 1.45 KB
/
com_module.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
'''
This file is a collection of functions used
to provide communication functionality.
This includes methods to establish the connection and send data
'''
import serial
import sys
def initSerialConnection(portNo, baudRate):
'''
This functions initializes the serial connection
Parameters
----------
portNo : string
Provides the local port information for the sending device
baudRate : int
Defines the serial baudRate
Returns
-------
ser : string
Connection information
'''
try:
ser = serial.Serial(portNo, baudRate, timeout=100)
print("Device Connected")
return ser
except:
print("Not connected")
# changes exit code and exits
exit_code = 0
sys.exit(exit_code)
def sendData(se, data, digits):
'''
This function formats and sends the data via serial connection
Parameters
----------
se : string
Provides the local port information for the sending device
data : string
Contains the data which will be sent
digits : int
Determines transfer length
'''
myString = "$"
for d in data:
myString += str(d).zfill(digits)
try:
se.write(myString.encode())
print(myString)
except:
print("Data Transmission failed!")
return myString
if __name__ == "__main__":
# TODO: set up the port right
ser = initSerialConnection("/dev/ttyACM1", 115200)