BowlerKernel
SerialConnectionPanel.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.ui;
16 
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.util.List;
20 
21 import javax.swing.JButton;
22 import javax.swing.JComboBox;
23 import javax.swing.JLabel;
24 import javax.swing.JOptionPane;
25 import javax.swing.JTextField;
26 
27 import net.miginfocom.swing.MigLayout;
28 
29 import com.neuronrobotics.sdk.common.BowlerAbstractConnection;
30 import com.neuronrobotics.sdk.common.Log;
31 import com.neuronrobotics.sdk.common.MissingNativeLibraryException;
32 import com.neuronrobotics.sdk.serial.SerialConnection;
33 
34 // TODO: Auto-generated Javadoc
39 
41  private static final long serialVersionUID = 1L;
42 
44  private JTextField baudrateTxt = new JTextField(8);
45 
47  private JComboBox connectionCbo = null;
48 
50  private JButton refresh;
51 
53  private SerialConnection connection = null;
54 
61  super("Serial", ConnectionImageIconFactory.getIcon("images/usb-icon.png"),connectionDialog);
62 
63  baudrateTxt.setText("115200");
64 
65  connectionCbo = new JComboBox();
66  connectionCbo.setEditable(true);
67 
68  refresh = new JButton("Refresh");
69  refresh.addActionListener(new ActionListener() {
70 
71  public void actionPerformed(ActionEvent arg0) {
72  refresh();
73  }
74  });
75 
76  setLayout(new MigLayout("", // Layout Constraints
77  "[right][left]", // Column constraints with default align
78  "[center][center]" // Row constraints with default align
79  ));
80 
81  add(new JLabel("Connection:"), "cell 0 0");
82  add(connectionCbo);
83  add(refresh);
84 
85  add(new JLabel("Baudrate:"), "cell 0 1");
86  add(baudrateTxt, "cell 1 1");
87 
88  refresh();
89  }
90 
91 
92  /* (non-Javadoc)
93  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#getConnection()
94  */
96  try {
97  int baud = Integer.parseInt(baudrateTxt.getText());
98  if(baud < 0) {
99  throw new NumberFormatException();
100  }
101  String port =connectionCbo.getSelectedItem().toString();
102  connection = new SerialConnection(port, baud);
103  Log.info("Using port:"+port+"\n");
104  } catch(NumberFormatException e) {
105  JOptionPane.showMessageDialog(null, "Invalid baudrate given. Please review the list of valid baudrates.", "Invalid Baudrate", JOptionPane.ERROR_MESSAGE);
106  } catch(Exception e) {
107  } finally {
108  setVisible(false);
109  }
110  return connection;
111  }
112 
113 
114  /* (non-Javadoc)
115  * @see com.neuronrobotics.sdk.ui.AbstractConnectionPanel#refresh()
116  */
117  public void refresh() {
118  connectionCbo.removeAllItems();
119  String m = "NRSDK not installed properly, native library not found\n\n" +
120  "librxtxSerial.so in Linux\n" +
121  "librxtxSerial.jnilib in OSX\n" +
122  "rxtxSerial.dll in Windows\n\n"+
123  "This must be in your JVM or system library path. See:\n"+
124  "http://neuronrobotics.com/wiki/Installing_The_Native_Serial_Library";
125  try {
126  List<String> prts= SerialConnection.getAvailableSerialPorts();
127  for(int i=0;i<prts.size();i++) {
128  String s = prts.get(i);
129  if(s.contains("DyIO")||s.contains("Bootloader"))
130  connectionCbo.addItem(prts.remove(i));
131  }
132  for(String s:prts){
133  if(!(s.contains("ttyS") || s.equals("COM1") || s.equals("COM2") || s.contains("ttyACM")))
134  connectionCbo.addItem(s);
135  else{
136  // TODO maybe add the others if you can change the color?
137  }
138  }
139  connectionCbo.addItem(null);
140  for(String s:prts){
141  if((s.contains("ttyS") || s.equals("COM1") || s.equals("COM2") || s.contains("ttyACM")))
142  connectionCbo.addItem(s);
143  }
144 
145  } catch(MissingNativeLibraryException e) {
146  JOptionPane.showMessageDialog(this, m,"NRSDK not installed properly", JOptionPane.ERROR_MESSAGE);
147  throw new MissingNativeLibraryException(m);
148  }catch (Exception e){
149  e.printStackTrace();
150  JOptionPane.showMessageDialog(this, m,"NRSDK not installed properly", JOptionPane.ERROR_MESSAGE);
151  throw new MissingNativeLibraryException(m);
152  }catch (Error e){
153  e.printStackTrace();
154  JOptionPane.showMessageDialog(this, m,"NRSDK not installed properly", JOptionPane.ERROR_MESSAGE);
155  throw new MissingNativeLibraryException(m);
156  }
157  getConnectionDialog().pack();
158  }
159 }
static void info(String message)
Definition: Log.java:110
SerialConnectionPanel(ConnectionDialog connectionDialog)