BowlerKernel
SerialConnection.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.serial;
16 
17 import gnu.io.NRSerialPort;
18 
19 import java.io.DataInputStream;
20 import java.io.DataOutputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
25 import com.neuronrobotics.sdk.common.Log;
26 import com.neuronrobotics.sdk.common.MACAddress;
27 import com.neuronrobotics.sdk.common.MissingNativeLibraryException;
28 import com.neuronrobotics.sdk.genericdevice.GenericDevice;
29 import com.neuronrobotics.sdk.util.ThreadUtil;
30 
31 // TODO: Auto-generated Javadoc
48 
50  private int sleepTime = 1000;
51 
53  private int pollTimeoutTime = 5;
54 
55 
57  private String port=null;
58 
60  private int baud = 115200;
61 
63  private NRSerialPort serial;
64 
72  public SerialConnection() {
74  }
75 
83  public SerialConnection(String port) {
84  setPort(port);
86  }
87 
94  public SerialConnection(String port, int baud) {
95  setPort(port);
96  setBaud(baud);
98  }
99 
105  public void setPort(String port) {
106  this.port = port;
107  }
108 
114  public String getPort() {
115  return port;
116  }
117 
142  public void setBaud(int baud) {
143  this.baud = baud;
144  }
145 
146  /* (non-Javadoc)
147  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#connect()
148  */
149  @Override
150  public boolean connect() {
151  if(isConnected()) {
152  Log.error(port + " is already connected.");
153  return true;
154  }
155 
156  try
157  {
158  if(serial != null)
159  serial.disconnect();
160  serial = new NRSerialPort(getPort(), baud);
161  serial.connect();
162  setDataIns(new DataInputStream(serial.getInputStream()));
163  setDataOuts(new DataOutputStream(serial.getOutputStream()));
164  setConnected(true);
165  }catch(UnsatisfiedLinkError e){
166  throw new MissingNativeLibraryException(e.getMessage());
167  }catch (Exception e) {
168  Log.error("Failed to connect on port: "+port+" exception: ");
169  e.printStackTrace();
170  setConnected(false);
171  }
172 
173  if(isConnected()) {
174  serial.notifyOnDataAvailable(true);
175  }
176  return isConnected();
177  }
178 
179 
180 
181  /* (non-Javadoc)
182  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#disconnect()
183  */
184  @Override
185  public void disconnect() {
186  if(isConnected())
187  //new RuntimeException().printStackTrace();
188  Log.warning("Disconnecting Serial Connection");
189  try{
190  try{
191  serial.disconnect();
192  }catch(Exception e){
193  //e.printStackTrace();
194  //throw new RuntimeException(e);
195  }
196  serial = null;
197  setConnected(false);
198  } catch(UnsatisfiedLinkError e) {
199  throw new MissingNativeLibraryException(e.getMessage());
200  }
201  }
202 
203  /* (non-Javadoc)
204  * @see java.lang.Object#toString()
205  */
206  @Override
207  public String toString() {
208  return port;
209  }
210 
218 
219  List <String> ports = SerialConnection.getAvailableSerialPorts();
220  //Start by searching through all available serial connections for DyIOs connected to the system
221  for(String s: ports){
222  System.out.println("Searching "+s);
223  }
224  for(String s: ports){
225  try{
226  SerialConnection connection = new SerialConnection(s);
227  GenericDevice d = new GenericDevice(connection);
228  d.connect();
229  System.out.println("Pinging port: "+connection+" ");
230  if(d.ping()){
231  String addr = d.getAddress().toString();
232  if(addr.equalsIgnoreCase(mac.toString())){
233  connection.disconnect();
234  System.out.println("Device FOUND on port: "+connection+" "+addr);
235  return connection;
236  }
237  System.err.println("Device not on port: "+connection+" "+addr);
238  }
239  connection.disconnect();
240  }catch(Exception EX){
241  EX.printStackTrace();
242  System.err.println("Serial port "+s+" is not a DyIO");
243  }
244 
245  }
246 
247  return null;
248  }
249 
255  public static List<String> getAvailableSerialPorts() {
256  ArrayList<String> back = new ArrayList<String>();
257  for(String s:NRSerialPort.getAvailableSerialPorts()){
258  back.add(s);
259  }
260  return back;
261  }
262 
263 // /* (non-Javadoc)
264 // * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#reconnect()
265 // */
266 // @Override
267 // public boolean reconnect() {
268 // Log.warning("Reconnecting in serial");
269 // disconnect();
270 // ThreadUtil.wait(sleepTime);
271 // return connect();
272 // }
273 
274  /* (non-Javadoc)
275  * @see com.neuronrobotics.sdk.common.BowlerAbstractConnection#waitingForConnection()
276  */
277  @Override
278  public boolean waitingForConnection() {
279  // TODO Auto-generated method stub
280  return false;
281  }
282 
283 }
static void error(String message)
Definition: Log.java:92
static void warning(String message)
Definition: Log.java:101
static SerialConnection getConnectionByMacAddress(MACAddress mac)