Exemple #1
0
        //TODO: Evaluate ReadHoldingRegisters return type.  Should this be words? Bytes? something else?
        //      At the core of the protocol, this is really returning words.  The words can be interpreted
        //      as floating point, integers, unsigned, etc.
        public IEnumerable <Int16> ReadHoldingRegisters(Int16 startRegister, Int16 count)
        {
            List <byte> mbPacket = new List <byte>();

            mbPacket.Add(0x03);
            mbPacket.AddRange(startRegister.GetBytes());
            mbPacket.AddRange(count.GetBytes());

            MBAP mbap = MBAP.Create((Int16)(mbPacket.Count + 1), 0);

            mbPacket.InsertRange(0, mbap.GetBytes());

            tcpClient.GetStream().Write(mbPacket.ToArray(), 0, mbPacket.Count);

            byte[] readBuffer = new byte[500];

            tcpClient.GetStream().Read(readBuffer, 0, 500);

            int returnCount = (int)readBuffer[8];

            List <Int16> returnList = new List <short>();

            for (int i = 9; i < 9 + returnCount; i += 2)
            {
                returnList.Add(Int16Extensions.FromBytes(readBuffer[i], readBuffer[i + 1]));
            }

            return(returnList);
        }
Exemple #2
0
        public override ModbusTCPMessage Process(IMemoryModel memory)
        {
            int address = Int16Extensions.FromBytes(PDU.Data.First(), PDU.Data.Skip(1).First());
            int length  = Int16Extensions.FromBytes(PDU.Data.Skip(2).First(), PDU.Data.Skip(3).First());

            try
            {
                var values           = memory.Read4x(address, length);
                ProtocolDataUnit pdu = PDU.ValidResponse(
                    new byte[]
                    { (byte)(values.Count() * 2) }
                    .Concat(values.SelectMany(v => v.GetBytes())));
                ModbusTCPMessage reply = Create(
                    MBAP.Create((byte)(pdu.GetBytes().Count() + 1), ApplicationHeader.UnitID),
                    pdu);
                return(reply);
            }
            catch
            {
                ProtocolDataUnit pdu   = PDU.ErrorResponse(new byte[] { 0x01 });
                ModbusTCPMessage reply = Create(
                    MBAP.Create((byte)(pdu.GetBytes().Count() + 1), ApplicationHeader.UnitID),
                    pdu);
                return(reply);
            }
        }
        private Int16 StartAddress()
        {
            byte high = PDU.Data.First();
            byte low  = PDU.Data.Skip(1).First();

            return(Int16Extensions.FromBytes(high, low));
        }
        private IEnumerable <short> ParseValues()
        {
            List <short> values    = new List <short>();
            var          length    = PDU.Data.Skip(4).First();
            var          dataBlock = PDU.Data.Skip(5);

            for (int i = 0; i < length / 2; i++)
            {
                values.Add(Int16Extensions.FromBytes(
                               dataBlock.Skip(i * 2).First(),
                               dataBlock.Skip(i * 2 + 1).First()));
            }
            return(values);
        }
Exemple #5
0
 protected MBAP(IEnumerable <byte> message)
 {
     _length = Int16Extensions.FromBytes(message.Skip(4).First(), message.Skip(5).First());
     UnitID  = message.Skip(6).First();
 }