示例#1
0
        public static int SendData(SerialPort CommPort, byte[] TxBuffer, int TxLength, ref byte[] RxBuffer, int RxLength)
        {
            int Length = 0;

            if (!CommPort.IsOpen)
            {
                return(0);
            }
            MB TxModBus = new MB();
            MB RxModBus = new MB();

            ModBusClass.ModBusCoppyCreate(ref ModBusClass.DefMoBus, ref TxModBus);
            ModBusClass.ModBusCoppyCreate(ref ModBusClass.DefMoBus, ref RxModBus);
            for (int i = 0; i < 10; i++)
            {
                Length = SendDataOne(CommPort, TxBuffer, TxLength, ref RxBuffer, RxLength);
                ModBusClass.ModBusCoppyCreate(ref ModBusClass.DefMoBus, ref RxModBus);
                ModBusClass.ModBus_Expend(RxBuffer, Length, ref RxModBus);
                if (RxModBus.ErrorFlag == ModBusClass.ModBus_Ok)
                {
                    return(Length);
                }
                //  else MessageBox.Show(Tools.HexToString(RxBuffer, 100));
            }
            //  MessageBox.Show("重试次数过多");
            return(Length);
        }
示例#2
0
文件: ModBus.cs 项目: wpmyj/csharp
 public static void ModBus_Create(ref MB ModBusMsg, int HostAddr, int TargetAddr, MasterSlaveMode WorkMode, int CheakMode)
 {
     ModBusMsg.Start      = StartFlag;
     ModBusMsg.End        = EndFlag;
     ModBusMsg.ErrorFlag  = ModBus_FlagError;
     ModBusMsg.SlaveAddr  = (byte)TargetAddr;
     ModBusMsg.MasterAddr = (byte)HostAddr;
     ModBusMsg.CheakMode  = CheakMode;
     ModBusMsg.WorkMode   = (byte)WorkMode;
 }
示例#3
0
文件: ModBus.cs 项目: wpmyj/csharp
 public static void ModBusCoppyCreate(ref MB S, ref MB D)
 {
     D.Start      = S.Start;
     D.End        = S.End;
     D.ErrorFlag  = S.ErrorFlag;
     D.MasterAddr = S.MasterAddr;
     D.SlaveAddr  = S.SlaveAddr;
     D.WorkMode   = S.WorkMode;
     D.CheakMode  = S.CheakMode;
 }
示例#4
0
文件: ModBus.cs 项目: wpmyj/csharp
        public static int ModBus_CreateMsg(ref byte[] Buf, ref MB ModBusMsg, int MsgFlag, int MsgNum, int DataFlag, byte[] Data, int DataLength)
        {
            int i = 0;
            int Cheak;

            ModBusMsg.MsgFlag    = (byte)MsgFlag;
            ModBusMsg.MsgNum     = (byte)MsgNum;
            ModBusMsg.DataFlag   = (byte)DataFlag;
            ModBusMsg.DataLength = DataLength;
            if (DataLength > 0)
            {
                ModBusMsg.MsgLength = DataLength + 3;
            }
            else
            {
                ModBusMsg.MsgLength = 0;
            }
            Buf[i++] = (byte)ModBusMsg.Start;
            if (ModBusMsg.WorkMode == (byte)MasterSlaveMode.WorkMode_Master)//当协议运行为主机模式
            {
                Buf[i++] = ModBusMsg.SlaveAddr;
                Buf[i++] = ModBusMsg.MasterAddr;
            }
            else if (ModBusMsg.WorkMode == (byte)MasterSlaveMode.WorkMode_Slave)//当协议运行在从机模式
            {
                Buf[i++] = ModBusMsg.MasterAddr;
                Buf[i++] = ModBusMsg.SlaveAddr;
            }
            Buf[i++] = (byte)ModBusMsg.MsgFlag;
            Buf[i++] = (byte)ModBusMsg.MsgNum;
            Buf[i++] = (byte)(ModBusMsg.MsgLength >> 8);
            Buf[i++] = (byte)(ModBusMsg.MsgLength & 0xff);
            if (DataLength > 0)//当消息包含数据
            {
                Buf[i++] = (byte)(ModBusMsg.DataFlag);
                Buf[i++] = (byte)(ModBusMsg.DataLength >> 8);
                Buf[i++] = (byte)(ModBusMsg.DataLength & 0xff);
                Tools.BufferCoppy(Data, Buf, i, ModBusMsg.DataLength); i += ModBusMsg.DataLength;
            }
            if (ModBusMsg.CheakMode == CheakMode_Crc)
            {
                Cheak    = Tools.GetCrc16(Buf, i);
                Buf[i++] = (byte)(Cheak >> 8);
                Buf[i++] = (byte)(Cheak & 0xff);
            }
            else if (ModBusMsg.CheakMode == CheakMode_Xor)
            {
                Buf[i++] = Tools.Xor(Buf, i - 1);
            }
            Buf[i++] = ModBusMsg.End;
            return(i);
        }
