我的应用程序应该读取和写入串行端口。数据在 PortReader 类的 EventListener 中读取。我希望将此数据分配给全局字符串变量(私有字符串 sPortReaderString)以供进一步使用。全局字符串变量应该使用名为 getPortReader() 的方法返回其值,该方法只返回字符串 sPortReaderString。在应用程序的 JFrame 中,我打开串行端口连接,发送一个命令,我会自动收到来自串行设备的回复,并在标签中显示该回复输出。问题是标签总是空白的,因为从 getPortReader() 返回的 sPortReaderString 没有分配给它。我非常确定 sPortReaderString 在 EvenListener 中被分配了一个值。问题似乎是在任何值有足够的时间分配给 sPortReaderString 之前调用 JFrame 中的方法 getPortReader()。请看一下我的 otuput 和下面的代码:
以下是我得到的输出:
sPortReaderString: PortReader
JFrame 中 portsMethod 的简短示例:
public class MyJFrame extends javax.swing.JFrame {
public MySerialPort msp = new MySerialPort();
public MainJFrame() {
portsMethod();
}
private void portsMethod() {
msp.getPortNames();//Gets the name of the port (COM1 in my case)
msp.openPort();//Opens COM1 port
msp.getFirmwareVersion();//Prompts for device firmware version by sending a string command
msp.getPortReader();//Reads the reply from device
}
}
以下是我的 Serial Port 类的示例:
public class MySerialPort {
private SerialPort serialPort;
private int iBaudRate = SerialPort.BAUDRATE_57600;
private int iDataBits = SerialPort.DATABITS_8;
private int iStopBits = SerialPort.STOPBITS_1;
private int iParity = SerialPort.PARITY_NONE;
private String sPortName;
private String sPortReaderString = "";
private StringBuilder sbPortReaderString = new StringBuilder();
public void getFirmwareVersion() {
sendPortCommand("<FirmVer>\r\n");
}
public void clearPortReader() {
sbPortReaderString.setLength(0);
}
public String getPortReader() {
System.out.print("sPortReaderString: " + sPortReaderString);
return sPortReaderString;
}
public void getPortNames() {
String[] sPorts = SerialPortList.getPortNames();
sPortName = sPorts[0];
}
public void openPort() {
serialPort = new SerialPort(sPortName);
try {
if (serialPort.openPort()) {
if (serialPort.setParams(iBaudRate, iDataBits, iStopBits, iParity)) {
serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR
| SerialPort.MASK_RXFLAG
| SerialPort.MASK_CTS
| SerialPort.MASK_DSR
| SerialPort.MASK_RLSD);
} else {
serialPort.closePort();
}
} else {}
} catch (SerialPortException | HeadlessException ex) {}
}
private void sendPortCommand(String sSendPortCommand) {
if (sSendPortCommand.length() > 0) {
try {
serialPort.writeBytes(sSendPortCommand.getBytes());
} catch (Exception ex) {}
}
}
private class PortReader implements SerialPortEventListener {
private String sBuffer = "";
@Override
public void serialEvent(SerialPortEvent spe) {
if (spe.isRXCHAR() || spe.isRXFLAG()) {
if (spe.getEventValue() > 0) {
try {
//Read chars from buffer
byte[] bBuffer = serialPort.readBytes(spe.getEventValue());
sBuffer = new String(bBuffer);
SwingUtilities.invokeAndWait(
new Runnable() {
@Override
public void run() {
sbPortReaderString.append(sBuffer);
}
});
sPortReaderString = new String(sbPortReaderString);
//if I print sPortReaderString in here it is not blank and has the correct value
System.out.print("PortReader");
} catch (SerialPortException | InterruptedException | InvocationTargetException ex) {
}
}
}
}
}
}