SerialNode.java
Go to the documentation of this file.
00001 // Software License Agreement (BSD License)
00002 //
00003 // Copyright (c) 2011, Willow Garage, Inc.
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions
00008 // are met:
00009 //
00010 //  * Redistributions of source code must retain the above copyright
00011 //    notice, this list of conditions and the following disclaimer.
00012 //  * Redistributions in binary form must reproduce the above
00013 //    copyright notice, this list of conditions and the following
00014 //    disclaimer in the documentation and/or other materials provided
00015 //    with the distribution.
00016 //  * Neither the name of Willow Garage, Inc. nor the names of its
00017 //    contributors may be used to endorse or promote products derived
00018 //    from this software without specific prior written permission.
00019 //
00020 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 // POSSIBILITY OF SUCH DAMAGE.
00032 // Author: Adam Stambler  <adasta@gmail.com>
00033 
00034 package org.ros.rosserial;
00035 import org.ros.rosserial.*;
00036 
00037 
00038 import java.util.*;
00039 import gnu.io.*;
00040 
00041 import org.ros.node.Node;
00042 import org.ros.node.DefaultNodeFactory;
00043 import org.ros.node.NodeConfiguration;
00044 import org.ros.node.NodeMain;
00045 import org.ros.node.topic.Publisher;
00046 import java.io.FileOutputStream;
00047 
00048 public class SerialNode implements NodeMain {
00049 
00050           private Node node;
00051 
00052 
00053         public static SerialPort createSerialPort(String portName){
00054                 Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
00055                 CommPortIdentifier portId = null;  // will be set if port found
00056                 
00057                 while (portIdentifiers.hasMoreElements())
00058                 {
00059                     CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
00060                     if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
00061                        pid.getName().equals(portName)) 
00062                     {
00063                         portId = pid;
00064                         break;
00065                     }
00066                 }
00067                 if(portId == null)
00068                 {
00069                     System.err.println("Could not find serial port " + portName);
00070                     System.exit(1);
00071                 }
00072                 
00073                 SerialPort port = null;
00074                 try {
00075                     port = (SerialPort) portId.open(
00076                         "rosserial_java", // Name of the application asking for the port 
00077                         10000           // Wait max. 10 sec. to acquire port
00078                     );
00079                 } catch(PortInUseException e) {
00080                     System.err.println("Port already in use: " + e);
00081                     System.exit(1);
00082                 }
00083         
00084                 return port;
00085         }
00086 
00087           @Override
00088           public void main(NodeConfiguration configuration) {
00089             try {
00090                   System.out.println("Starting RosSerial node");
00091                   DefaultNodeFactory nodeFactory = new DefaultNodeFactory();
00092               node = nodeFactory.newNode("rosserial_node", configuration);
00093 
00094                         String portName = "/dev/ttyUSB0";
00095                         SerialPort port= createSerialPort(portName);
00096                         
00097                 
00098                         port.setSerialPortParams(57600,    
00099                                         SerialPort.DATABITS_8,
00100                                         SerialPort.STOPBITS_1,
00101                                         SerialPort.PARITY_NONE);
00102                         
00103                         //clear the librxtx serial port.  The first request for topics
00104                         //is normally missed if this is not done.
00105                         for(int i=0; i<50; i++)port.getOutputStream().write(0);
00106                         Thread.sleep(1000);
00107                         for(int i=0; i<50; i++)port.getOutputStream().write(0);
00108 
00109 
00110                         ROSSerial rs = new ROSSerial(node, port.getInputStream(), port.getOutputStream());
00111                         System.out.println("Now running RosSerial Node connected to " + portName);
00112                         rs.run();       
00113                         
00114                 }catch (Exception e) {
00115               if (node != null) {
00116                         e.printStackTrace();
00117               } else {
00118                 e.printStackTrace();
00119               }
00120             }
00121           }
00122 
00123           @Override
00124           public void shutdown() {
00125             node.shutdown();
00126             node = null;
00127           }
00128 
00129         }


rosserial_java_node
Author(s): asher
autogenerated on Thu Nov 28 2013 11:51:42