Exemplo n.º 1
0
 /// <summary>
 /// 重发指令
 /// </summary>
 private void ReSendCommand()
 {
     if (!(this._RetryCommandList == null || this._RetryCommandList.Count == 0))
     {
         bool     bIfSucc = false;
         TimeSpan tSpan   = DateTime.Now - this._LastReSendTime;
         if (tSpan.TotalSeconds >= this._ReSendSeconds)
         {
             try
             {
                 for (int i = this._RetryCommandList.Count - 1; i >= 0; i--)
                 {
                     Model.RetryCommand objCommand = (Model.RetryCommand) this._RetryCommandList[i];
                     if (objCommand != null)
                     {
                         if (objCommand.TryTimes < this._ReSendMaxTimes)
                         {
                             byte[] bArrBag = ScaleConverter.HexStr2ByteArr(objCommand.CommandHexStr);
                             bIfSucc = SendCommand(bArrBag, out string strErrMsg);
                             DoNotice(string.Format("{0}{1}(重发第{2}次)", "发送主机应答指令", (bIfSucc ? "成功" : "失败"), objCommand.TryTimes), bArrBag);
                             objCommand.TryTimes += 1;
                             if (objCommand.TryTimes >= this._ReSendMaxTimes)
                             {
                                 this._RetryCommandList.RemoveAt(i);
                             }
                             else
                             {
                                 this._RetryCommandList[i] = objCommand.Clone();
                             }
                         }
                         else
                         {
                             this._RetryCommandList.RemoveAt(i);
                         }
                     }
                     else
                     {
                         this._RetryCommandList.RemoveAt(i);
                     }
                 }
                 this._LastReSendTime = DateTime.Now;
             }
             catch (Exception err)
             {
                 string strErr = err.Message;
             }
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 创建包-ping指令
        /// </summary>
        /// <param name="bArrTargetAddress">目的地址5个字节</param>
        /// <returns></returns>
        private static byte[] BuildBag_Ping(DeviceAddress objDeviceAddress)
        {
            if (objDeviceAddress == null || string.IsNullOrEmpty(objDeviceAddress.DeviceNo))
            {
                return(new byte[0]);
            }

            byte[] arrRtn = new byte[18];
            int    iIndex = 0;

            //帧起始符 高位
            arrRtn[iIndex++] = 0x55;
            //帧起始符 低位
            arrRtn[iIndex++] = 0xFF;
            //数据长度 高位
            arrRtn[iIndex++] = 0x00;
            //数据长度 低位
            arrRtn[iIndex++] = 0x0D;
            //命令字
            arrRtn[iIndex++] = 0x92;

            //目的地址 5字节
            arrRtn[iIndex++] = GetDeviceTypeByte(objDeviceAddress.DeviceType);
            byte[] bArrDeviceNo = ScaleConverter.HexStr2ByteArr(objDeviceAddress.DeviceNo);
            for (int i = 0; i < 4; i++)
            {
                arrRtn[iIndex++] = bArrDeviceNo[i];
            }
            //源地址 5字节
            arrRtn[iIndex++] = 0x01;
            arrRtn[iIndex++] = 0x00;
            arrRtn[iIndex++] = 0x00;
            arrRtn[iIndex++] = 0x00;
            arrRtn[iIndex++] = 0x00;

            //序号 2字节 BCD码:0~9999(每次序号不一样)
            byte[] bArrPingNum = ScaleConverter.Int2ByteArr(objDeviceAddress.PingNum);
            arrRtn[iIndex++] = bArrPingNum[1];
            arrRtn[iIndex++] = bArrPingNum[0];
            //CRC8校验
            arrRtn[iIndex++] = CRC8A.GetCRC8(arrRtn, 0, arrRtn.Length - 1);
            return(arrRtn);
        }
Exemplo n.º 3
0
        private bool CheckDeviceAddress(string strDeviceAddress)
        {
            bool bIfOk = false;

            if (!string.IsNullOrEmpty(strDeviceAddress))
            {
                strDeviceAddress = strDeviceAddress.Trim();
                if (!strDeviceAddress.Equals(""))
                {
                    try
                    {
                        byte[] bArrDeviceAddress = ScaleConverter.HexStr2ByteArr(strDeviceAddress);
                        if (bArrDeviceAddress.Length >= 1 && bArrDeviceAddress.Length <= 4)
                        {
                            bIfOk = true;
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return(bIfOk);
        }
Exemplo n.º 4
0
        private bool CheckSerialNo(string strSerialNo)
        {
            bool bIfOk = false;

            if (!string.IsNullOrEmpty(strSerialNo))
            {
                strSerialNo = strSerialNo.Trim();
                if (!strSerialNo.Equals("") && strSerialNo.Length == 32)
                {
                    try
                    {
                        byte[] bArrSerialNo = ScaleConverter.HexStr2ByteArr(strSerialNo);
                        if (bArrSerialNo.Length == 16)
                        {
                            bIfOk = true;
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return(bIfOk);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 创建发送包
        /// </summary>
        /// <param name="CommandType">指令类型</param>
        /// <param name="objCard">写卡指令专用</param>
        /// <param name="dt">时间,同步时间专用</param>
        /// <param name="objDeviceAddress">地址,Ping指令专用</param>
        /// <param name="chs">扇区重置,写卡专用</param>
        /// <returns></returns>
        public static byte[] BuildBag(ECommandType CommandType, Card objCard = null, String chs = null, DateTime?dt = null, DeviceAddress objDeviceAddress = null)
        {
            byte[] arrRtn = new byte[0];
            byte[] arrTemp;
            switch (CommandType)
            {
            case ECommandType.WriteCard_Send:
                arrTemp = BuildBag_WriteCard(objCard);
                if (arrTemp.Length > 0)
                {
                    arrRtn = new byte[arrTemp.Length];
                    Array.Copy(arrTemp, arrRtn, arrRtn.Length);
                }
                break;

            case ECommandType.ReadCard_Send:
                arrRtn  = new byte[6];
                arrTemp = ScaleConverter.HexStr2ByteArr("55 FF 00 01 34");
                Array.Copy(arrTemp, arrRtn, arrTemp.Length);
                arrRtn[5] = CRC8A.GetCRC8(arrTemp);
                break;

            case ECommandType.Ping:
                if (objDeviceAddress != null)
                {
                    arrTemp = BuildBag_Ping(objDeviceAddress);
                    if (arrTemp.Length > 0)
                    {
                        arrRtn = new byte[arrTemp.Length];
                        Array.Copy(arrTemp, arrRtn, arrRtn.Length);
                    }
                }
                break;

            case ECommandType.SyncTime_Send:    //同步时间
                if (dt != null)
                {
                    byte[] arrRtnTemp2 = BuildBag_SyncTime(Convert.ToDateTime(dt));
                    if (arrRtnTemp2.Length > 0)
                    {
                        arrRtn = new byte[arrRtnTemp2.Length];
                        Array.Copy(arrRtnTemp2, arrRtn, arrRtn.Length);
                    }
                }
                break;

            case ECommandType.ChsSel:    //扇区查询
                arrTemp = BuildBag_ChsSel();
                if (arrTemp.Length > 0)
                {
                    arrRtn = new byte[arrTemp.Length];
                    Array.Copy(arrTemp, arrRtn, arrRtn.Length);
                }
                break;

            case ECommandType.ChsClean:    //扇区重置
                arrTemp = BuildBag_ChsClean(chs);
                if (arrTemp.Length > 0)
                {
                    arrRtn = new byte[arrTemp.Length];
                    Array.Copy(arrTemp, arrRtn, arrRtn.Length);
                }
                break;
            }
            return(arrRtn);
        }