-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsr_comm.py
70 lines (54 loc) · 1.5 KB
/
sr_comm.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
import serial
import time
__author__ = "Rohit"
class SerialAPI(object):
def __init__(self):
self.port = '/dev/ttyACM0'
self.baud_rate = 9600
self.ser = None
def connect_serial(self):
"""
Initialize serial socket
"""
try:
self.ser = serial.Serial(self.port, self.baud_rate)
print "Serial link connected"
except Exception, e:
# print "Error (Serial): %s " % str(e)
print "Error: Serial connection not established. Try reconnecting the serial cable and/or restart the pi"
def close_sr_socket(self):
if (self.ser):
self.ser.close()
print "Closing serial socket"
def write_to_serial(self, msg):
"""
Write to arduino
"""
try:
self.ser.write(msg)
# print "Write to arduino: %s " % msg
except AttributeError:
print "Error in serial comm. No value to be written. Check connection!"
def read_from_serial(self):
"""
Read from arduino
Waits until data is received from arduino
"""
try:
received_data = self.ser.readline()
# print "Received from arduino: %s " % received_data
return received_data
except AttributeError:
print "Error in serial comm. No value received. Check connection!"
# if __name__ == "__main__":
# print "Running Main"
# sr = SerialAPI()
# sr.connect_serial()
# print "serial connection successful"
# send_msg = raw_input()
# print "Writing [%s] to arduino" % send_msg
# sr.write_to_serial(send_msg)
# print "read"
# print "data received from serial" % sr.read_from_serial
# print "closing sockets"
# sr.close_sr_socket()