1212else :
1313 import glob
1414
15+ libraryVersion = 'V0.4'
1516
1617log = logging .getLogger (__name__ )
1718
@@ -62,6 +63,7 @@ def find_port(baud, timeout):
6263 ports = enumerate_serial_ports ()
6364 elif platform .system () == 'Darwin' :
6465 ports = [i [0 ] for i in list_ports .comports ()]
66+ ports = ports [::- 1 ]
6567 else :
6668 ports = glob .glob ("/dev/ttyUSB*" ) + glob .glob ("/dev/ttyACM*" )
6769 for p in ports :
@@ -71,31 +73,40 @@ def find_port(baud, timeout):
7173 except (serial .serialutil .SerialException , OSError ) as e :
7274 log .debug (str (e ))
7375 continue
74- time .sleep (2 )
76+
77+ sr .readline () # wait for board to start up again
78+
7579 version = get_version (sr )
76- if version != 'version' :
77- log .debug ('Bad version {0}. This is not a Shrimp/Arduino!' .format (
78- version ))
79- sr .close ()
80- continue
80+
81+ if version != libraryVersion :
82+ if version [0 ] == 'V' or version == "version" :
83+ print ("You need to update the version of the Arduino-Python3 " ,
84+ "library running on your Arduino. " ,
85+ "Flash the prototype sketch again." )
86+ return sr
87+ else :
88+ log .debug ('Bad version {0}. This is not a Shrimp/Arduino!' .format (
89+ version ))
90+ sr .close ()
91+ continue
8192 log .info ('Using port {0}.' .format (p ))
8293 if sr :
8394 return sr
8495 return None
8596
86-
8797def get_version (sr ):
8898 cmd_str = build_cmd_str ("version" )
8999 try :
90100 sr .write (str .encode (cmd_str ))
91101 sr .flush ()
92102 except Exception :
93103 return None
94- return sr .readline ().replace ("\r \n " , "" )
104+ return sr .readline ().decode ( "utf-8" ). replace ("\r \n " , "" )
95105
96106
97107class Arduino (object ):
98108
109+
99110 def __init__ (self , baud = 115200 , port = None , timeout = 2 , sr = None ):
100111 """
101112 Initializes serial communication with Arduino if no connection is
@@ -108,7 +119,14 @@ def __init__(self, baud=115200, port=None, timeout=2, sr=None):
108119 raise ValueError ("Could not find port." )
109120 else :
110121 sr = serial .Serial (port , baud , timeout = timeout )
111- sr .readline ()
122+ sr .readline () # wait til board has rebooted and is connected
123+
124+ # check version
125+ if get_version (sr ) != libraryVersion :
126+ print ("You need to update the version of the Arduino-Python3 " ,
127+ "library running on your Arduino. " ,
128+ "Flash the prototype sketch again." )
129+
112130 sr .flush ()
113131 self .sr = sr
114132 self .SoftwareSerial = SoftwareSerial (self )
@@ -572,7 +590,7 @@ def begin(self, p1, p2, baud):
572590 self .sr .flush ()
573591 except :
574592 pass
575- response = self .sr .readline ().replace ("\r \n " , "" )
593+ response = self .sr .readline ().decode ( "utf-8" ). replace ("\r \n " , "" )
576594 if response == "ss OK" :
577595 self .connected = True
578596 return True
@@ -592,7 +610,7 @@ def write(self, data):
592610 self .sr .flush ()
593611 except :
594612 pass
595- response = self .sr .readline ().replace ("\r \n " , "" )
613+ response = self .sr .readline ().decode ( "utf-8" ). replace ("\r \n " , "" )
596614 if response == "ss OK" :
597615 return True
598616 else :
@@ -607,7 +625,7 @@ def read(self):
607625 cmd_str = build_cmd_str ("sr" )
608626 self .sr .write (str .encode (cmd_str ))
609627 self .sr .flush ()
610- response = self .sr .readline ().replace ("\r \n " , "" )
628+ response = self .sr .readline ().decode ( "utf-8" ). replace ("\r \n " , "" )
611629 if response :
612630 return response
613631 else :
@@ -632,7 +650,7 @@ def size(self):
632650 try :
633651 self .sr .write (str .encode (cmd_str ))
634652 self .sr .flush ()
635- response = self .sr .readline ().replace ("\r \n " , "" )
653+ response = self .sr .readline ().decode ( "utf-8" ). replace ("\r \n " , "" )
636654 return int (response )
637655 except :
638656 return 0
@@ -664,7 +682,7 @@ def read(self, adrress):
664682 try :
665683 self .sr .write (str .encode (cmd_str ))
666684 self .sr .flush ()
667- response = self .sr .readline ().replace ("\r \n " , "" )
685+ response = self .sr .readline ().decode ( "utf-8" ). replace ("\r \n " , "" )
668686 if response :
669687 return int (response )
670688 except :
0 commit comments