java Modbus TCP SCADA/HMI工作速度非常慢

java Modbus TCP SCADA/HMI工作速度非常慢,java,multithreading,tcp,modbus,Java,Multithreading,Tcp,Modbus,我试图用Java编写一个简单的SCADA/HMI代码,通过Modbus TCP将我的计算机连接到PLC。我编写了在我的PLC上打开/关闭5个线圈的代码,但应用程序运行非常缓慢–当我按两次按钮时,PLC需要4-6秒才能获得命令,这是打开/关闭线圈的条件。但我希望它能很快发挥作用 在代码中,我编写了main类(在其中建立连接)和thread类(在其中为每个coil执行ModBusTCPTransaction)。我将线程类称为»Daemon«,并在主类中启动它。但也许这不是应该做的事情——也许任何人都

我试图用Java编写一个简单的SCADA/HMI代码,通过Modbus TCP将我的计算机连接到PLC。我编写了在我的PLC上打开/关闭5个线圈的代码,但应用程序运行非常缓慢–当我按两次按钮时,PLC需要4-6秒才能获得命令,这是打开/关闭线圈的条件。但我希望它能很快发挥作用

在代码中,我编写了main类(在其中建立连接)和thread类(在其中为每个coil执行ModBusTCPTransaction)。我将线程类称为»Daemon«,并在主类中启动它。但也许这不是应该做的事情——也许任何人都可以用2/3句话写下这种类型的SCADA/HMI系统通常是如何完成/工作的……我甚至需要一个deamon thread类吗

这是我的密码

import java.net.*;
import java.io.*;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;

// 1. daemon class
class NitCoil extends Thread {

    private WriteCoilRequest coil_req = null;
    private ModbusTCPTransaction trans = null;
    private int i;

    NitCoil(String s , int i) {
        super(s);
        this.i = i;
    }

    public void run(){
        try {
        while(true) {
            coil_req = new WriteCoilRequest(i, ModbusTest.coil_con[i]);
            trans = new ModbusTCPTransaction(ModbusTest.con);
            trans.setRequest(coil_req);
            trans.execute();
            this.sleep((int)(Math.random()*100));
        }
        } catch (Exception ex) {
            ex.printStackTrace();
    }
    }

}
//2. main class
public class ModbusTest {

    public static TCPMasterConnection con = null;
    public static boolean[] coil_con = {false,false,false,false,false};
    public static void main(String[] args) {

      try {

          /* Variables for storing the parameters */
          InetAddress addr = null; //the slave's address
          int port = Modbus.DEFAULT_PORT;
          int repeat = 1; //a loop for repeating the transaction

          //3. Setup the parameters
          if (args.length < 1) {
              System.exit(1);
          } else {
              try {
                  String astr = "192.168.0.25:502"; 
                  int idx = astr.indexOf(':');
                  {
                      port = Integer.parseInt(astr.substring(idx+1));
                      astr = astr.substring(0,idx);
                  }
                  addr = InetAddress.getByName(astr);
                  if (args.length == 1) {
                      repeat = Integer.parseInt(args[0]);
                  }
              } catch (Exception ex) {
                  ex.printStackTrace();
                  System.exit(1);
              }
          }

          //4. Open the connection
          con = new TCPMasterConnection(addr);
          con.setPort(port);
          con.connect();

          //5. defining frame, panel, button
          JFrame frame = new JFrame("JFrame Example");
          JPanel panel = new JPanel();
          panel.setLayout(new FlowLayout());
          JLabel label = new JLabel("This is a label!");

          //6. creating 5 buttons             
          JButton[] button = new JButton[5];

          for (int j = 0; j < 5; j++){
               final int temp = j;
               button[j] = new JButton(String.valueOf(j));

          //7. button
          button[j].setText("Switch ON light "+j);

          button[j].addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                 if (coil_con[temp] == true) 
                     coil_con[temp] = false; 
                    else
                     coil_con[temp] = true;
            }
            });
          };
          panel.add(label);
          panel.add(button[0]);
          panel.add(button[1]);
          panel.add(button[2]);
          panel.add(button[3]);
          panel.add(button[4]);

          //8. call of demon
          NitCoil n1 = new NitCoil("daemon1", 0);
          n1.setDaemon(true);
          n1.start();
          NitCoil n2 = new NitCoil("daemon2", 1);
          n2.setDaemon(true);
          n2.start();
          NitCoil n3 = new NitCoil("daemon3", 2);
          n3.setDaemon(true);
          n3.start();
          NitCoil n4 = new NitCoil("daemon4", 3);
          n4.setDaemon(true);
          n4.start();
          NitCoil n5 = new NitCoil("daemon5", 4);
          n5.setDaemon(true);
          n5.start();


          //9. Close the connection
          JButton buttonClose = new JButton();
          buttonClose.setText("disconnect");

          buttonClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                 ModbusTest.con.close();
            }
            }); 
          panel.add(buttonClose);  
          panel.setBackground(Color.green);

          frame.add(panel);
          frame.setSize(300, 300);
          frame.setLocationRelativeTo(null);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);

      } catch (Exception ex) {
        ex.printStackTrace();
      }

    }  

}

你看过Wireshark的响应时间了吗?可能设备响应速度慢

另外,按照目前的设置方式,您一直在连续写入所有五个线圈,这可能并不理想


由于线圈地址是连续的,您也可以使用WriteMultipleCoils函数0x0F在一个请求中写入所有五个线圈。

我现在使用了WriteMultipleCoilsRequest函数,现在SCADA工作得非常快。现在,我在无限循环后台程序中执行事务—每100毫秒发生一次事务,这可以吗,或者通常这种SCADA只执行一次事务—当我更改发生时,按下一个按钮。。?关于保留寄存器,它们的事务是否也在无限循环中连续执行,或者它们是否仅在发生更改时执行?SCADA上的十进制值发生更改..?您通常不会不断地反复写入相同的值。阅读价值观是另一回事。。。大多数工业协议没有任何订阅或未经请求的机制,因此使用轮询。实际上是什么»轮询«,我一直在网上搜索,但没有找到任何明确的解释-轮询是否意味着执行modbus事务。。?那么读取值呢?是否也使用轮询呢?轮询只是指一遍又一遍地读取您感兴趣的值。我猜,使用这个库,它将一遍又一遍地执行读取请求的事务。