-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArduino.py
More file actions
43 lines (38 loc) · 1.14 KB
/
Arduino.py
File metadata and controls
43 lines (38 loc) · 1.14 KB
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
import sys
import glob
import serial
from time import sleep
class Arduino(object):
port = None
connection = None
def connect(self):
if not self.port is None and not self.connection is None:
return True
self.port = None
self.connection = None
try:
data = self.get_ports()
# print data
self.port = data[0]
self.connection = serial.Serial(self.port, 115200)
print self.connection.name
return True
except Exception:
return False
def get_ports(self):
ports = glob.glob('/dev/tty.*')
result = []
for port in ports:
if 'tty.usbmodem' in port:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def sendPacket(self, packet):
if self.connection is None:
self.connect()
if self.connection is not None:
self.connection.write(packet)