我的应用程序应该读取和写入串行端口。数据在PortReader类的EventListener中读取。我希望将此数据分配给全局String变量(private String sPortReaderString)以供进一步使用。全局字符串变量应该使用名为getPortReader()的方法返回其值,该方法只返回字符串sPortReaderString。在应用程序的JFrame中,我打开串口连接,发送一个命令,我自动从串口设备接收回复,然后在标签中显示该回复输出。问题是标签总是空白的,因为从getPortReader()返回的sPortReaderString没有分配给它。我非常确定在EvenListener中为sPortReaderString分配了一个值。问题似乎是在任何值有足够的时间分配给sPortReaderString之前调用JFrame中的方法getPortReader()。请查看我的输出和以下代码:
以下是我得到的输出:
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
}
}
以下是我的串行端口类的示例:
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) {
}
}
}
}
}
}
答案 0 :(得分:2)
对我来说似乎很合乎逻辑:
您在EDT中执行第一个事件处理方法,获取固件版本,然后获取端口读取器。获取固件版本会导致接收事件,在另一个线程中(因此与EDT中portsMethod()
的执行并行)。
事件处理代码调用SwingUtilities.invokeAndWait()
。因此,此调用等待第一个事件处理方法完成,然后将收到的字符串追加到sbPortReaderString
。因此,在portsMethod
完成后完成此追加。
串口提供基于事件的机制。我只是用它来将事件传播给EDT中的一个或几个听众:
// accessed only from the EDT
private List<MyPortListener> portListeners = new ArrayList<MyPortListener>();
public void addMyPortListener(MyPortListener listener) {
portListeners.add(listener);
}
public void removeMyPortListener(MyPortListener listener) {
portListeners.remove(listener);
}
...
@Override
public void serialEvent(SerialPortEvent spe) {
...
final String receivedString = ...;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
for (MyPortListener listener : portListeners) {
listener.stringReveivedFromSerialPort(receivedString);
}
}
});
}
旁注:您的代码很难理解,主要是因为您的变量和方法命名错误。