public void CreateWriteMultipleRegistersRequestMaxSize()
 {
     WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(1, 2,
         MessageUtility.CreateDefaultCollection<RegisterCollection, ushort>(3,
             Modbus.MaximumRegisterRequestResponseSize));
     Assert.Equal(Modbus.MaximumRegisterRequestResponseSize, request.NumberOfPoints);
 }
        public void CreateWriteMultipleRegistersRequestMaxSize()
        {
            WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(1, 2,
                                                                                      MessageUtility.CreateDefaultCollection <RegisterCollection, ushort>(3, Modbus.MaximumRegisterRequestResponseSize));

            Assert.Equal(Modbus.MaximumRegisterRequestResponseSize, request.NumberOfPoints);
        }
Beispiel #3
0
        internal static WriteMultipleRegistersResponse WriteMultipleRegisters(WriteMultipleRegistersRequest request, DataStore dataStore, ModbusDataCollection <ushort> dataSource)
        {
            DataStore.WriteData(dataStore, request.Data, dataSource, request.StartAddress, dataStore.SyncRoot);
            WriteMultipleRegistersResponse response = new WriteMultipleRegistersResponse(request.SlaveAddress, request.StartAddress, request.NumberOfPoints);

            return(response);
        }
        public void ToString_WriteMultipleRegistersRequest()
        {
            RegisterCollection col = new RegisterCollection(10, 20, 30, 40, 50);
            WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(11, 34, col);

            Assert.Equal("Write 5 holding registers starting at address 34.", request.ToString());
        }
        private void RequestToDB(IModbusMessage message)
        {
            WriteMultipleRegistersRequest typedResponse = (WriteMultipleRegistersRequest)message;

            ModbusMessageLog logitem = new ModbusMessageLog()
            {
                LogTime          = DateTime.Now,
                LogMessage       = message.ToString(),
                FunctionCode     = message.FunctionCode.ToString(),
                MessageFrame     = message.MessageFrame,
                ProtocolDataUnit = message.ProtocolDataUnit,
                SlaveAddress     = message.SlaveAddress.ToString(),
                TransactionId    = message.TransactionId.ToString()
            };

            int iStartIndex = typedResponse.MinimumFrameSize;
            int iLen        = message.MessageFrame.Length - iStartIndex;

            byte[] bdata = new byte[iLen];
            Array.Copy(message.MessageFrame, iStartIndex, bdata, 0, iLen);

            string strFrame = System.Text.Encoding.UTF8.GetString(bdata).TrimEnd('\0');
            string strUnit  = Modbus.ConvertTools.BytesToHexString(message.ProtocolDataUnit);

            DataLogHelper.AddModbusMessageLog(logitem);

            //将数据传入后台处理
            string strToText = logitem.ToJsonString();

            SendToSocketServer(strToText);
        }
        public void ToString_WriteMultipleRegistersRequest()
        {
            RegisterCollection            col     = new RegisterCollection(10, 20, 30, 40, 50);
            WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(11, 34, col);

            Assert.AreEqual("Write 5 holding registers starting at address 34.", request.ToString());
        }
 public void GetMbapHeader()
 {
     WriteMultipleRegistersRequest message = new WriteMultipleRegistersRequest(3, 1,
         MessageUtility.CreateDefaultCollection<RegisterCollection, ushort>(0, 120));
     message.TransactionId = 45;
     Assert.Equal(new byte[] {0, 45, 0, 0, 0, 247, 3}, ModbusIpTransport.GetMbapHeader(message));
 }
Beispiel #8
0
        public void GetMbapHeader()
        {
            var message = new WriteMultipleRegistersRequest(3, 1, MessageUtility.CreateDefaultCollection <RegisterCollection, ushort>(0, 120));

            message.TransactionId = 45;
            Assert.Equal(new byte[] { 0, 45, 0, 0, 0, 247, 3 }, ModbusIpTransport.GetMbapHeader(message));
        }
        public void CreateModbusMessageWriteMultipleRegistersRequestWithInvalidFrameSize()
        {
            WriteMultipleRegistersRequest request =
                ModbusMessageFactory.CreateModbusMessage <WriteMultipleRegistersRequest>(new byte[]
                                                                                         { 11, Modbus.WriteMultipleRegisters, 0, 5, 0, 1, 2 });

            Assert.Fail();
        }
