Пример #1
0
        void Test(IModbusCommand cmd)
        {
            const int offset  = 2;
            var       request = new byte[offset + cmd.RequestLength];

            cmd.FillRequest(request, offset);
            var pcmd = ModbusParser.Parse(request, offset);

            Assert.AreEqual(pcmd.ToString(), cmd.ToString());

            var rtuWrap    = rtuProto.Wrap(cmd);
            var rtuRequest = new byte[offset + rtuWrap.RequestLength];

            rtuWrap.FillRequest(rtuRequest, offset);
            var prtuWrap = rtuProto.Parse(rtuRequest, offset);

            Assert.AreEqual(prtuWrap.ToString(), rtuWrap.ToString());

            var tcpWrap    = tcpProto.Wrap(cmd);
            var tcpRequest = new byte[offset + tcpWrap.RequestLength];

            tcpWrap.FillRequest(tcpRequest, offset);
            var ptcpWrap = tcpProto.Parse(tcpRequest, offset);

            Assert.AreEqual(ptcpWrap.ToString(), tcpWrap.ToString());
        }
Пример #2
0
        void Test(IModbusScanner scanner, IModbusProtocol protocol, IModbusCommand cmd)
        {
            const int offset  = 2;
            var       wrap    = protocol.Wrap(cmd);
            var       request = new byte[offset + wrap.RequestLength];

            wrap.FillRequest(request, offset);
            scanner.Append(request, offset, wrap.RequestLength);
            Assert.AreEqual(wrap.ToString(), scanner.Scan().ToString());
        }
Пример #3
0
        private object Execute(IModbusCommand cmd)
        {
            var wrapper  = protocol.Wrap(cmd);
            var request  = new byte[wrapper.RequestLength];
            var response = new byte[wrapper.ResponseLength];

            wrapper.FillRequest(request, 0);
            stream.Write(request);
            stream.Read(response);
            return(wrapper.ParseResponse(response, 0));
        }
Пример #4
0
        private object Execute(IModbusCommand cmd)
        {
            var wrapper  = protocol.Wrap(cmd);
            var request  = new byte[wrapper.RequestLength];
            var response = new byte[wrapper.ResponseLength];

            wrapper.FillRequest(request, 0);
            stream.Write(request);
            var count = stream.Read(response);

            if (count < response.Length)
            {
                wrapper.CheckException(response, count);
            }
            return(wrapper.ParseResponse(response, 0));
        }
    private object Execute(IModbusCommand cmd)
    {
        var wrapper  = new ModbusTCPWrapper(cmd, TransactionId++);
        var request  = new byte[wrapper.RequestLength];
        var response = new byte[wrapper.ResponseLength];

        wrapper.FillRequest(request, 0);

        var isOk = WriteReadDevice(request, response);

        if (isOk == false)
        {
            _log.Warn("Resending request.");
            Thread.Sleep(100);
            WriteReadDevice(request, response);
        }

        return(wrapper.ParseResponse(response, 0));
    }
Пример #6
0
 public ModbusTCPWrapper(IModbusCommand wrapped, int transactionId)
 {
     this.wrapped       = wrapped;
     this.transactionId = transactionId;
 }
Пример #7
0
 public IModbusWrapper Wrap(IModbusCommand wrapped)
 {
     return(new ModbusTCPWrapper(wrapped, NextTid()));
 }
Пример #8
0
 object Apply(ModbusModel model, IModbusCommand cmd)
 {
     return(cmd.ApplyTo(model));
 }
Пример #9
0
 public WriteSingleRegister(byte id, IModbusCommand mbCommand, DeviceInfo device, object locker)
     : base(id, mbCommand, device, locker)
 {
     modbusMapType = ModbusMapType.WriteSingleRegister;
 }
Пример #10
0
 protected Map(byte id, IModbusCommand mbCommand, DeviceInfo device)
 {
     slaveAddress = id;
     master       = mbCommand;
     deviceInfo   = device;
 }
 public WriteMultipleCoils(byte id, IModbusCommand mbCommand, DeviceInfo device, object locker)
     : base(id, mbCommand, device, locker)
 {
     modbusMapType = ModbusMapType.WriteMultipleCoils;
 }
 public ModbusTCPWrapper(IModbusCommand wrapped, ushort transactionId)
 {
     Wrapped       = wrapped;
     TransactionId = transactionId;
 }