示例#5
0
文件: ModBus.cs 项目: wpmyj/csharp
 public static void ModBus_Clear(ref MB ModBus)
 {
     ModBus.Data       = null;
     ModBus.DataFlag   = 0;
     ModBus.DataLength = 0;
     ModBus.End        = 0;
     ModBus.ErrorFlag  = 0;
     ModBus.MasterAddr = 0;
     ModBus.MsgFlag    = 0;
     ModBus.MsgLength  = 0;
     ModBus.MsgNum     = 0;
     ModBus.Start      = 0;
     ModBus.SlaveAddr  = 0;
     ModBus.XorValue   = 0;
     ModBus.CrcValue   = 0;
     ModBus.CheakMode  = 0;
 }
示例#6
0
        public static int SendData(SerialPort CommPort, byte[] TxBuffer, int TxLength, ref byte[] RxBuffer, int RxLength)
        {
            int Length = 0;

            MB TxModBus = new MB();
            MB RxModBus = new MB();

            ModBusClass.ModBusCoppyCreate(ref ModBusClass.DefMoBus, ref TxModBus);
            ModBusClass.ModBusCoppyCreate(ref ModBusClass.DefMoBus, ref RxModBus);
            for (int i = 0; i < 20; i++)
            {
                Length = SendDataOne(CommPort, TxBuffer, TxLength, ref RxBuffer, RxLength);
                ModBusClass.ModBusCoppyCreate(ref ModBusClass.DefMoBus, ref RxModBus);
                ModBusClass.ModBus_Expend(RxBuffer, Length, ref RxModBus);
                if (RxModBus.ErrorFlag == ModBusClass.ModBus_Ok)
                {
                    return(Length);
                }
            }
            return(Length);
        }
示例#7
0
文件: ModBus.cs 项目: wpmyj/csharp
        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++])
            {
                ModBusMsg.ErrorFlag = ModBus_FlagError; return(0);
            }
            if (ModBusMsg.WorkMode == (byte)MasterSlaveMode.WorkMode_Master)//当协议运行为主机模式
            {
                if (ModBusMsg.MasterAddr != Buf[i++])
                {
                    ModBusMsg.ErrorFlag = ModBus_AddrError; 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))
                {
                    ModBusMsg.ErrorFlag = ModBus_AddrError; return(0);
                }
                if (Buf[i++] != ModBusMsg.MasterAddr)
                {
                    ModBusMsg.ErrorFlag = ModBus_AddrError; 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))
                {
                    ModBusMsg.ErrorFlag = ModBus_SizeError;
                }
            }
            if (ModBusMsg.CheakMode == CheakMode_Xor)
            {
                Cheak = Tools.Xor(Buf, i);
                if (Cheak != Buf[i])
                {
                    ModBusMsg.ErrorFlag = ModBus_CheakError;
                }
                else
                {
                    ModBusMsg.ErrorFlag = ModBus_Ok;
                } i++;                                   //xor校验一位
                if (ModBusMsg.End != Buf[i++])
                {
                    ModBusMsg.ErrorFlag = ModBus_FlagError; 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))
                {
                    ModBusMsg.ErrorFlag = ModBus_CheakError;
                }
                else
                {
                    ModBusMsg.ErrorFlag = ModBus_Ok;
                } i += 2;                                    //crc校验2位
                if (ModBusMsg.End != Buf[i++])
                {
                    ModBusMsg.ErrorFlag = ModBus_FlagError; return(0);
                }
                //if (i != (Length - 3)) ModBusMsg.ErrorFlag = ModBus_SizeError;//有可能尾部还有其他直接忽略
            }
            return(DataOffSet);
        }