-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSerialConfigPanel.java
More file actions
120 lines (98 loc) · 3.18 KB
/
SerialConfigPanel.java
File metadata and controls
120 lines (98 loc) · 3.18 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.farrellf.TelemetryGUI;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
/**
* A JPanel to allow the user to configure the RS232 link.
*
* @author Farrell Farahbod
* @version 1.0
*/
public class SerialConfigPanel extends JPanel implements MouseListener {
JLabel portLabel;
JComboBox portString;
JLabel baudRateLabel;
JComboBox baudRateSelection;
JButton applyButton;
String[] baudRates;
SerialPortListener rs232;
Thread rs232thread;
public SerialConfigPanel(Database db) {
rs232 = new SerialPortListener(db);
rs232thread = new Thread(rs232);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
portLabel = new JLabel("Serial Port:");
portLabel.setFont(new Font("Dialog", Font.BOLD, 12));
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(0, 0, 10, 5);
gbc.gridx = 0;
gbc.gridy = 0;
add(portLabel, gbc);
portString = new JComboBox(rs232.getSerialPorts());
portString.setEditable(true);
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(0, 0, 10, 5);
gbc.gridx = 1;
gbc.gridy = 0;
add(portString, gbc);
baudRateLabel = new JLabel("Baud Rate:");
baudRateLabel.setFont(new Font("Dialog", Font.BOLD, 12));
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(0, 30, 10, 5);
gbc.gridx = 2;
gbc.gridy = 0;
add(baudRateLabel, gbc);
baudRates = new String[] {"1382400", "921600", "460800", "230400", "115200", "57600", "38400", "19200", "9600"};
baudRateSelection = new JComboBox<String>(baudRates);
baudRateSelection.setSelectedIndex(1); // default to 921600 baud
baudRateSelection.setEditable(true);
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(0, 0, 10, 5);
gbc.gridx = 3;
gbc.gridy = 0;
add(baudRateSelection, gbc);
applyButton = new JButton("Apply");
applyButton.setFont(new Font("Dialog", Font.BOLD, 12));
applyButton.addMouseListener(this);
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(0, 30, 10, 5);
gbc.gridx = 4;
gbc.gridy = 0;
add(applyButton, gbc);
// auto connect if only one serial port exists
if(rs232.getSerialPorts().length == 1) {
if(rs232.establishConnection((String) rs232.getSerialPorts()[0], Integer.parseInt((String) baudRateSelection.getSelectedItem()))) {
rs232thread.start();
applyButton.setEnabled(false);
}
}
}
/**
* Attempt to create an RS232 link if a link is not currently active.
*/
@Override
public void mouseClicked(MouseEvent e) {
if(applyButton.isEnabled() == false)
return;
if(rs232.establishConnection((String) portString.getSelectedItem(), Integer.parseInt((String) baudRateSelection.getSelectedItem()))) {
rs232thread.start();
applyButton.setEnabled(false);
}
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}