Example #1
0
        protected override void BtnOK_Click(object sender, EventArgs e)
        {
            string strDeviceAddress          = txtDeviceAddress.Text.Trim();
            string strOringinalDeviceAddress = strDeviceAddress;

            if (strDeviceAddress.Equals(""))
            {
                txtDeviceAddress.Focus();
                CMessageBox.ShowError(string.Format("设备地址必须填写,且必须是16进制字符!"), Config.DialogTitle);
                return;
            }
            if (strDeviceAddress.Length > 8)
            {
                strDeviceAddress = strDeviceAddress.Substring(0, 8);
            }
            strDeviceAddress = strDeviceAddress.PadRight(8, '0');

            if (!CheckDeviceAddress(strDeviceAddress))
            {
                txtDeviceAddress.Focus();
                CMessageBox.ShowError(string.Format("设备地址必须填写,且必须是16进制字符!"), Config.DialogTitle);
                return;
            }
#if DEBUG
            _CurrentPingNum = 1;
#else
            this._CurrentPingNum++;
#endif
            if (this._CurrentPingNum < 0 || this._CurrentPingNum > 9999)
            {
                this._CurrentPingNum = 0;
            }
            DeviceAddress objDeviceAddress = new DeviceAddress()
            {
                DeviceNo         = strDeviceAddress,
                OriginalDeviceNo = strOringinalDeviceAddress,
                PingNum          = this._CurrentPingNum,
                DeviceType       = Convert.ToInt32(((ComboBoxItem)cbDeviceType.SelectedItem).Value)
            };

            if (!CardMonitor.GetInstance().Ping(objDeviceAddress, out string strErrInfo))
            {
                Tools.CMessageBox.ShowError(string.Format("对不起,ping设备失败,错误原因:\r\n{0}!", strErrInfo), Config.DialogTitle);
            }
            else
            {
                //浅表副本 复制给定时任务 用于失败重试
                _LastDeviceAddress = (DeviceAddress)objDeviceAddress.Clone();
                timer1.Enabled     = true;
            }
        }
