Exemple #1
0
        public static void ModBus_CreatStruct(ref MB ModBus, byte[] Buf)
        {
            int i = 0;

            ModBus.ErrorFlag   = 0;
            ModBus.Start       = Buf[i++];
            ModBus.TargetAddr  = Buf[i++];
            ModBus.HostAddr    = Buf[i++];
            ModBus.MsgFlag     = Buf[i++];
            ModBus.MsgNum      = Buf[i++];
            ModBus.MsgLength   = Buf[i++];
            ModBus.MsgLength <<= 8;
            ModBus.MsgLength  |= Buf[i++];
            if (ModBus.MsgLength > 0)
            {
                ModBus.DataFlag     = Buf[i++];
                ModBus.DataLength   = Buf[i++];
                ModBus.DataLength <<= 8;
                ModBus.DataLength  |= Buf[i++];
                ModBus.Data         = new byte[ModBus.DataLength];
                try
                {
                    for (int n = 0; n < ModBus.DataLength; n++)
                    {
                        ModBus.Data[n] = Buf[i++];
                    }
                }
                catch { }
            }

            ModBus.XorValue = Buf[i++];
            ModBus.End      = Buf[i++];
        }
Exemple #2
0
 public static void ModBus_Create(ref MB ModBus, byte HostAddr, byte Target, byte StartFlag, byte EndFlag)
 {
     ModBus.HostAddr   = HostAddr;
     ModBus.TargetAddr = TargetAddr;
     ModBus.Start      = StartFlag;
     ModBus.End        = EndFlag;
 }
Exemple #3
0
        public static int ModBus_CreatBuf(ref MB ModBus, ref byte[] Buf)
        {
            int i = 0;

            if (ModBus.DataLength > 0)
            {
                ModBus.MsgLength = ModBus.DataLength + 3;
            }
            else
            {
                ModBus.MsgLength = 0;
            }

            Buf[i++] = ModBus.Start;
            Buf[i++] = ModBus.TargetAddr;
            Buf[i++] = ModBus.HostAddr;
            Buf[i++] = ModBus.MsgFlag;
            Buf[i++] = ModBus.MsgNum;
            Buf[i++] = (byte)(ModBus.MsgLength >> 8);
            Buf[i++] = (byte)(ModBus.MsgLength & 0xff);
            if (ModBus.MsgLength > 0)
            {
                Buf[i++] = (byte)(ModBus.DataFlag);
                Buf[i++] = (byte)(ModBus.DataLength >> 8);
                Buf[i++] = (byte)(ModBus.DataLength & 0xff);
                for (int n = 0; n < ModBus.DataLength; n++)
                {
                    Buf[i++] = ModBus.Data[n];
                }
            }
            ModBus.XorValue = Tools.Xor(Buf, i);
            Buf[i++]        = ModBus.XorValue;
            Buf[i++]        = EndFlag;
            return(i);
        }
Exemple #4
0
        public static bool CheakXor(MB ModBuf)
        {
            byte[] Buf = new byte[100];
            ModBus_CreatBuf(ref ModBuf, ref Buf);
            byte XorValue = Tools.Xor(Buf, ModBuf.MsgLength + 7);

            if (XorValue == ModBuf.XorValue)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }