示例#1
0
        /// <summary>
        /// Helper for unpacking discrete data incoming as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PopDiscretes(
            ModbusCommand command,
            ByteArrayReader body)
        {
            var byteCount = body.ReadByte();

            var count = command.Count;

            command.Data              = new ushort[count];
            command.QueryTotalLength += (byteCount + 1);

            int k = 0;

            while (body.EndOfBuffer == false)
            {
                if (command.Count <= k)
                {
                    break;
                }
                byte hb = body.CanRead(1) ? body.ReadByte() : (byte)0;
                byte lb = body.CanRead(1) ? body.ReadByte() : (byte)0;
                command.Data[k++] = (ushort)((hb << 8) | lb);
                //int n = count <= 8 ? count : 8;
                //count -= n;
                //for (int i = 0; i < n; i++)
                //    command.Data[k++] = (ushort)(cell & (1 << i));
            }
        }
示例#2
0
        public override bool ServerDecode(
            ModbusCommand command,
            ByteArrayReader body)
        {
            if (ModbusCodecBase.PopRequestHeader(command, body) == false)
            {
                return(false);
            }

            var success = false;

            if (body.CanRead(1))
            {
                var count = body.ReadByte();

                if (body.CanRead(count))
                {
                    command.QueryTotalLength += (count + 1);
                    count       /= 2;
                    command.Data = new ushort[count];

                    for (int i = 0; i < count; i++)
                    {
                        command.Data[i] = body.ReadUInt16BE();
                    }

                    success = true;
                }
            }

            return(success);
        }
        public override bool ClientDecode(
            ModbusCommand command,
            ByteArrayReader body)
        {
            var success = false;

            if (body.CanRead(1))
            {
                var count = body.ReadByte();

                if (body.CanRead(count))
                {
                    count       /= 2;
                    command.Data = new ushort[count];
                    for (int i = 0; i < count; i++)
                    {
                        command.Data[i] = body.ReadUInt16BE();
                    }

                    success = true;
                }
            }

            return(success);
        }
示例#4
0
        CommResponse IProtocolCodec.ClientDecode(CommDataBase data)
        {
            ModbusClient    ownerProtocol = (ModbusClient)data.OwnerProtocol;
            ModbusCommand   userData      = (ModbusCommand)data.UserData;
            ByteArrayReader incomingData  = data.IncomingData;
            int             count         = incomingData.Length - 4;

            if (count >= 0 && (int)incomingData.ReadByte() == (int)ownerProtocol.Address)
            {
                byte            num1 = incomingData.ReadByte();
                ByteArrayReader body = new ByteArrayReader(incomingData.ReadBytes(count));
                ushort          num2 = ByteArrayHelpers.CalcCRC16(incomingData.ToArray(), 0, incomingData.Length - 2);
                if ((int)incomingData.ReadInt16LE() == (int)(short)num2 && ((int)num1 & (int)sbyte.MaxValue) == (int)userData.FunctionCode)
                {
                    if (num1 <= (byte)127)
                    {
                        ModbusCodecBase.CommandCodecs[(int)num1]?.ClientDecode(userData, body);
                        return(new CommResponse(data, 3));
                    }
                    if (incomingData.CanRead(1))
                    {
                        userData.ExceptionCode = incomingData.ReadByte();
                    }
                    return(new CommResponse(data, 2));
                }
            }
            return(new CommResponse(data, 0));
        }
示例#5
0
        internal static void PopDiscretes(ModbusCommand command, ByteArrayReader body)
        {
            byte num1  = body.ReadByte();
            int  count = command.Count;

            command.Data              = new ushort[count];
            command.QueryTotalLength += (int)num1 + 1;
            byte num2;
            byte num3;

            for (int index = 0; !body.EndOfBuffer && command.Count > index; command.Data[index++] = (ushort)((uint)num2 << 8 | (uint)num3))
            {
                num2 = body.CanRead(1) ? body.ReadByte() : (byte)0;
                num3 = body.CanRead(1) ? body.ReadByte() : (byte)0;
            }
        }
示例#6
0
        /// <summary>
        /// Helper for unpacking discrete data incoming as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        /// <returns>True if the data could be read</returns>
        internal static bool PopDiscretes(
            ModbusCommand command,
            ByteArrayReader body)
        {
            var success = false;

            if (body.CanRead(1))
            {
                var byteCount = body.ReadByte();

                if (body.CanRead(byteCount))
                {
                    var count = command.Count;
                    command.Data              = new ushort[count];
                    command.QueryTotalLength += (byteCount + 1);

                    int k = 0;
                    while (count > 0)
                    {
                        byteCount--;
                        int cell = body.ReadByte();

                        int n = count <= 8 ? count : 8;
                        count -= n;
                        for (int i = 0; i < n; i++)
                        {
                            command.Data[k++] = (ushort)(cell & (1 << i));
                        }
                    }

                    success = true;
                }
            }

            return(success);
        }
示例#7
0
        private static void ReadExceptionHandlers(CilRawFatMethodBody fatBody, CilMethodBody result)
        {
            foreach (var section in fatBody.ExtraSections)
            {
                if (section.IsEHTable)
                {
                    var reader = new ByteArrayReader(section.Data);
                    int size = section.IsFat
                        ? CilExceptionHandler.FatExceptionHandlerSize
                        : CilExceptionHandler.TinyExceptionHandlerSize;

                    while (reader.CanRead(size))
                        result.ExceptionHandlers.Add(CilExceptionHandler.FromReader(result, reader, section.IsFat));
                }
            }
        }
示例#8
0
 /// <summary>
 /// Extract the typical header for a request command (server-side)
 /// </summary>
 /// <param name="command"></param>
 /// <param name="body"></param>
 /// <returns>True if the data could be read</returns>
 internal static bool PopRequestHeader(
     ModbusCommand command,
     ByteArrayReader body)
 {
     if (body.CanRead(4))
     {
         command.Offset            = body.ReadUInt16BE();
         command.Count             = body.ReadInt16BE();
         command.QueryTotalLength += 4;
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#9
0
        public override bool ServerDecode(
            ModbusCommand command,
            ByteArrayReader body)
        {
            if (body.CanRead(4))
            {
                command.Offset            = body.ReadUInt16BE();
                command.Count             = 1;
                command.QueryTotalLength += 4;

                command.Data    = new ushort[1];
                command.Data[0] = body.ReadUInt16BE();
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#10
0
        /// <summary>
        /// Helper for unpacking discrete data incoming as a bit-array
        /// </summary>
        /// <param name="command"></param>
        /// <param name="body"></param>
        internal static void PopDiscretes(
            ModbusCommand command,
            ByteArrayReader body)
        {
            var byteCount = body.ReadByte();

            var count = command.Count;
            command.Data = new ushort[count];
            command.QueryTotalLength += (byteCount + 1);

            int k = 0;
            while (body.EndOfBuffer == false)
            {
                if (command.Count <= k)
                    break;
                byte hb = body.CanRead(1) ? body.ReadByte() : (byte)0;
                byte lb = body.CanRead(1) ? body.ReadByte() : (byte)0;
                command.Data[k++] = (ushort)((hb << 8) | lb);
                //int n = count <= 8 ? count : 8;
                //count -= n;
                //for (int i = 0; i < n; i++)
                //    command.Data[k++] = (ushort)(cell & (1 << i));
            }
        }