Пример #1
0
        /// <summary>
        /// 检查当前的Modbus-Rtu响应是否是正确的
        /// </summary>
        /// <param name="send">发送的数据信息</param>
        /// <returns>带是否成功的结果数据</returns>
        protected virtual OperateResult <byte[]> CheckModbusTcpResponse(byte[] send)
        {
            // 核心交互
            OperateResult <byte[]> result = ReadBase(send);

            if (!result.IsSuccess)
            {
                return(result);
            }

            // 长度校验
            if (result.Content.Length < 5)
            {
                return(new OperateResult <byte[]>(StringResources.Language.ReceiveDataLengthTooShort + "5"));
            }

            // 检查crc
            if (!SoftCRC16.CheckCRC16(result.Content))
            {
                return(new OperateResult <byte[]>(StringResources.Language.ModbusCRCCheckFailed));
            }

            // 发生了错误
            if ((send[1] + 0x80) == result.Content[1])
            {
                return(new OperateResult <byte[]>(result.Content[2], ModbusInfo.GetDescriptionByErrorCode(result.Content[2])));
            }

            // 移除CRC校验
            byte[] buffer = new byte[result.Content.Length - 2];
            Array.Copy(result.Content, 0, buffer, 0, buffer.Length);
            return(OperateResult.CreateSuccessResult(buffer));
        }
Пример #2
0
        /// <summary>
        /// 重写的数据接收方法,需要连续接收,直到CRC校验成功或是超时,接收时间会比较久
        /// </summary>
        /// <param name="sender">串口对象</param>
        /// <param name="e">串口的数据对象</param>
        protected override void SP_ReadData_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (true)
            {
                Thread.Sleep(40);

                try
                {
                    if (SP_ReadData.BytesToRead < 1)
                    {
                        continue;
                    }
                    // 继续接收数据
                    receiveCount += SP_ReadData.Read(buffer, receiveCount, SP_ReadData.BytesToRead);

                    // CRC校验成功及退出
                    byte[] data = new byte[receiveCount];
                    Array.Copy(buffer, 0, data, 0, receiveCount);
                    if (SoftCRC16.CheckCRC16(data))
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    isComError  = true;
                    ComErrorMsg = ex.Message;
                    break;
                }
            }
            resetEvent.Set( );
        }
Пример #3
0
        /// <summary>
        /// 检查当前的Modbus-Tcp响应是否是正确的
        /// </summary>
        /// <param name="send">发送的数据信息</param>
        /// <returns>带是否成功的结果数据</returns>
        private OperateResult <byte[]> CheckModbusTcpResponse(byte[] send)
        {
            OperateResult <byte[]> result = ReadBase(send);

            if (!result.IsSuccess)
            {
                return(result);
            }

            if (result.Content.Length < 5)
            {
                return(new OperateResult <byte[]>( )
                {
                    IsSuccess = false,
                    Message = "接收数据长度不能小于5",
                });
            }

            if (!SoftCRC16.CheckCRC16(result.Content))
            {
                return(new OperateResult <byte[]>( )
                {
                    IsSuccess = false,
                    Message = "CRC校验失败",
                });
            }

            if ((send[1] + 0x80) == result.Content[1])
            {
                // 发生了错误
                return(new OperateResult <byte[]>( )
                {
                    IsSuccess = false,
                    Message = ModbusInfo.GetDescriptionByErrorCode(result.Content[2]),
                    ErrorCode = result.Content[2],
                });
            }
            else
            {
                // 移除CRC校验
                byte[] buffer = new byte[result.Content.Length - 2];
                Array.Copy(result.Content, 0, buffer, 0, buffer.Length);
                return(OperateResult.CreateSuccessResult(buffer));
            }
        }
Пример #4
0
        /// <summary>
        /// 检查当前的Modbus-Rtu响应是否是正确的
        /// </summary>
        /// <param name="send">发送的数据信息</param>
        /// <returns>带是否成功的结果数据</returns>
        protected virtual OperateResult <byte[]> CheckModbusTcpResponse(byte[] send)
        {
            // 核心交互
            OperateResult <byte[]> result = ReadBase(send);

            if (!result.IsSuccess)
            {
                return(result);
            }

            // 长度校验
            if (result.Content.Length < 5)
            {
                return new OperateResult <byte[]>( )
                       {
                           IsSuccess = false, Message = "接收数据长度不能小于5"
                       }
            }
            ;

            // 检查crc
            if (!SoftCRC16.CheckCRC16(result.Content))
            {
                return new OperateResult <byte[]>( )
                       {
                           Message = "CRC校验失败"
                       }
            }
            ;

            // 发生了错误
            if ((send[1] + 0x80) == result.Content[1])
            {
                return new OperateResult <byte[]>( )
                       {
                           ErrorCode = result.Content[2], Message = ModbusInfo.GetDescriptionByErrorCode(result.Content[2])
                       }
            }
            ;

            // 移除CRC校验
            byte[] buffer = new byte[result.Content.Length - 2];
            Array.Copy(result.Content, 0, buffer, 0, buffer.Length);
            return(OperateResult.CreateSuccessResult(buffer));
        }
Пример #5
0
 /// <summary>
 /// 检查当前接收的字节数据是否正确的
 /// </summary>
 /// <param name="rBytes"></param>
 /// <returns></returns>
 protected override bool CheckReceiveBytes(byte[] rBytes)
 {
     return(SoftCRC16.CheckCRC16(rBytes));
 }