2424Classes:
2525 Arduino(...):
2626 Manages serial communication with an Arduino(-like) device.
27-
27+
2828 Methods:
2929 close():
3030 Close the serial connection.
4141 query_ascii_values(...)
4242 Write a string to the serial port and return the reply, parsed
4343 into a list of floats.
44-
44+
4545 Important member:
4646 ser: serial.Serial instance belonging to the Arduino
4747"""
4848__author__ = "Dennis van Gils"
4949__authoremail__ = "vangils.dennis@gmail.com"
5050__url__ = "https://github.com/Dennis-van-Gils/DvG_dev_Arduino"
51- __date__ = "23-08 -2018"
52- __version__ = "1.0.0 "
51+ __date__ = "15-09 -2018"
52+ __version__ = "1.0.1 "
5353
5454import sys
5555import serial
@@ -65,10 +65,10 @@ def __init__(self, name="Ard", baudrate=9600,
6565 # Reference to the serial.Serial device instance when a connection has
6666 # been established
6767 self .ser = None
68-
68+
6969 # Given name for display and debugging purposes
7070 self .name = name
71-
71+
7272 # Response of self.query('id?') received from the Arduino.
7373 # Note that the Arduino should be programmed to respond to such a
7474 # query if you want the following functionality:
@@ -79,7 +79,7 @@ def __init__(self, name="Ard", baudrate=9600,
7979 # or
8080 # self.auto_connect(path_config, match_identity='your identity here')
8181 self .identity = None
82-
82+
8383 # Serial communication settings
8484 self .baudrate = baudrate
8585 self .read_timeout = read_timeout
@@ -89,7 +89,7 @@ def __init__(self, name="Ard", baudrate=9600,
8989
9090 # Is the connection to the device alive?
9191 self .is_alive = False
92-
92+
9393 # Placeholder for keeping track of future automated data acquisition as
9494 # used by e.g. DvG_dev_Arduino__pyqt_lib.py
9595 self .update_counter = 0
@@ -111,10 +111,10 @@ def close(self):
111111 except : pass
112112 try : self .ser .cancel_write ()
113113 except : pass
114-
114+
115115 try : self .ser .close ()
116116 except : pass
117-
117+
118118 self .is_alive = False
119119
120120 # --------------------------------------------------------------------------
@@ -124,7 +124,7 @@ def close(self):
124124 def connect_at_port (self , port_str , match_identity = None ,
125125 print_trying_message = True ):
126126 """Open the port at address 'port_str' and try to establish a
127- connection. Subsequently, an identity query is send to the device.
127+ connection. Subsequently, an identity query is send to the device.
128128 Optionally, if a 'match_identity' string is passed, only connections to
129129 devices with a matching identity response are accepted.
130130
@@ -233,7 +233,7 @@ def scan_ports(self, match_identity=None):
233233 continue
234234
235235 # Scanned over all the ports without finding a match
236- print ("\n ERROR: Device not found" )
236+ print ("\n ERROR: Device not found\n " )
237237 return False
238238
239239 # --------------------------------------------------------------------------
@@ -303,7 +303,7 @@ def write(self, msg_str, timeout_warning_style=1):
303303 sys .exit (1 )
304304 else :
305305 success = True
306-
306+
307307 return success
308308
309309 # --------------------------------------------------------------------------
@@ -370,7 +370,7 @@ def query_ascii_values(self, msg_str="", separator='\t'):
370370 Expects a reply from the Arduino in the form of an ASCII string
371371 containing a list of numeric values. These values will be parsed into a
372372 list of floats and returned.
373-
373+
374374 Returns:
375375 success (bool):
376376 True if successful, False otherwise.
@@ -379,7 +379,7 @@ def query_ascii_values(self, msg_str="", separator='\t'):
379379 [None] if unsuccessful.
380380 """
381381 [success , ans_str ] = self .query (msg_str )
382-
382+
383383 if success and not (ans_str == '' ):
384384 try :
385385 ans_floats = list (map (float , ans_str .split (separator )))
@@ -391,9 +391,9 @@ def query_ascii_values(self, msg_str="", separator='\t'):
391391 sys .exit (1 )
392392 else :
393393 return [True , ans_floats ]
394-
394+
395395 return [False , []]
396-
396+
397397# ------------------------------------------------------------------------------
398398# read_port_config_file
399399# ------------------------------------------------------------------------------
@@ -434,7 +434,7 @@ def write_port_config_file(filepath, port_str):
434434 Path to the config file, e.g. Path("config/port.txt")
435435 port_str (string):
436436 Serial port string to save to file.
437-
437+
438438 Returns: True when successful, False otherwise.
439439 """
440440 if isinstance (filepath , Path ):
@@ -461,16 +461,16 @@ def write_port_config_file(filepath, port_str):
461461
462462if __name__ == '__main__' :
463463 ard = Arduino (name = "Ard" , baudrate = 9600 )
464-
464+
465465 ard .auto_connect (path_config = Path ("last_used_port.txt" ),
466466 match_identity = "My Arduino" )
467467 #ard.scan_ports(match_identity="My Arduino")
468468 #ard.scan_ports()
469-
469+
470470 if not (ard .is_alive ):
471471 sys .exit (1 )
472-
472+
473473 print (ard .query ("?" ))
474474 print (ard .query_ascii_values ("?" , '\t ' ))
475-
475+
476476 ard .close ()
0 commit comments