@@ -47,61 +47,65 @@ def build_cmd_str(cmd, args=None):
4747 return "@{cmd}%{args}$!" .format (cmd = cmd , args = args )
4848
4949
50+ def find_port (baud , timeout ):
51+ """
52+ Find the first port that is connected to an arduino with a compatible
53+ sketch installed.
54+ """
55+ if platform .system () == 'Windows' :
56+ ports = enumerate_serial_ports ()
57+ elif platform .system () == 'Darwin' :
58+ ports = [i [0 ] for i in list_ports .comports ()]
59+ else :
60+ ports = glob .glob ("/dev/ttyUSB*" )
61+ for p in ports :
62+ print 'Found ' , p
63+ version = None
64+ try :
65+ print 'Testing ' , p
66+ sr = self .serial_factory (p , self .baud , timeout = self .timeout )
67+ time .sleep (2 )
68+ version = get_version (sr )
69+ if version != 'version' :
70+ raise Exception ('This is not a Shrimp/Arduino!' )
71+ print p , 'passed'
72+ return sr
73+ except Exception , e :
74+ print "Exception: " , e
75+ pass
76+ return None
77+
78+
79+ def get_version (sr ):
80+ cmd_str = build_cmd_str ("version" )
81+ try :
82+ sr .write (cmd_str )
83+ sr .flush ()
84+ except Exception :
85+ return None
86+ return sr .readline ().replace ("\r \n " , "" )
87+
88+
5089class Arduino (object ):
51- def __init__ (self , baud = 9600 , port = None , timeout = 2 ):
90+
91+ def __init__ (self , baud = 9600 , port = None , timeout = 2 , sr = None ):
5292 """
53- Initializes serial communication with Arduino.
93+ Initializes serial communication with Arduino if no connection is given .
5494 Attempts to self-select COM port, if not specified.
5595 """
56- self . baud = baud
57- self . timeout = timeout
58- self . port = port
59- if not self . port :
60- self . findPort ()
61- else :
62- self . sr = serial . Serial ( self . port , self . baud ,
63- timeout = self . timeout )
96+ if not sr :
97+ if not port :
98+ sr = find_port ( baud , timeout )
99+ raise ValueError ( "Could not find port." )
100+ else :
101+ sr = serial . Serial ( port , baud , timeout = timeout )
102+ sr . flush ()
103+ self . sr = sr
64104 self .SoftwareSerial = SoftwareSerial (self )
65105 self .Servos = Servos (self )
66- self .sr .flush ()
67106
68107 def version (self ):
69- cmd_str = build_cmd_str ("version" )
70- try :
71- self .sr .write (cmd_str )
72- self .sr .flush ()
73- except :
74- pass
75- version = self .sr .readline ().replace ("\r \n " , "" )
76- return version
77-
78- def findPort (self ):
79- """
80- Sets port to the first Arduino found
81- in system's device list
82- """
83- if platform .system () == 'Windows' :
84- ports = enumerate_serial_ports ()
85- elif platform .system () == 'Darwin' :
86- ports = [i [0 ] for i in list_ports .comports ()]
87- else :
88- ports = glob .glob ("/dev/ttyUSB*" )
89- for p in ports :
90- print 'Found ' , p
91- version = None
92- try :
93- print 'Testing ' , p
94- self .sr = serial .Serial (p , self .baud , timeout = self .timeout )
95- time .sleep (2 )
96- version = self .version ()
97- if version != 'version' :
98- raise Exception ('This is not a Shrimp/Arduino!' )
99- self .port = p
100- print p , 'passed'
101- break
102- except Exception , e :
103- print "Exception: " , e
104- pass
108+ return get_version (self .sr )
105109
106110 def digitalWrite (self , pin , val ):
107111 """
0 commit comments