serial_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #####################################################################
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2011, Willow Garage, Inc.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of Willow Garage, Inc. nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
35 
36 __author__ = "mferguson@willowgarage.com (Michael Ferguson)"
37 
38 import rospy
39 from rosserial_python import SerialClient, RosSerialServer
40 from serial import SerialException
41 from time import sleep
42 import multiprocessing
43 
44 import sys
45 
46 if __name__=="__main__":
47 
48  rospy.init_node("serial_node")
49  rospy.loginfo("ROS Serial Python Node")
50 
51  port_name = rospy.get_param('~port','/dev/ttyUSB0')
52  baud = int(rospy.get_param('~baud','57600'))
53 
54  # for systems where pyserial yields errors in the fcntl.ioctl(self.fd, TIOCMBIS, \
55  # TIOCM_DTR_str) line, which causes an IOError, when using simulated port
56  fix_pyserial_for_test = rospy.get_param('~fix_pyserial_for_test', False)
57 
58  # TODO: should these really be global?
59  tcp_portnum = int(rospy.get_param('/rosserial_embeddedlinux/tcp_port', '11411'))
60  fork_server = rospy.get_param('/rosserial_embeddedlinux/fork_server', False)
61 
62  # TODO: do we really want command line params in addition to parameter server params?
63  sys.argv = rospy.myargv(argv=sys.argv)
64  if len(sys.argv) >= 2 :
65  port_name = sys.argv[1]
66  if len(sys.argv) == 3 :
67  tcp_portnum = int(sys.argv[2])
68 
69  if port_name == "tcp" :
70  server = RosSerialServer(tcp_portnum, fork_server)
71  rospy.loginfo("Waiting for socket connections on port %d" % tcp_portnum)
72  try:
73  server.listen()
74  except KeyboardInterrupt:
75  rospy.loginfo("got keyboard interrupt")
76  finally:
77  rospy.loginfo("Shutting down")
78  for process in multiprocessing.active_children():
79  rospy.loginfo("Shutting down process %r", process)
80  process.terminate()
81  process.join()
82  rospy.loginfo("All done")
83 
84  else : # Use serial port
85  while not rospy.is_shutdown():
86  rospy.loginfo("Connecting to %s at %d baud" % (port_name,baud) )
87  try:
88  client = SerialClient(port_name, baud, fix_pyserial_for_test=fix_pyserial_for_test)
89  client.run()
90  except KeyboardInterrupt:
91  break
92  except SerialException:
93  sleep(1.0)
94  continue
95  except OSError:
96  sleep(1.0)
97  continue
98 


rosserial_python
Author(s): Michael Ferguson
autogenerated on Mon Jun 10 2019 14:53:29