套接字被中断-Modbus-J2Mod

时间:2018-07-30 12:01:26

标签: java sockets udp communication modbus

我正在使用Java中的 modbus UDP实现[J2Mod(2.3.4)] 。我发现几乎没有有用的文档。我写了一个奴隶,如下所示。

基本上,创建一个 UDP从站,并为unitId = 0和unitId = 1添加processImage。然后它写出数字输入和数字输出。

public class Slave {
    private SimpleProcessImage image;
    private ModbusSlave slave;
    public Slave(){
        image = new SimpleProcessImage();
        for (int i = 0; i < 62000; i++) {
            image.addDigitalOut(i, new SimpleDigitalOut(false));
            image.addDigitalIn(i, new SimpleDigitalIn(false));
         }
         (new Timer()).scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                    read();
            }
        }, 10000, 10000);
        startServer();
    }

private void read() {
        System.out.print("Read In : ");
        for (int i = 0; i < interSize; i++) {
            System.out.print((image.getDigitalIn(i).isSet() ? 1 : 0) + " ");
        }
        System.out.print("Read Out: ");
        for (int i = 0; i < interSize; i++) {
            System.out.print((image.getDigitalOut(i).isSet() ? 1 : 0) + " ");
        }
}
    public void startServer() {
        try {
            slave = ModbusSlaveFactory.createUDPSlave(502);
            slave.addProcessImage(0, image);
            slave.addProcessImage(1, image);
            slave.open();
        } catch (ModbusException e) {
            e.printStackTrace();
        }
    }
}

我还写了一个客户示例。它可以成功地从/向从设备读取/写入。我使用master.readCoils master.writeCoil函数。

但是我有一个像modbus客户端一样工作的Java程序。 我看不到源代码,但是有一个日志屏幕。所以我可以看到该程序可以(使用writeCoil)编写,但无法从从服务器读取(通过readCoils),并依次抛出“无法发送UDP消息”,“套接字被中断”和“接收超时”错误

10:42:30.611 [Timer-1] ERROR c.g.j.m.io.ModbusUDPTransaction - Cannot send UDP message
com.ghgande.j2mod.modbus.ModbusIOException: Socket was interrupted
    at com.ghgande.j2mod.modbus.io.ModbusUDPTransport.readResponse(ModbusUDPTransport.java:132) ~[j2mod-2.3.4.jar:2.3.4]
    at com.ghgande.j2mod.modbus.io.ModbusUDPTransaction.execute(ModbusUDPTransaction.java:129) ~[j2mod-2.3.4.jar:2.3.4]
    at com.ghgande.j2mod.modbus.facade.AbstractModbusMaster.readCoils(AbstractModbusMaster.java:97) [j2mod-2.3.4.jar:2.3.4]
    at gov.tubitak.ys03.railcore.request.RailModbusUDPMaster.readCoils(RailModbusUDPMaster.java:42) [railcore-0.0.26.jar:na]
    at gov.tubitak.ys03.railcore.service.strategy.ILReaderSchedule.run(ILReaderSchedule.java:67) [railcore-0.0.26.jar:na]
    at java.util.TimerThread.mainLoop(Unknown Source) [na:1.8.0_121]
    at java.util.TimerThread.run(Unknown Source) [na:1.8.0_121]
Caused by: java.net.SocketTimeoutException: Receive timed out
    at java.net.TwoStacksPlainDatagramSocketImpl.receive0(Native Method) ~[na:1.8.0_121]
    at java.net.TwoStacksPlainDatagramSocketImpl.receive(Unknown Source) ~[na:1.8.0_121]
    at java.net.DatagramSocket.receive(Unknown Source) ~[na:1.8.0_121]
    at com.ghgande.j2mod.modbus.net.UDPMasterTerminal.receiveMessage(UDPMasterTerminal.java:100) ~[j2mod-2.3.4.jar:2.3.4]
    at com.ghgande.j2mod.modbus.io.ModbusUDPTransport.readResponse(ModbusUDPTransport.java:122) ~[j2mod-2.3.4.jar:2.3.4]
    ... 6 common frames omitted

0 个答案:

没有答案