Пример #13
0
        public override bool CustomInit(DeviceInfo deviceInfo)
        {
            if (deviceInfo == null)
            {
                return(false);
            }

            try
            {
                device = deviceInfo;
                LoadSettings();
                if (protocol == "modbustcp")
                {
                    modbusCommand = new ModbusTCP(ipAddress, port);
                }

                if (protocol == "modbusrtu")
                {
                    modbusCommand = new ModbusRTU(portName, baudRate, parity, dataBits, stopBits);
                }

                if (protocol == "modbusascii")
                {
                    modbusCommand = new ModbusASCII(portName, baudRate, parity, dataBits, stopBits);
                }

                modbusCommand.OnMessage += modbusCommand_OnMessage;

                readInputsReadMap = new ReadInputs(slaveId, modbusCommand, device, maxNumberReadRegistersAtTime,
                                                   registerShift);
                readInputsReadMap.OnMessage += OnNewMessage;
                readInputsReadMap.InitMap();

                readCoilsReadMap = new ReadCoils(slaveId, modbusCommand, device, maxNumberReadRegistersAtTime,
                                                 registerShift);
                readCoilsReadMap.OnMessage += OnNewMessage;
                readCoilsReadMap.InitMap();

                readInputRegisters = new ReadInputRegisters(slaveId, modbusCommand, device, maxNumberReadRegistersAtTime,
                                                            registerShift);
                readInputRegisters.OnMessage += OnNewMessage;
                readInputRegisters.InitMap();

                readHoldingRegisters = new ReadHoldingRegisters(slaveId, modbusCommand, device, maxNumberReadRegistersAtTime,
                                                                registerShift);
                readHoldingRegisters.OnMessage += OnNewMessage;
                readHoldingRegisters.InitMap();

                writeSingleCoil            = new WriteSingleCoil(slaveId, modbusCommand, device, Locker);
                writeSingleCoil.OnMessage += OnNewMessage;
                writeSingleCoil.InitMap();

                writeSingleRegister            = new WriteSingleRegister(slaveId, modbusCommand, device, Locker);
                writeSingleRegister.OnMessage += OnNewMessage;
                writeSingleRegister.InitMap();

                writeMultipleCoils            = new WriteMultipleCoils(slaveId, modbusCommand, device, Locker);
                writeMultipleCoils.OnMessage += OnNewMessage;
                writeMultipleCoils.InitMap();

                writeMultipleRegisters            = new WriteMultipleRegisters(slaveId, modbusCommand, device, Locker);
                writeMultipleRegisters.OnMessage += OnNewMessage;
                writeMultipleRegisters.InitMap();

                new PlcThread(CustomWorkThread, null, "ModbusMasterIO_CustomWorkThread").Start();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Пример #14
0
 protected ReadMap(byte id, IModbusCommand mbCommand, DeviceInfo device, ushort maxNumberReadRegisters,
                   short shift) : base(id, mbCommand, device)
 {
     maxNumberReadRegistersAtTime = maxNumberReadRegisters;
     registerShift = shift;
 }
Пример #15
0
 public ModbusRTUWrapper(IModbusCommand wrapped)
 {
     this.wrapped = wrapped;
 }
Пример #16
0
 public IModbusWrapper Wrap(IModbusCommand wrapped)
 {
     return(new ModbusRTUWrapper(wrapped));
 }
Пример #17
0
 public ReadInputs(byte id, IModbusCommand mbCommand, DeviceInfo device, ushort maxNumberReadRegisters,
                   short shift) : base(id, mbCommand, device, maxNumberReadRegisters, shift)
 {
     modbusMapType = ModbusMapType.ReadInputs;
 }
Пример #18
0
 public IModbusWrapper Wrap(IModbusCommand wrapped)
 {
     return(new ModbusTCPWrapper(wrapped, transactionId++));
 }
Пример #19
0
 protected WriteMap(byte id, IModbusCommand mbCommand, DeviceInfo device, object locker) :
     base(id, mbCommand, device)
 {
     Locker = locker;
 }