-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_handler.py
77 lines (53 loc) · 1.61 KB
/
arduino_handler.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
#-*-coding:utf-8
'''
PUG-PE Arduino Tools
Por Marcel Pinheiro Caraciolo - [email protected]
Victor Case - [email protected]
Gabriel - [email protected]
Diego Liber - [email protected]
Arduino Connector Handler
'''
import serial
import time
import sys
PASS = 1
FAIL = 0
GREEN = '1'
RED = '0'
class Arduino(object):
def __init__(self):
self.arduino = None
if sys.platform == 'linux2':
for i in range(0,45):
try:
self.arduino = serial.Serial('/dev/ttyACM%d' %i, 9600)
break
except serial.serialutil.SerialException:
pass
elif sys.platform == 'darwin':
for i in range(0,500):
try:
self.arduino = serial.Serial('/dev/cu.usbmodem%d' %i, 9600)
break
except serial.serialutil.SerialException:
pass
else:
PORTA = 5
try:
self.arduino = serial.Serial('COM%d' % PORTA, 9600)
except serial.SerialException:
pass
if not self.arduino:
print 'ERRO: Arduino nao encontrado. Favor verificar a porta correta.'
exit()
def send_message(self,test_code):
'''
Send a message to Arduino via serial
'''
if test_code == PASS:
self.arduino.write(GREEN)
else:
self.arduino.write(RED)
if __name__ == '__main__':
handler = Arduino()
handler.send_message(PASS)