Example #2
0
        private bool ProcessOne(byte[] bufFrame, out string strErrMsg)
        {
            bool bIfSucc = false;

            strErrMsg = "";
            Package.ECommandType eResponseType = Package.GetResponseType(bufFrame, out strErrMsg);
            switch (eResponseType)
            {
            case Package.ECommandType.SwipingCard:
                SwipingCardData objSwipingCardData = new SwipingCardData();
                IList <string>  listResponseData   = new List <string>();
                bIfSucc = Package.ParseBag_SwipingCard(bufFrame, ref objSwipingCardData, out strErrMsg);
                if (bIfSucc)
                {
                    SwipingCardData objSent = (objSwipingCardData != null) ? (SwipingCardData)objSwipingCardData.Clone() : null;
                    _OnReceiveSwipingCard?.Invoke(objSent, strErrMsg);

                    DoNotice(string.Format("接收刷卡数据正确"), bufFrame);

                    //保存记录
                    SaveSwipingCardData(objSent);

                    //响应
                    byte[] bArrTargetAddress  = new byte[5];
                    byte[] bArrOrigialAddress = new byte[5];
                    Array.Copy(bufFrame, 5, bArrTargetAddress, 0, bArrTargetAddress.Length);
                    Array.Copy(bufFrame, 10, bArrOrigialAddress, 0, bArrOrigialAddress.Length);

                    //55 AA 00 0B 20 01 00 00 00 00 06 12 34 00 01 1E  主机链路层ACK
                    byte[] bArrBag = Package.BuildBag_MainDeviceACK(bArrTargetAddress, bArrOrigialAddress);
                    bIfSucc = SendCommand(bArrBag, out strErrMsg);
                    DoNotice(string.Format("{0}{1}", "发送主机链路层ACK指令", (bIfSucc ? "成功" : "失败")), bArrBag);

                    /*
                     * Thread.Sleep(50);//停顿数50毫秒
                     *
                     * //55 FF 00 0C 21 06 12 34 00 01 01 00 00 00 00 00 xx 主机应答
                     * byte[] bArrBag2 = Package.BuildBag_MainDeviceResponse(bArrTargetAddress, bArrOrigialAddress);
                     * bIfSucc = SendCommand(bArrBag2, out strErrMsg);
                     * DoNotice(string.Format("{0}{1}", "发送主机应答指令", (bIfSucc ? "成功" : "失败")), bArrBag2);
                     * if (!(bArrBag2 == null || bArrBag2.Length == 0))
                     * {
                     *  this._LastReSendTime = DateTime.Now;
                     *  this._RetryCommandList.Add(new Model.RetryCommand(ScaleConverter.ByteArr2HexStr(bArrBag2).Replace(" ", ""), 1));
                     * }
                     * //55 AA 00 0B 21 06 12 34 00 01 01 00 00 00 00 XX 刷卡头链路层ACK
                     */

                    bIfSucc = true;
                }
                break;

            case Package.ECommandType.CardHeadDeviceACK:
                try
                {
                    if (!(this._RetryCommandList == null || this._RetryCommandList.Count == 0))
                    {
                        string strBuffHexStr    = "";
                        string strCommandHexStr = "";
                        for (int i = this._RetryCommandList.Count - 1; i >= 0; i--)
                        {
                            Model.RetryCommand objCommand = (Model.RetryCommand) this._RetryCommandList[i];
                            if (objCommand != null)
                            {
                                strCommandHexStr = objCommand.CommandHexStr.Substring(10, 20);
                                strBuffHexStr    = ScaleConverter.ByteArr2HexStr(bufFrame).Replace(" ", "").Substring(10, 20);
                                if (string.Compare(strCommandHexStr, strBuffHexStr) == 0)
                                {
                                    this._RetryCommandList.RemoveAt(i);
                                }
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    strErrMsg = err.Message;
                }
                DoNotice(string.Format("接收刷卡头链路层ACK指令"), bufFrame);
                bIfSucc = true;
                break;

            case Package.ECommandType.PingResponse:
                DeviceAddress objDeviceAddress = new DeviceAddress();
                bIfSucc = Package.ParseBag_PingResponse(bufFrame, ref objDeviceAddress, out strErrMsg);
                if (bIfSucc)
                {
                    DeviceAddress objSent = (objDeviceAddress != null) ? (DeviceAddress)objDeviceAddress.Clone() : null;
                    _OnPingResponse?.Invoke(objSent, strErrMsg);
                    //响应
                    byte[] bArrTargetAddress  = new byte[5];
                    byte[] bArrOrigialAddress = new byte[5];
                    Array.Copy(bufFrame, 5, bArrTargetAddress, 0, bArrTargetAddress.Length);
                    Array.Copy(bufFrame, 10, bArrOrigialAddress, 0, bArrOrigialAddress.Length);

                    //Thread.Sleep(50);//停顿数50毫秒

                    //55 FF 00 0D 93 01 00 00 00 00 06 12 34 00 00 41 98 FE  从机PING应答
                    //55 AA 00 0B 93 01 00 00 00 00 06 12 34 00 00 09 AA   主机链路层应答

                    byte[] bArrBag = Package.BuildBag_MainDeviceACK4Ping(bArrTargetAddress, bArrOrigialAddress);
                    bIfSucc = SendCommand(bArrBag, out strErrMsg);
                    //DoNotice(string.Format("{0}{1}", "发送主机链路层ACK指令", (bIfSucc ? "成功" : "失败")), bArrBag);

                    //_OnPingNotice?.Invoke(string.Format("发送Ping指令{0}", (bIfSucc ? "成功" : "失败")), bArrBag);
                    //if (_OnPingNotice != null) _OnPingNotice(string.Format("发送主机链路层ACK指令{0}", (bIfSucc ? "成功" : "失败")), bArrBag);
                }
                break;

            case Package.ECommandType.MainDeviceACK4PingResponse:
                DeviceAddress objDeviceAddress1 = new DeviceAddress();
                bIfSucc = Package.ParseBag_MainDeviceACK4PingResponse(bufFrame, ref objDeviceAddress1, out strErrMsg);
                if (bIfSucc)
                {
                    DeviceAddress objSent = (objDeviceAddress1 != null) ? (DeviceAddress)objDeviceAddress1.Clone() : null;
                    _OnMainDeviceACK4PingResponse?.Invoke(objSent, strErrMsg);
                }
                break;

            default:
                strErrMsg = "无效数据";
                bIfSucc   = false;
                break;
            }
            return(bIfSucc);
        }