-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
SerialTransportLayerPanel.java
64 lines (54 loc) · 2.24 KB
/
SerialTransportLayerPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.marginallyclever.communications.serial;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import com.marginallyclever.communications.NetworkConnection;
import com.marginallyclever.communications.TransportLayerPanel;
public class SerialTransportLayerPanel extends TransportLayerPanel {
/**
*
*/
private static final long serialVersionUID = -5048852192781164326L;
private SerialTransportLayer layer;
private JComboBox<String> connectionPort;
private JComboBox<String> connectionBaud;
private String lastConnectionPort="COM4";
private int lastConnectionBaud=57600;
public static final int [] COMMON_BAUD_RATES = {300,1200,2400,4800,9600,19200,38400,57600,74880,115200,230400,250000,500000,1000000,2000000};
public SerialTransportLayerPanel(SerialTransportLayer serialTransportLayer) {
this.layer = serialTransportLayer;
this.setBorder(new EmptyBorder(5,5,5,5));
this.setLayout(new GridLayout(0, 1));
this.add(new JLabel("Port",JLabel.LEADING));
add(connectionPort = new JComboBox<String>());
this.add(new JLabel("Baud",JLabel.LEADING));
add(connectionBaud = new JComboBox<String>());
// fill in the ports
String [] portsDetected = layer.listConnections();
int i;
for(i=0;i<portsDetected.length;++i) {
connectionPort.addItem(portsDetected[i]);
// set the drop down to the last port used, if possible.
if(portsDetected[i].contentEquals(lastConnectionPort)) {
connectionPort.setSelectedIndex(i);
}
}
// fill in the baud rates
for(i=0;i<COMMON_BAUD_RATES.length;++i) {
connectionBaud.addItem(Integer.toString(COMMON_BAUD_RATES[i]));
// set the drop down to the last port used, if possible.
if(COMMON_BAUD_RATES[i] == lastConnectionBaud) {
connectionBaud.setSelectedIndex(i);
}
}
this.add(Box.createVerticalGlue());
}
@Override
public NetworkConnection openConnection() {
lastConnectionPort = connectionPort.getItemAt(connectionPort.getSelectedIndex());
lastConnectionBaud = Integer.parseInt(connectionBaud.getItemAt(connectionBaud.getSelectedIndex()));
return layer.openConnection(lastConnectionPort + "@" + lastConnectionBaud);
}
}