英文:
Connect with modbus device every 10 seconds
问题
我正在处理通过串口与系统连接的Modbus设备。我可以读/写设备寄存器上的数据。我正在使用Java进行数据读/写。
设备每隔10秒钟会自动关闭,为了保持其激活状态,我需要每隔10秒钟建立一次连接。我如何实现这个要求?
英文:
I am working on modbus device which is connected with system through serial port. I can read/write data on device's register. I am using java for data read/write.
The device automatically turns off on every 10 seconds, to keep it activated I need to establish the connection on every 10 seconds. How can I achieve this requirement?
答案1
得分: 2
可以使用Timer类来每隔10秒钟建立连接,代码如下:
class EstablishConnection extends TimerTask {
public void run() {
// 建立连接的代码
}
}
Timer timer = new Timer();
timer.schedule(new EstablishConnection(), 0, 10000);
英文:
You can use timer class to establish connection every 10 seconds as such:
class EstablishConnection extends TimerTask {
public void run() {
// Code to establish connection
}
}
Timer timer = new Timer();
timer.schedule(new EstablishConnection(), 0, 10000);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论