Beispiel #10
0
        public void WriteMultipleRegistersRequestToString()
        {
            // Arrange
            var request = new WriteMultipleRegistersRequest(11, 34, new ushort[] { 10, 20, 30, 40, 50 });

            // Act/Assert
            Assert.Equal("Write 5 holding registers starting at address 34 into slave 11", request.ToString());
        }
Beispiel #11
0
        public Task WriteMultipleRegistersAsync(byte slaveAddress, ushort startAddress, ushort[] data, CancellationToken token = default)
        {
            ValidateData(data, 123);

            var request = new WriteMultipleRegistersRequest(slaveAddress, startAddress, data);

            return(Transport.SendAsync <WriteMultipleRegistersResponse>(request, token));
        }
Beispiel #12
0
        /// <summary>
        ///     Write a block of 1 to 123 contiguous 16 bit holding registers.
        /// </summary>
        /// <param name="slaveAddress">Address of the device to write to.</param>
        /// <param name="startAddress">Address to begin writing values.</param>
        /// <param name="data">Values to write.</param>
        public void WriteMultipleRegisters(byte slaveAddress, ushort startAddress, short[] data)
        {
            ValidateData("data", data, 123);

            var request = new WriteMultipleRegistersRequest(slaveAddress, startAddress, new RegisterCollection(data));

            Transport.UnicastMessage <WriteMultipleRegistersResponse>(request);
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="slaveAddress"></param>
 /// <param name="startReadAddress"></param>
 /// <param name="numberOfPointsToRead"></param>
 /// <param name="startWriteAddress"></param>
 /// <param name="writeData"></param>
 public ReadWriteMultipleRegistersRequest(byte slaveAddress, ushort startReadAddress, ushort numberOfPointsToRead,
     ushort startWriteAddress, RegisterCollection writeData)
     : base(slaveAddress, Modbus.ReadWriteMultipleRegisters)
 {
     _readRequest = new ReadHoldingInputRegistersRequest(Modbus.ReadHoldingRegisters, slaveAddress,
         startReadAddress, numberOfPointsToRead);
     _writeRequest = new WriteMultipleRegistersRequest(slaveAddress, startWriteAddress, writeData);
 }
Beispiel #14
0
        /// <summary>
        ///    Asynchronously writes a block of 1 to 123 contiguous registers.
        /// </summary>
        /// <param name="slaveAddress">Address of the device to write to.</param>
        /// <param name="startAddress">Address to begin writing values.</param>
        /// <param name="data">Values to write.</param>
        /// <returns>A task that represents the asynchronous write operation</returns>
        public Task WriteMultipleRegistersAsync(byte slaveAddress, ushort startAddress, short[] data)
        {
            ValidateData("data", data, 123);

            var request = new WriteMultipleRegistersRequest(slaveAddress, startAddress, new RegisterCollection(data));

            return(PerformWriteRequestAsync <WriteMultipleRegistersResponse>(request));
        }
 public void CreateWriteMultipleRegistersRequestFixture()
 {
     RegisterCollection col = new RegisterCollection(10, 20, 30, 40, 50);
     WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(11, 34, col);
     Assert.Equal(Modbus.WriteMultipleRegisters, request.FunctionCode);
     Assert.Equal(11, request.SlaveAddress);
     Assert.Equal(34, request.StartAddress);
     Assert.Equal(10, request.ByteCount);
     Assert.Equal(col.NetworkBytes, request.Data.NetworkBytes);
 }
        public byte[] BuildWriteMultipleRegistersCommand(byte slaveAddress, ushort startAddress, ushort[] data, out IModbusMessage request)
        {
            ValidateData("data", data, 123);

            request = new WriteMultipleRegistersRequest(
                slaveAddress,
                startAddress,
                new RegisterCollection(data));

            return(Transport.BuildMessageFrame(request));
        }
        public void CreateWriteMultipleRegistersRequestFixture()
        {
            RegisterCollection            col     = new RegisterCollection(10, 20, 30, 40, 50);
            WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(11, 34, col);

            Assert.AreEqual(Modbus.WriteMultipleRegisters, request.FunctionCode);
            Assert.AreEqual(11, request.SlaveAddress);
            Assert.AreEqual(34, request.StartAddress);
            Assert.AreEqual(10, request.ByteCount);
            Assert.AreEqual(col.NetworkBytes, request.Data.NetworkBytes);
        }
        public void CreateModbusMessageWriteMultipleRegistersRequest()
        {
            WriteMultipleRegistersRequest request         = ModbusMessageFactory.CreateModbusMessage <WriteMultipleRegistersRequest>(new byte[] { 11, Modbus.WriteMultipleRegisters, 0, 5, 0, 1, 2, 255, 255 });
            WriteMultipleRegistersRequest expectedRequest = new WriteMultipleRegistersRequest(11, 5, new RegisterCollection(ushort.MaxValue));

            AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
            Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
            Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
            Assert.AreEqual(expectedRequest.ByteCount, request.ByteCount);
            Assert.AreEqual(expectedRequest.Data.NetworkBytes, request.Data.NetworkBytes);
        }
        protected override void InitializeUnique(byte[] frame)
        {
            if (frame.Length < _minimumFrameSize + frame[10])
                throw new FormatException("Message frame does not contain enough bytes.");

            byte[] readFrame = CollectionUtility.Slice(frame, 2, 4);
            byte[] writeFrame = CollectionUtility.Slice(frame, 6, frame.Length - 6);
            byte[] header = { SlaveAddress, FunctionCode };

            _readRequest = ModbusMessageFactory.CreateModbusMessage<ReadHoldingInputRegistersRequest>(CollectionUtility.Concat(header, readFrame));
            _writeRequest = ModbusMessageFactory.CreateModbusMessage<WriteMultipleRegistersRequest>(CollectionUtility.Concat(header, writeFrame));
        }
Beispiel #20
0
        public Function12RequestWrite(byte functionCode, ushort startAddress, byte devicenumber, byte moduleNumber, ushort[] writeArray) : base(devicenumber, functionCode)
        {
            List <byte> customBytes = new List <byte>();
            var         res         = 0xF0 | moduleNumber;

            customBytes.Add(Convert.ToByte(res));

            WriteMultipleRegistersRequest innerRequest =
                new WriteMultipleRegistersRequest(devicenumber, startAddress, new RegisterCollection(writeArray));

            InnerReauest = innerRequest;
            byte[] crcforinnerRequest = ModbusUtility.CalculateCrc(innerRequest.MessageFrame);
            customBytes.AddRange(innerRequest.MessageFrame);
            customBytes.AddRange(crcforinnerRequest);


            MessageImpl.CustomBytesInRequest = customBytes.ToArray();
        }
Beispiel #21
0
    public ReadWriteMultipleRegistersRequest(
        byte slaveAddress,
        ushort startReadAddress,
        ushort numberOfPointsToRead,
        ushort startWriteAddress,
        RegisterCollection writeData)
        : base(slaveAddress, Modbus.ReadWriteMultipleRegisters)
    {
        ReadRequest = new ReadHoldingInputRegistersRequest(
            Modbus.ReadHoldingRegisters,
            slaveAddress,
            startReadAddress,
            numberOfPointsToRead);

        WriteRequest = new WriteMultipleRegistersRequest(
            slaveAddress,
            startWriteAddress,
            writeData);
    }
Beispiel #22
0
    protected override void InitializeUnique(byte[] frame)
    {
        if (frame.Length < MinimumFrameSize + frame[10])
        {
            throw new FormatException("Message frame does not contain enough bytes.");
        }

        byte[] readFrame  = new byte[2 + 4];
        byte[] writeFrame = new byte[frame.Length - 6 + 2];

        readFrame[0] = writeFrame[0] = SlaveAddress;
        readFrame[1] = writeFrame[1] = FunctionCode;

        Buffer.BlockCopy(frame, 2, readFrame, 2, 4);
        Buffer.BlockCopy(frame, 6, writeFrame, 2, frame.Length - 6);

        ReadRequest  = ModbusMessageFactory.CreateModbusMessage <ReadHoldingInputRegistersRequest>(readFrame);
        WriteRequest = ModbusMessageFactory.CreateModbusMessage <WriteMultipleRegistersRequest>(writeFrame);
    }
Beispiel #23
0
        public WriteMultipleRegistersResponse WriteMultipleRegisters(ushort startingAddress, ushort[] registers)
        {
            var function = new WriteMultipleRegistersRequest(startingAddress, registers);

            return(CallModbusFunctionSync <WriteMultipleRegistersResponse>(function));
        }
Beispiel #24
0
        public ushort WriteMultipleRegistersAsync(ushort startingAddress, ushort[] registers)
        {
            var function = new WriteMultipleRegistersRequest(startingAddress, registers);

            return(CallModbusFunction(function));
        }
Beispiel #25
0
        public override ModbusFunction WriteMultipleRegisters(WriteMultipleRegistersRequest request)
        {
            var response = new WriteMultipleRegistersResponse(request.StartingAddress, request.Quantity);

            return(response);
        }
 public abstract ModbusFunction WriteMultipleRegisters(WriteMultipleRegistersRequest request);
        internal static WriteMultipleRegistersResponse WriteMultipleRegisters(WriteMultipleRegistersRequest request, ModbusDataCollection<ushort> dataSource)
        {
            DataStore.WriteData(request.Data, dataSource, request.StartAddress);
            WriteMultipleRegistersResponse response = new WriteMultipleRegistersResponse(request.SlaveAddress, request.StartAddress, request.NumberOfPoints);

            return response;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="frame"></param>
        protected override void InitializeUnique(byte[] frame)
        {
            if (frame.Length < MinimumFrameSize + frame[10])
                throw new FormatException("Message frame does not contain enough bytes.");

            byte[] readFrame = new byte[2 + 4];
            byte[] writeFrame = new byte[frame.Length - 6 + 2];

            readFrame[0] = writeFrame[0] = SlaveAddress;
            readFrame[1] = writeFrame[1] = FunctionCode;

            Buffer.BlockCopy(frame, 2, readFrame, 2, 4);
            Buffer.BlockCopy(frame, 6, writeFrame, 2, frame.Length - 6);

            _readRequest = ModbusMessageFactory.CreateModbusMessage<ReadHoldingInputRegistersRequest>(readFrame);
            _writeRequest = ModbusMessageFactory.CreateModbusMessage<WriteMultipleRegistersRequest>(writeFrame);
        }
 public void CreateModbusMessageWriteMultipleRegistersRequest()
 {
     WriteMultipleRegistersRequest request =
         ModbusMessageFactory.CreateModbusMessage<WriteMultipleRegistersRequest>(new byte[]
         {11, Modbus.WriteMultipleRegisters, 0, 5, 0, 1, 2, 255, 255});
     WriteMultipleRegistersRequest expectedRequest = new WriteMultipleRegistersRequest(11, 5,
         new RegisterCollection(ushort.MaxValue));
     AssertModbusMessagePropertiesAreEqual(expectedRequest, request);
     Assert.AreEqual(expectedRequest.StartAddress, request.StartAddress);
     Assert.AreEqual(expectedRequest.NumberOfPoints, request.NumberOfPoints);
     Assert.AreEqual(expectedRequest.ByteCount, request.ByteCount);
     Assert.AreEqual(expectedRequest.Data.NetworkBytes, request.Data.NetworkBytes);
 }
Beispiel #30
0
        static void TestMessage()
        {
            ModbusMessage fc01Req = new ReadCoilsRequest(0x13, 0x13);
            ModbusMessage fc01Res = new ReadCoilsResponse(true, false, true, true, false, false, true, true, true, true, false, true, false, true, true, false, true, false, true, false);
            ModbusMessage fc02Req = new ReadDiscreteInputsRequest(0xc4, 0x16);
            ModbusMessage fc02Res = new ReadDiscreteInputsResponse(false, false, true, true, false, true, false, true, true, true, false, true, true, false, true, true, true, false, true, false, true, true);
            ModbusMessage fc03Req = new ReadHoldingRegistersRequest(0x6b, 0x03);
            ModbusMessage fc03Res = new ReadHoldingRegistersResponse(0x022b, 0x0000, 0x0064);
            ModbusMessage fc04Req = new ReadInputRegistersRequest(0x08, 0x01);
            ModbusMessage fc04Res = new ReadInputRegistersResponse(0x000a);
            ModbusMessage fc05Req = new WriteSingleCoilRequest(0xac, true);
            ModbusMessage fc05Res = new WriteSingleCoilResponse(0xac, true);
            ModbusMessage fc06Req = new WriteSingleRegisterRequest(0x01, 0x0003);
            ModbusMessage fc06Res = new WriteSingleRegisterResponse(0x01, 0x0003);
            ModbusMessage fc0fReq = new WriteMultipleCoilsRequest(0x13, true, false, true, true, false, false, true, true, true, false);
            ModbusMessage fc0fRes = new WriteMultipleCoilsResponse(0x13, 0x0a);
            ModbusMessage fc10Req = new WriteMultipleRegistersRequest(0x01, 0x000a, 0x0102);
            ModbusMessage fc10Res = new WriteMultipleRegistersResponse(0x01, 0x02);

            PrintMessagePDU(fc01Req);
            PrintMessagePDU(fc01Res);
            PrintMessagePDU(fc02Req);
            PrintMessagePDU(fc02Res);
            PrintMessagePDU(fc03Req);
            PrintMessagePDU(fc03Res);
            PrintMessagePDU(fc04Req);
            PrintMessagePDU(fc04Res);
            PrintMessagePDU(fc05Req);
            PrintMessagePDU(fc05Res);
            PrintMessagePDU(fc06Req);
            PrintMessagePDU(fc06Res);
            PrintMessagePDU(fc0fReq);
            PrintMessagePDU(fc0fRes);
            PrintMessagePDU(fc10Req);
            PrintMessagePDU(fc10Res);

            Assert(MessagePDU(fc01Req) == "Req.: 01 00 13 00 13", $"{nameof(fc01Req)} Fail");
            Assert(MessagePDU(fc01Res) == "Res.: 01 03 cd 6b 05", $"{nameof(fc01Res)} Fail");
            Assert(MessagePDU(fc02Req) == "Req.: 02 00 c4 00 16", $"{nameof(fc02Req)} Fail");
            Assert(MessagePDU(fc02Res) == "Res.: 02 03 ac db 35", $"{nameof(fc02Res)} Fail");
            Assert(MessagePDU(fc03Req) == "Req.: 03 00 6b 00 03", $"{nameof(fc03Req)} Fail");
            Assert(MessagePDU(fc03Res) == "Res.: 03 06 02 2b 00 00 00 64", $"{nameof(fc03Res)} Fail");
            Assert(MessagePDU(fc04Req) == "Req.: 04 00 08 00 01", $"{nameof(fc04Req)} Fail");
            Assert(MessagePDU(fc04Res) == "Res.: 04 02 00 0a", $"{nameof(fc04Res)} Fail");
            Assert(MessagePDU(fc05Req) == "Req.: 05 00 ac ff 00", $"{nameof(fc05Req)} Fail");
            Assert(MessagePDU(fc05Res) == "Res.: 05 00 ac ff 00", $"{nameof(fc05Res)} Fail");
            Assert(MessagePDU(fc06Req) == "Req.: 06 00 01 00 03", $"{nameof(fc06Req)} Fail");
            Assert(MessagePDU(fc06Res) == "Res.: 06 00 01 00 03", $"{nameof(fc06Res)} Fail");
            Assert(MessagePDU(fc0fReq) == "Req.: 0f 00 13 00 0a 02 cd 01", $"{nameof(fc0fReq)} Fail");
            Assert(MessagePDU(fc0fRes) == "Res.: 0f 00 13 00 0a", $"{nameof(fc0fRes)} Fail");
            Assert(MessagePDU(fc10Req) == "Req.: 10 00 01 00 02 04 00 0a 01 02", $"{nameof(fc10Req)} Fail");
            Assert(MessagePDU(fc10Res) == "Res.: 10 00 01 00 02", $"{nameof(fc10Res)} Fail");

            Assert(fc01Req.FunctionCode == (FunctionCode)0x01);
            Assert(fc01Res.FunctionCode == (FunctionCode)0x01);
            Assert(fc02Req.FunctionCode == (FunctionCode)0x02);
            Assert(fc02Res.FunctionCode == (FunctionCode)0x02);
            Assert(fc03Req.FunctionCode == (FunctionCode)0x03);
            Assert(fc03Res.FunctionCode == (FunctionCode)0x03);
            Assert(fc04Req.FunctionCode == (FunctionCode)0x04);
            Assert(fc04Res.FunctionCode == (FunctionCode)0x04);
            Assert(fc05Req.FunctionCode == (FunctionCode)0x05);
            Assert(fc05Res.FunctionCode == (FunctionCode)0x05);
            Assert(fc06Req.FunctionCode == (FunctionCode)0x06);
            Assert(fc06Res.FunctionCode == (FunctionCode)0x06);
            Assert(fc0fReq.FunctionCode == (FunctionCode)0x0f);
            Assert(fc0fRes.FunctionCode == (FunctionCode)0x0f);
            Assert(fc10Req.FunctionCode == (FunctionCode)0x10);
            Assert(fc10Res.FunctionCode == (FunctionCode)0x10);
        }