Exemplo n.º 1
0
        /// <summary>处理指令</summary>
        /// <param name="entity">指令实体</param>
        /// <param name="expect">预期返回数据长度</param>
        /// <returns></returns>
        ModbusEntity Process(ModbusEntity entity, Int32 expect)
        {
            if (Transport == null)
            {
                throw new NullReferenceException("Transport");
            }

            entity.Host = Host;

            // 发送
            var buf = entity.ToArray();

            if (Debug)
            {
                WriteLine(entity.Function + " :" + buf.ToHex());
            }

            // Modbus加锁,防止冲突
            lock (this)
            {
                // 预期返回指令长度,传入参数expect没有考虑头部和校验位
                //var st = Transport as SerialTransport;
                //if (st != null) st.FrameSize = expect + ModbusEntity.NO_DATA_LENGTH;
                Transport.Send(buf);

                // lscy 2013-7-29
                // 发送后,休眠一段时间,避免设备数据未全部写到串口缓冲区中
                // 一般情况下,100ms 已足够
                if (Delay > 0)
                {
                    Thread.Sleep(Delay);
                }

                // 读取
                var pk = Transport.Receive();
                if (pk == null || pk.Count == 0)
                {
                    return(null);
                }

                if (Debug)
                {
                    WriteLine(new String(' ', entity.Function.ToString().Length) + "=>" + pk.ToHex());
                }

                var rs = new ModbusEntity().Parse(pk.Data, pk.Offset, pk.Count);
                if (rs == null)
                {
                    return(null);
                }
                if (rs.IsException)
                {
                    throw new ModbusException(rs.Data != null && rs.Data.Length > 0 ? (Errors)rs.Data[0] : (Errors)0);
                }

                return(rs);
            }
        }
Exemplo n.º 2
0
        /// <summary>处理Modbus消息</summary>
        /// <param name="pk"></param>
        /// <returns></returns>
        public virtual Packet Process(Packet pk)
        {
#if DEBUG
            var buf = pk.Data;
            var str = "Request :";
            for (var i = 0; i < buf.Length; i++)
            {
                str += " " + buf[i].ToString("X2");
            }
            WriteLine(str);
#endif

            // 处理
            var entity = new ModbusEntity().Parse(pk.ReadBytes());
            // 检查主机
            if (entity.Host != 0 && entity.Host != Host)
            {
                return(null);
            }
            // 检查Crc校验
            var crc = pk.Data.Crc(pk.Offset, pk.Total - 2);
            if (crc != entity.Crc)
            {
                entity.SetError(Errors.CrcError);
            }
            else
            {
                entity = Process(entity);
            }
            pk = entity.ToArray();

#if DEBUG
            str = "Response:";
            for (var i = 0; i < buf.Length; i++)
            {
                str += " " + buf[i].ToString("X2");
            }
            WriteLine(str);
            WriteLine("");
#endif
            return(pk);
        }