以下是我对SSCCE的最佳尝试。您需要RXTX库才能使用。我的目标是根据组合框中的选择设置串口参数。请有人指出我正确的方向吗?
这是为了开启Comport:
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.OutputStream;
public class TwoWaySerialComm {
SerialPort serialPort;
public TwoWaySerialComm() {
super();
}
public synchronized void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(),
2000);
if (commPort instanceof SerialPort) {
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
} else {
System.out
.println("Error: Only serial ports are handled by this.");
}
}
}
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
try {
int c = 0;
while ((c = System.in.read()) > -1) {
this.out.write(c);
System.out.println(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这就是GIU:
import gnu.io.CommPortIdentifier;
import java.awt.EventQueue;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComboBox;
import java.awt.Frame;
import java.awt.Rectangle;
public class SSCCE extends JFrame {
public TwoWaySerialComm twoWaySerCom;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SSCCE frame = new SSCCE();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SSCCE() {
initComponents();
twoWaySerCom = new TwoWaySerialComm();
}
private void initComponents(){
setBounds(new Rectangle(0, 0, 550, 250));
setExtendedState(Frame.MAXIMIZED_HORIZ);
setType(Type.POPUP);
setAlwaysOnTop(true);
setTitle("Alarm Generator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 118, 0, 0, 0, 0, 0, 17, 23, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
getContentPane().setLayout(gridBagLayout);
comPortcomboBox = new JComboBox();
comPortcomboBox.setEnabled(true);
GridBagConstraints gbc_comPortcomboBox = new GridBagConstraints();
gbc_comPortcomboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comPortcomboBox.insets = new Insets(0, 0, 5, 5);
gbc_comPortcomboBox.gridx = 1;
gbc_comPortcomboBox.gridy = 2;
getContentPane().add(comPortcomboBox, gbc_comPortcomboBox);
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) portList.nextElement();
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
comPortcomboBox.addItem(cpi.getName());
}
}
comPortcomboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comPortcomboBoxActionPerformed(evt);
}
public void comPortcomboBoxActionPerformed(java.awt.event.ActionEvent evt) {
comPortcomboBox.getSelectedItem();
comPortcomboBox.setSelectedItem(portName);
}
});
JComboBox baudRateComboBox = new JComboBox(baudRates);
baudRateComboBox.setSelectedIndex(2);
GridBagConstraints gbc_baudRateComboBox = new GridBagConstraints();
gbc_baudRateComboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_baudRateComboBox.insets = new Insets(0, 0, 5, 5);
gbc_baudRateComboBox.gridx = 1;
gbc_baudRateComboBox.gridy = 3;
getContentPane().add(baudRateComboBox, gbc_baudRateComboBox);
GridBagConstraints gbc_alarm2Button = new GridBagConstraints();
gbc_alarm2Button.insets = new Insets(0, 0, 5, 0);
gbc_alarm2Button.gridx = 9;
gbc_alarm2Button.gridy = 3;
openComportButton = new JButton("Open");
GridBagConstraints gbc_openComportButton = new GridBagConstraints();
gbc_openComportButton.anchor = GridBagConstraints.NORTH;
gbc_openComportButton.insets = new Insets(0, 0, 0, 5);
gbc_openComportButton.gridx = 0;
gbc_openComportButton.gridy = 7;
getContentPane().add(openComportButton, gbc_openComportButton);
openComportButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openComportButtonActionPerformed(evt);
}
public synchronized void openComportButtonActionPerformed(
java.awt.event.ActionEvent evt) {
try {
twoWaySerCom.connect(portName);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public javax.swing.JButton openComportButton;
public javax.swing.JComboBox comPortcomboBox;
public javax.swing.JComboBox baudRatecomboBox;
String portName ="COM4";
String[] baudRates = { "2400", "4800", "9600", "14400", "19200", "38400", "56000", "115200" };
}
答案 0 :(得分:1)
所以,基本上,在openComportButtonActionPerformed
方法中,您需要向comPortcomboBox
和baudRatecomboBox
询问所选的值
String port = (String)comPortcomboBox.getSelectedItem();
String rate = (String)baudRatecomboBox.getSelectedItem();
然后,您需要检查值是否有效...
if (port != null && rate != null) {
//...
}
然后您需要将这些值传递给twoWaySerCom
public void openComportButtonActionPerformed(java.awt.event.ActionEvent evt) {
String port = (String)comPortcomboBox.getSelectedItem();
String rate = (String)baudRatecomboBox.getSelectedItem();
if (port != null && rate != null) {
try {
twoWaySerCom.connect(portName, rate);
} catch (Exception e) {
e.printStackTrace();
}
}
}
但您需要修改connect
方法以支持波特率值