gpt4 book ai didi

java - 实现线程接口(interface)

转载 作者:行者123 更新时间:2023-12-02 07:56:46 25 4
gpt4 key购买 nike

我正在尝试使用 SerialPortListener 接口(interface)将两个类“连接”在一起,MyJFrame 和 MySerialPort,但我不知道如何做到这一点。我这样做的原因是因为昨天我在将数据从串行端口缓冲区分配给 MySerialPort 类中的全局字符串 (finalString) 时遇到问题。该字符串应返回到 MyJFrame,其中标签会显示它。问题是我的标签会在任何内容之前显示 FinalString被分配给finalString,因为类在不同的线程中运行。我在论坛上发布了这个问题,并收到了使用接口(interface)连接他们的线程的建议,我据此修改了代码。我在哪里使用关键字implements SerialPortListener以及如何获取该值?

SerialPortListener接口(interface)代码

public interface SerialPortListener {

void stringReveivedFromSerialPort(String s);

}

MyJFrame 类代码

public class MyJFrame extends JFrame{

public MySerialPorts msp = new MySerialPorts();


public MyJFrame(){

initComponents();//draws GUI components
initSerialPorts();//initializes serial ports

}

private void initSerialPorts(){

msp.getPortName();//gets serial port's name (in this example COM1)
msp.openPort();//opens the communication for port COM1

}

private String firmwareVersion(){
//This is the method I call when I want to get the Firmware Version

msp.getFirmwareVersion();//sends command to receive reply from serial device
lblFirmwareVersion.setText();//label that prints the firmware version String

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new MainJFrame().setVisible(true);
}
});
}

private JLabel lblFirmwareVersion;

}

MySerialPort 类代码

public class MySerialPort {

//this method is using the jSSC API (java simple serial connector)
//http://code.google.com/p/java-simple-serial-connector/

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 portName = "";
// private String finalString = "";
// private StringBuilder sbBuffer = new StringBuilder();

private List<SerialPortListener> portListeners = new ArrayList<SerialPortListenerInterface>();

public void addMyPortListener(SerialPortListener listener) {
portListeners.add(listener);
}

public void removeMyPortListener(SerialPortListener listener) {
portListeners.remove(listener);
}

public void getFirmwareVersion() {
sendPortCommand("<VersFV1A2>\r\n");
}

// public String returnFinalString() {
// return finalString;
// }

public void getPortName() {
String[] ports = SerialPortList.getPortNames();
portName = ports[0];
}

public void openPort() {

serialPort = new SerialPort(portName);

try {

if (serialPort.openPort()) {

if (serialPort.setParams(iBaudRate, iDataBits, iStopBits, iParity)) {

serialPort.addEventListener(new Reader(), SerialPort.MASK_RXCHAR
| SerialPort.MASK_RXFLAG
| SerialPort.MASK_CTS
| SerialPort.MASK_DSR
| SerialPort.MASK_RLSD);

} else {
//Error Message - Can't set selected port parameters!
serialPort.closePort();
}

} else {
//Error Message - Can't open port!
}
} catch (SerialPortException | HeadlessException ex) {
//Error Message - Can't open port! - Do nothing
}
}

private void sendPortCommand(String sSendPortCommand) {

if (sSendPortCommand.length() > 0) {

try {
serialPort.writeBytes(sSendPortCommand.getBytes());
} catch (Exception ex) {
//Error Message - Error occured while sending data!
}
}
}

private class Reader 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.invokeLater(new Runnable() {

@Override
public void run() {
for (SerialPortListenerInterface listener : portListeners) {
listener.stringReveivedFromSerialPort(sBuffer);
}
}
});

// The following is the code I had prior to suggestion of using invokeLater instead of invokeAndWait
//
// sbBuffer.setLength(0);
//
// SwingUtilities.invokeAndWait(
// new Runnable() {
//
// @Override
// public void run() {
// sbBuffer.append(sBuffer);
// }
// });
//
// finalString = new String(sbBuffer);

} catch (Exception ex) {
}

}
}
}
}
}

最佳答案

以下是您可以添加到 initSerialPorts() 方法中的一些代码,这些代码将打开一个对话框,显示从串行端口接收到的文本:

msp.addMyPortListener(new SerialPortListener() {
@Override
public void stringReveivedFromSerialPort(String s) {
JOptionPane.showMessageDialog(MyJFrame.this, s);
}
});

它创建一个匿名 SerialPortListener 实例,该实例显示一个包含收到的文本作为消息的对话框,并将其添加到您的 MySerialPort msp 实例中。

关于java - 实现线程接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com