我正在使用jamod库连接到modbus,我已连接到modbus模拟器并成功读取值但我想实时读取值。我的问题是我想……
定义一个 Runnable 将轮询数据
Runnable
Runnable r = new Runnable(){ @Override public void run(){ //TODO: define abort criteria for boolean:isRunning while(isRunning){ //TODO define int:pollDelay Thread.sleep(pollDelay); //here you poll for new results trans.setRequest(req); ReadMultipleRegistersResponse res = (ReadMultipleRegistersResponse) trans.getResponse(); tf1.setText(String.valueOf(res.getRegisterValue(0))); ... } } }
然后启动runnable而不是仅获取一次数据
VBox vb = new VBox(); vb.setAlignment(Pos.CENTER); TextField tf1 = new TextField(); ... ModbusTCPTransaction trans = new ModbusTCPTransaction(con); Runnable r = new ... //see code above new Thread(r).start(); //starts the polling
这段代码既不编译也不运行,但它提供了如何连续轮询数据的提示......