我有一些想法,但我不知道该怎么做。 如果我可以正确地将事件监听器添加到类中,我可以使这个工作。现在结果是空的。这是我工作的射频RX / TX应用程序。我只需要收到的代码显示在我的jTextArea(ta1)上,它也是一个静态组件。
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class TestRead extends javax.swing.JFrame implements SerialPortEventListener {
static SerialPort serialPort;
public String max;
static class SerialPortReader implements SerialPortEventListener {
@Override
public void serialEvent(SerialPortEvent event) {
try {
//System.out.print(serialPort.readString());
max = serialPort.readString();
ta1.setText(serialPort.readString());
} catch (Exception e) {
System.out.println(e);
}
}
}
static String getSE(String x) {
System.out.println("Result: "+max);
return x;
}
public TestRead() {
initComponents();
}
//some netbeans generate code goes here..
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
getSE("");
}
public static void main(String args[]) {
serialPort = new SerialPort("COM21");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
serialPort.writeBytes("\002v0\003".getBytes());//Write data to port
serialPort.closePort();//Close serial port
} catch (SerialPortException ex) {
System.out.println("Serial set Verbose: " + ex);
}
try {
serialPort.openPort();//Open port
serialPort.setParams(9600, 8, 1, 0);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
//Add SerialPortEventListener
} catch (SerialPortException ex) {
System.out.println("Serial Port SetMask: " + ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TestRead().setVisible(true);
}
});
}
当我尝试将serialPort的值传递给max时,它为空。有人可以帮忙吗?
我正在使用JSSC.jar