Close the serial port when exiting arduino_node.py

This commit is contained in:
Patrick Goebel 2016-11-22 07:32:25 -08:00
parent 174878c7ab
commit ef83edc1dc

View File

@ -27,6 +27,7 @@ from ros_arduino_python.base_controller import BaseController
from geometry_msgs.msg import Twist from geometry_msgs.msg import Twist
import os, time import os, time
import thread import thread
from serial.serialutil import SerialException
class ArduinoROS(): class ArduinoROS():
def __init__(self): def __init__(self):
@ -201,6 +202,8 @@ class ArduinoROS():
return AnalogReadResponse(value) return AnalogReadResponse(value)
def shutdown(self): def shutdown(self):
rospy.loginfo("Shutting down Arduino Node...")
# Stop the robot # Stop the robot
try: try:
rospy.loginfo("Stopping the robot...") rospy.loginfo("Stopping the robot...")
@ -208,7 +211,19 @@ class ArduinoROS():
rospy.sleep(2) rospy.sleep(2)
except: except:
pass pass
rospy.loginfo("Shutting down Arduino Node...")
# Close the serial port
try:
self.controller.close()
except:
pass
finally:
rospy.loginfo("Serial port closed.")
os._exit(0)
if __name__ == '__main__': if __name__ == '__main__':
try:
myArduino = ArduinoROS() myArduino = ArduinoROS()
except SerialException:
rospy.logerr("Serial exception trying to open port.")
os._exit(0)