Modbus TCP common driver class. This class implements a modbus TCP master driver. It supports the following commands: Read coils Read discrete inputs Write single coil Write multiple cooils Read holding register Read input register Write single register Write multiple register All commands can be sent in synchronous or asynchronous mode. If a value is accessed in synchronous mode the program will stop and wait for slave to response. If the slave didn't answer within a specified time a timeout exception is called. The class uses multi threading for both synchronous and asynchronous access. For the communication two lines are created. This is necessary because the synchronous thread has to wait for a previous command to finish.
Пример #1
0
 public ModbusServer(string ip, ushort port)
 {
     this.ip = ip;
     this.port = port;
     this.modbusMaster = new Master(ip, port);
 }
Пример #2
0
 void modbusMaster_OnException(Master obj, ushort id, byte function, byte exception)
 {
     if (modbusMaster == obj) {
         Logger.Error("Ошибка при чтении данных " + CurrentServer.IP + ":" + CurrentServer.Port);
         if (OnError != null) {
             OnError();
         }
     }
 }
Пример #3
0
 void modbusMaster_OnResponseData(Master obj, ushort id, byte function, byte[] data)
 {
     if (modbusMaster == obj) {
         if (OnResponse != null) {
             OnResponse(id, function, data);
         }
     }
 }
Пример #4
0
 public void Init()
 {
     if (this.modbusMaster != null) {
         this.modbusMaster.OnException -= exceptionEvent;
         this.modbusMaster.OnResponseData -= responseEvent;
         try { modbusMaster.disconnect(); } catch { }
     }
     this.modbusMaster = new Master();
     this.modbusMaster.OnException += exceptionEvent;
     this.modbusMaster.OnResponseData += responseEvent;
     this.modbusMaster.timeout = 500;
 }