Example #1
0
        public static int ModBus_Expend(byte[] Buf, int Length, ref MB ModBusMsg)
        {
            int i          = 0;
            int DataOffSet = 0;
            int Cheak;

            ModBusMsg.ErrorFlag = (byte)ModBus_SizeError;
            if (ModBusMsg.Start != Buf[i++])
            {
                return(0);
            }
            if (ModBusMsg.WorkMode == (byte)MasterSlaveMode.WorkMode_Master)//当协议运行为主机模式
            {
                if (ModBusMsg.MasterAddr != Buf[i++])
                {
                    return(0);
                }
                if (ModBusMsg.SlaveAddr != Buf[i++])
                {
                    ;
                }
            }
            else if (ModBusMsg.WorkMode == (byte)MasterSlaveMode.WorkMode_Slave)//当协议运行在从机模式
            {
                byte TempAddr = Buf[i++];
                if ((TempAddr != ModBusMsg.SlaveAddr) & (TempAddr != BroadAddr))
                {
                    return(0);
                }
                if (Buf[i++] != ModBusMsg.MasterAddr)
                {
                    return(0);
                }
            }
            ModBusMsg.MsgFlag     = Buf[i++];
            ModBusMsg.MsgNum      = Buf[i++];
            ModBusMsg.MsgLength   = Buf[i++];
            ModBusMsg.MsgLength <<= 8;
            ModBusMsg.MsgLength  |= Buf[i++];
            if (ModBusMsg.MsgLength > 0)
            {
                ModBusMsg.DataFlag     = Buf[i++];
                ModBusMsg.DataLength   = Buf[i++];
                ModBusMsg.DataLength <<= 8;
                ModBusMsg.DataLength  |= Buf[i++];
                DataOffSet             = i;
                i += ModBusMsg.DataLength;
                ModBusMsg.Data = new byte[ModBusMsg.DataLength];
                for (int n = 0; n < ModBusMsg.DataLength; n++)
                {
                    ModBusMsg.Data[n] = Buf[DataOffSet++];
                }
                if (ModBusMsg.MsgLength != (ModBusMsg.DataLength + 3))
                {
                    return(0);
                }
            }
            if (ModBusMsg.CheakMode == CheakMode_Xor)
            {
                Cheak = Tools.Xor(Buf, i);
                if (Cheak != Buf[i])
                {
                    return(0);
                }
                i++;//xor校验一位
                if (ModBusMsg.End != Buf[i++])
                {
                    return(0);
                }
                // if (i != (Length - 2)) ModBusMsg.ErrorFlag = ModBus_SizeError;//有可能尾部还有其他直接忽略
            }
            else if (ModBusMsg.CheakMode == CheakMode_Crc)
            {
                Cheak = Tools.GetCrc16(Buf, i);
                //MessageBox.Show(Cheak.ToString("X")); MessageBox.Show(Tools.ByteToInt16(Buf, i, 0).ToString("X"));
                if (Cheak != Tools.ByteToInt16(Buf, i, 0))
                {
                    return(0);
                }
                i += 2;//crc校验2位
                if (ModBusMsg.End != Buf[i++])
                {
                    return(0);
                }
                // if (i != (Length)) return 0;//有可能尾部还有其他直接忽略
            }
            if (ModBusMsg.End != Buf[ModBusMsg.MsgLength + 9])
            {
                return(0);
            }
            if (i != (ModBusMsg.MsgLength + 10))
            {
                return(0);
            }
            ModBusMsg.ErrorFlag = ModBus_Ok;
            return(DataOffSet);
        }