Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

a little cleanup and remove a line that would cause an exception #336

Merged
merged 1 commit into from
Aug 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions nodes/arduino_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,21 @@ def process_message(line):
rospy.logwarn(message)
return message

def connect_serial(serial_connection=None):

# Thanks to Lindo St Angel, this fixes the serial / arduino lockup issue!
def close_serial():
if serial_connection is not None:
serial_connection.close() # Forces DTS on reconnect to reset Arduino


def connect_serial():
timeout_s = 2 / serial_rate_hz # serial port timeout is 2x loop rate
baud_rate = rospy.get_param("~baud_rate", 115200)

# Initialize the serial connection
path = "/dev/serial/by-id"
port = None
close_serial()
while port is None:
try:
if not os.path.exists(path):
Expand Down Expand Up @@ -392,7 +400,6 @@ def connect_serial(serial_connection=None):
# Fix issue #328, sometimes serial_connection is None because of a
# serial port path / or error opening issue.
if serial_connection is None:
serial_connection.close() # Forces DTS on reconnect to reset Arduino
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since serial_connection is None here, this line would have thrown an Exception.

serial_connection = connect_serial()

try:
Expand Down Expand Up @@ -430,17 +437,14 @@ def connect_serial(serial_connection=None):
except serial.serialutil.SerialException as e1:
# This usually happens when the serial port gets closed or switches
rospy.logwarn(e1)
serial_connection.close()
serial_connection = connect_serial()
except serial.serialutil.SerialTimeoutException as e2:
# Exception that is raised on write timeouts
rospy.logwarn(e2)
serial_connection.close()
serial_connection = connect_serial()
except Exception as e3:
# Exception triggered by too many consecutive empty reads
rospy.logwarn(e3)
serial_connection.close()
serial_connection = connect_serial()

pairs_or_error = process_message(buf)
Expand All @@ -463,5 +467,5 @@ def connect_serial(serial_connection=None):
serial_rate.sleep()
# end of while loop

serial_connection.close()
close_serial()
# end of main