如果Java程序应该连续地从串行通信接收数据,但现在程序出现延迟或延迟,该怎么办?
我正在使用Arduino通过Serialcomm连接到Java Netbeans。我的java程序应该持续接收数据,串行端口必须持续打开。我现在在程序上遇到了延迟或延迟。我想这可能是因为计时器的缘故,所以我试着把它取下来。但当我这样做时,串行端口不再打开 我应该怎么做来消除延迟或程序被减慢? 也许有另一种方法可以做到这一点,比如改变我的动作监听器,或者用另一种方法让它在不使用定时器的情况下连续读取串行端口 提前感谢您的回答如果Java程序应该连续地从串行通信接收数据,但现在程序出现延迟或延迟,该怎么办?,java,timer,arduino,serial-port,serial-communication,Java,Timer,Arduino,Serial Port,Serial Communication,我正在使用Arduino通过Serialcomm连接到Java Netbeans。我的java程序应该持续接收数据,串行端口必须持续打开。我现在在程序上遇到了延迟或延迟。我想这可能是因为计时器的缘故,所以我试着把它取下来。但当我这样做时,串行端口不再打开 我应该怎么做来消除延迟或程序被减慢? 也许有另一种方法可以做到这一点,比如改变我的动作监听器,或者用另一种方法让它在不使用定时器的情况下连续读取串行端口 提前感谢您的回答 Timer timer ; ActionListener read
Timer timer ;
ActionListener readRF = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str;
SerialPort serialPort = new SerialPort("COM3");
//In the constructor pass the name of the port with which we work
try {
//Open port
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Thread.sleep(1000);//Very important !!!
//Read data from port
for (int i = 0; i < 30; i++) {
str = serialPort.readString();
if (str != null) {
System.out.println(str);
if(str.contains("15538849")){
queue1.setText(String.valueOf(q.Table1Peek()));
}else if(str.contains("15538850")){
q.Table1DeQueue();
queue1.setText(String.valueOf(q.Table1Peek()));
}
}
Thread.sleep(100);
}
//Closing the port
serialPort.closePort();
} catch (SerialPortException ex ) {
System.out.println(ex);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
};
timer = new Timer(1000,readRF);
timer.setInitialDelay(0);
timer.start();
定时器;
ActionListener readRF=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
字符串str;
SerialPort SerialPort=新的SerialPort(“COM3”);
//在构造函数中,传递我们使用的端口的名称
试一试{
//开放端口
serialPort.openPort();
serialPort.setParams(serialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
串行端口。奇偶校验(无);
线程。睡眠(1000);//非常重要!!!
//从端口读取数据
对于(int i=0;i<30;i++){
str=serialPort.readString();
如果(str!=null){
系统输出打印项次(str);
如果(str.contains(“15538849”)){
queue1.setText(String.valueOf(q.Table1Peek());
}否则如果(str.contains(“15538850”)){
q、 表1等式();
queue1.setText(String.valueOf(q.Table1Peek());
}
}
睡眠(100);
}
//关闭港口
serialPort.closePort();
}捕获(SerialPortException例外){
系统输出打印项次(ex);
}捕获(中断异常例外){
系统输出打印项次(ex);
}
}
};
定时器=新定时器(1000,readRF);
timer.setInitialDelay(0);
timer.start();