繁体   English   中英

从 JcomboBox 获取参数

[英]Getting parameters from a JcomboBox

以下是我对 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"  };

}

因此,基本上,在您的openComportButtonActionPerformed方法中,您需要询问comPortcomboBoxbaudRatecomboBox的选定值

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方法以支持波特率值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM