read nor you defined, you call is ReadHoldingRegistersRequest, Only oneself again enclosed the < br / > write call class libraries also provide write can
Java implements the serial port. I have implemented reading data, but sending data to the serial port does not work at all. Here is my code for reading data. Could you please tell me how to add the function of sending data to the serial port based on the following code?
public class VersolApplication {
// 设定MODBUS网络上从站地址
private final static int SLAVE_ADDRESS = 201;
//串行波特率
private final static int BAUD_RATE = 9600;
public static void main(String[] args) {
SpringApplication.run(VersolApplication.class, args);
try {
Runtime.getRuntime().exec("cmd /c start http://localhost:8080/index");
modbus(args);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void modbus(String[] args){
SerialPortWrapper serialParameters = new
SerialPortWrapperImpl("COM3", BAUD_RATE, 8, 1, 0, 0, 0);
/* 创建ModbusFactory工厂实例 */
ModbusFactory modbusFactory = new ModbusFactory();
/* 创建ModbusMaster实例 */
ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
/* 初始化 */
try {
master.setTimeout(3000);
master.setRetries(3);
master.init();
readHoldingRegistersTest(master, SLAVE_ADDRESS, 79, 4);
} catch (ModbusInitException e) {
e.printStackTrace();
} finally {
master.destroy();
}
}
private static void readHoldingRegistersTest(ModbusMaster master, int slaveId, int start, int len) {
try {
ReadHoldingRegistersRequest request = new ReadHoldingRegistersRequest(slaveId, start, len);
ReadHoldingRegistersResponse response = (ReadHoldingRegistersResponse)master.send(request);
if (response.isException()) {
System.out.println("Exception response: message=" + response.getExceptionMessage());
} else {
System.out.println("success"+ Arrays.toString(response.getShortData()));
short[] list = response.getShortData();
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
}
} catch (ModbusTransportException e) {
e.printStackTrace();
}
}
}
0 Answer
read nor you defined, you call is ReadHoldingRegistersRequest, Only oneself again enclosed the < br / > write call class libraries also provide write can
This method is in the startup class. Can I call it from the service layer?
direct call? But I didn't define the write method, right? Is it to define yourself?
这家伙很懒,什么都没留下...