Ejemplo n.º 1
0
        /// <summary>
        /// 生成批量写入寄存器的报文信息
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="values">实际值</param>
        /// <returns>包含结果对象的报文</returns>
        public OperateResult <byte[]> BuildWriteRegisterCommand(string address, byte[] values)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.WriteRegister);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            // 生成最终rtu指令
            byte[] buffer = ModbusInfo.PackCommandToRtu(analysis.Content.CreateWriteRegister(station, values));
            return(OperateResult.CreateSuccessResult(buffer));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成一个读取离散信息的指令头
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="length">长度</param>
        /// <returns>携带有命令字节</returns>
        public OperateResult <byte[]> BuildReadDiscreteCommand(string address, ushort length)
        {
            OperateResult <ModbusAddress> analysis = ModbusInfo.AnalysisAddress(address, isAddressStartWithZero, ModbusInfo.ReadDiscrete);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            // 生成最终tcp指令
            byte[] buffer = ModbusInfo.PackCommandToRtu(analysis.Content.CreateReadDiscrete(station, length));
            return(OperateResult.CreateSuccessResult(buffer));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 接收到串口数据的时候触发
        /// </summary>
        /// <param name="sender">串口对象</param>
        /// <param name="e">消息</param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int rCount = 0;

            byte[] buffer  = new byte[1024];
            byte[] receive = null;

            while (true)
            {
                System.Threading.Thread.Sleep(20);            // 此处做个微小的延时,等待数据接收完成
                int count = serialPort.Read(buffer, rCount, serialPort.BytesToRead);
                rCount += count;
                if (count == 0)
                {
                    break;
                }

                receive = new byte[rCount];
                Array.Copy(buffer, 0, receive, 0, count);
            }

            if (receive == null)
            {
                return;
            }

            if (receive.Length < 3)
            {
                LogNet?.WriteError(ToString(), $"Uknown Data:" + SoftBasic.ByteToHexString(receive, ' '));
                return;
            }

            if (Serial.SoftCRC16.CheckCRC16(receive))
            {
                byte[] modbusCore = SoftBasic.BytesArrayRemoveLast(receive, 2);

                if (!CheckModbusMessageLegal(modbusCore))
                {
                    // 指令长度验证错误,关闭网络连接
                    LogNet?.WriteError(ToString(), $"Receive Nosense Modbus-rtu : " + SoftBasic.ByteToHexString(receive, ' '));
                    return;
                }

                // 验证站号是否一致
                if (station >= 0 && station != modbusCore[0])
                {
                    LogNet?.WriteError(ToString(), $"Station not match Modbus-rtu : " + SoftBasic.ByteToHexString(receive, ' '));
                    return;
                }

                // LogNet?.WriteError( ToString( ), $"Success:" + BasicFramework.SoftBasic.ByteToHexString( receive, ' ' ) );
                // 需要回发消息
                byte[] copy = ModbusInfo.PackCommandToRtu(ReadFromModbusCore(modbusCore));

                serialPort.Write(copy, 0, copy.Length);

                if (IsStarted)
                {
                    RaiseDataReceived(receive);
                }
            }
            else
            {
                LogNet?.WriteWarn("CRC Check Failed : " + SoftBasic.ByteToHexString(receive, ' '));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 生成一个读取寄存器的指令头
 /// </summary>
 /// <param name="address">地址</param>
 /// <param name="length">长度</param>
 /// <returns>携带有命令字节</returns>
 private OperateResult <byte[]> BuildReadRegisterCommand(ModbusAddress address, ushort length)
 {
     // 生成最终rtu指令
     byte[] buffer = ModbusInfo.PackCommandToRtu(address.CreateReadRegister(station, length));
     return(OperateResult.CreateSuccessResult(buffer));
 }