/// <summary> /// 解包收到 ping指令 响应包 /// </summary> /// <param name="bArrBag"></param> /// <returns></returns> public static bool ParseBag_MainDeviceACK4PingResponse(byte[] bArrBag, ref DeviceAddress objDeviceAddress, out string strErrMsg) { bool bIfSucc = false; strErrMsg = ""; try { //55 AA 00 0B 92 06 01 01 00 00 01 00 00 00 00 57 从机PING应答 //数据位 byte bCode = bArrBag[10]; if (bCode == 0x01) { //解析源地址 //解析设备类型 byte bDeviceType = Convert.ToByte(bArrBag[5] & 0x7F); //01111111 objDeviceAddress.DeviceType = GetDeviceType(bDeviceType); //解析设备地址 byte[] bArrDeviceNo = new byte[2]; Array.Copy(bArrBag, 6, bArrDeviceNo, 0, bArrDeviceNo.Length); objDeviceAddress.OriginalDeviceNo = ScaleConverter.ByteArr2HexStr(bArrDeviceNo).Replace(" ", ""); objDeviceAddress.DeviceNo = string.Format("{0}0000", objDeviceAddress.OriginalDeviceNo); //拷贝数据包 objDeviceAddress.Buffer = new byte[bArrBag.Length]; Array.Copy(bArrBag, objDeviceAddress.Buffer, objDeviceAddress.Buffer.Length); bIfSucc = true; } } catch (Exception err) { strErrMsg = err.Message; } return(bIfSucc); }
/// <summary> /// 解包-ping响应包 /// </summary> /// <param name="bArrBag"></param> /// <returns></returns> public static bool ParseBag_PingResponse(byte[] bArrBag, ref DeviceAddress objDeviceAddress, out string strErrMsg) { bool bIfSucc = false; strErrMsg = ""; try { //55 FF 00 0D 93 01 00 00 00 00 06 12 34 00 00 41 98 FE 从机PING应答 //数据位 byte bCode = bArrBag[5]; if (bCode == 0x01) { //解析源地址 //解析设备类型 byte bDeviceType = Convert.ToByte(bArrBag[10] & 0x7F); //01111111 objDeviceAddress.DeviceType = GetDeviceType(bDeviceType); //解析设备地址 byte[] bArrDeviceNo = new byte[2]; Array.Copy(bArrBag, 11, bArrDeviceNo, 0, bArrDeviceNo.Length); objDeviceAddress.OriginalDeviceNo = ScaleConverter.ByteArr2HexStr(bArrDeviceNo).Replace(" ", ""); objDeviceAddress.DeviceNo = string.Format("{0}0000", objDeviceAddress.OriginalDeviceNo); int iBeginIndex = 15; ////解析系列号 byte[] bArrPingNum = new byte[2]; for (int i = bArrPingNum.Length - 1; i >= 0; i--) { bArrPingNum[i] = bArrBag[iBeginIndex++]; } objDeviceAddress.PingNum = Functions.ConverToInt(bArrPingNum);//低字节在前,高字节在后 //拷贝数据包 objDeviceAddress.Buffer = new byte[bArrBag.Length]; Array.Copy(bArrBag, objDeviceAddress.Buffer, objDeviceAddress.Buffer.Length); bIfSucc = true; } } catch (Exception err) { strErrMsg = err.Message; } return(bIfSucc); }
/// <summary> /// 解包刷卡响应包 /// </summary> /// <param name="bArrBag"></param> /// <returns></returns> public static bool ParseBag_SwipingCard(byte[] bArrBag, ref SwipingCardData objSwipingCardData, out string strErrMsg) { bool bIfSucc = false; strErrMsg = ""; try { //55 FF 00 12 20 01 00 00 00 00 06 12 34 00 01 F0 00 1D 00 9C 00 00 6D 刷卡头刷卡上传 //数据位 byte bCode = bArrBag[5]; if (bCode == 0x01) { //解析源地址 //解析设备类型 byte bDeviceType = Convert.ToByte(bArrBag[10] & 0x7F); //01111111 objSwipingCardData.DeviceType = GetDeviceType(bDeviceType); //解析设备地址 byte[] bArrDeviceNo = new byte[2]; Array.Copy(bArrBag, 11, bArrDeviceNo, 0, bArrDeviceNo.Length); objSwipingCardData.DeviceNo = ScaleConverter.ByteArr2HexStr(bArrDeviceNo).Replace(" ", ""); //解析卡类型 byte bCardType = bArrBag[15]; objSwipingCardData.CardType = GetCardType(bCardType); int iBeginIndex = 16; //解析卡号 byte[] bArrCardNo = new byte[4]; for (int i = bArrCardNo.Length - 1; i >= 0; i--) { bArrCardNo[i] = bArrBag[iBeginIndex++]; } objSwipingCardData.CardNo = Functions.ConverToUInt(bArrCardNo);//低字节在前,高字节在后 bIfSucc = true; } } catch (Exception err) { strErrMsg = err.Message; } return(bIfSucc); }
/// <summary> /// 回调方法-监控器接收时 /// </summary> /// <param name="strNotice"></param> /// <param name="bArrBuffer"></param> private void CommunicationForm_OnNotice(string strNotice, byte[] bArrBuffer) { if (!(string.IsNullOrEmpty(strNotice) || strNotice.Equals(""))) { string strContent = (RunVariable.IfDebug && bArrBuffer != null) ? string.Format("{0}:{1}", strNotice, ScaleConverter.ByteArr2HexStr(bArrBuffer)) : strNotice; ShowNotice(string.Format("{0} {1}", DateTime.Now.ToString(Config.LongTimeFormat), strContent)); } }
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); }
/// <summary> /// 回调方法-配置器接收时 /// </summary> /// <param name="strNotice"></param> /// <param name="bArrBuffer"></param> private void SelChsForm_OnNotice(string strNotice, byte[] bArrBuffer) { if (!(string.IsNullOrEmpty(strNotice) || strNotice.Equals(""))) { string strContent = (RunVariable.IfDebug && bArrBuffer != null) ? string.Format("{0}:{1}", strNotice, ScaleConverter.ByteArr2HexStr(bArrBuffer)) : strNotice; ShowSendOrReceiveMessage(strContent); } }
private void OnMainDeviceACK4PingResponse(DeviceAddress objDeviceAddress, string strErrInfo) { if (!(objDeviceAddress == null || !string.IsNullOrEmpty(strErrInfo))) { if (this._LastDeviceAddress != null) { if (this._LastDeviceAddress.DeviceNo.Equals(objDeviceAddress.DeviceNo) && this._LastDeviceAddress.DeviceType.Equals(objDeviceAddress.DeviceType)) { //2017-07-13 11:14:17 接收从机链路层ACK指令:55 AA 00 0B 92 06 01 01 00 00 01 00 00 00 00 57 string strContent = string.Format("接收从机链路层ACK指令"); strContent = (RunVariable.IfDebug && objDeviceAddress.Buffer != null) ? string.Format("{0}:{1}", strContent, ScaleConverter.ByteArr2HexStr(objDeviceAddress.Buffer)) : strContent; ShowNotice(string.Format("{0} {1}", DateTime.Now.ToString(Config.LongTimeFormat), strContent)); //this._LastDeviceAddress = null; timer1.Enabled = false; } } } }
private void OnPingResponse(DeviceAddress objDeviceAddress, string strErrInfo) { if (!(objDeviceAddress == null || !string.IsNullOrEmpty(strErrInfo))) { if (this._LastDeviceAddress != null) { if (this._LastDeviceAddress.DeviceNo.Equals(objDeviceAddress.DeviceNo) && this._LastDeviceAddress.DeviceType.Equals(objDeviceAddress.DeviceType) && this._LastDeviceAddress.PingNum.Equals(objDeviceAddress.PingNum)) { string strContent = string.Format("ping {0}{1}成功 !", GetDeviceTypeDesc(objDeviceAddress.DeviceType), objDeviceAddress.DeviceNo); strContent = (RunVariable.IfDebug && objDeviceAddress.Buffer != null) ? string.Format("{0}:{1}", strContent, ScaleConverter.ByteArr2HexStr(objDeviceAddress.Buffer)) : strContent; ShowNotice(string.Format("{0} {1}", DateTime.Now.ToString(Config.LongTimeFormat), strContent)); this._LastDeviceAddress = null; //timer1.Enabled = false; } } } }
/// <summary> /// 解包(读卡响应包) /// </summary> /// <param name="bArrBag"></param> /// <returns></returns> public static bool ParseBag_ReadCard(byte[] bArrBag, ref CardData objCardData, out string strErrMsg) { bool bIfSucc = false; objCardData = new CardData(); strErrMsg = ""; try { //数据位 byte bCode = bArrBag[5]; strErrMsg = GetFaultInfoByCode(bCode); if (strErrMsg.Equals("")) { //解析卡号 int iBeginIndex = 6; byte[] bArrCardNo = new byte[4]; for (int i = bArrCardNo.Length - 1; i >= 0; i--) { bArrCardNo[i] = bArrBag[iBeginIndex++]; } objCardData.CardNo = Functions.ConverToUInt(bArrCardNo);//低字节在前,高字节在后 //空卡直接返回 年月日为零则是空卡 if (int.Parse(bArrBag[15].ToString()) + int.Parse(bArrBag[16].ToString()) + int.Parse(bArrBag[17].ToString()) + int.Parse(bArrBag[18].ToString()) == 0) { objCardData.CardType = 6; } else { //解析区编码 objCardData.AreaCode = Convert.ToInt32(ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++])); //解析栋编码 objCardData.BuildCode = Convert.ToInt32(ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++])); //解析单元编码 objCardData.UnitCode = Convert.ToInt32(ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++])); //解析楼层编码 int iFloorCode = Convert.ToInt32(ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++])); //解析房间编码 objCardData.RoomCode = (iFloorCode * 100) + Convert.ToInt32(ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++])); //解析日期 //解析年 string strYear = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]) + ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); //解析月 string strMonth = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); //解析日 string strDay = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); objCardData.ExpiryDate = Functions.ConvertToUnixTime(Convert.ToDateTime(string.Format("{0}-{1}-{2} 00:00:00", strYear, strMonth, strDay))); //解析卡片类型 objCardData.CardType = Convert.ToInt32(bArrBag[iBeginIndex++]); //解析预留 6个字节 //iBeginIndex += 6; //新卡预留信息为开卡时间与及当前写扇区,旧卡信息为空 //bArrBag[iBeginIndex].ToString(); string strBeginYear = "20" + ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); string strBeginMonth = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); string strBeginDay = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); string strBeginHour = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); string strBeginMin = ScaleConverter.Byte2HexStr(bArrBag[iBeginIndex++]); string chsStr = bArrBag[iBeginIndex++].ToString(); //月份为00表示旧卡 if (strBeginMonth.CompareTo("00") != 0 && strBeginMonth.CompareTo("0") != 0) { objCardData.BeginDate = Functions.ConvertToUnixTime(Convert.ToDateTime(string.Format("{0}-{1}-{2} {3}:{4}:00", strBeginYear, strBeginMonth, strBeginDay, strBeginHour, strBeginMin))); } //扇区00也表示旧卡, 防止乱写情况还是做个判断 if (chsStr.CompareTo("00") != 0 && chsStr.CompareTo("0") != 0) { objCardData.listChsInfo[1].Add(Convert.ToInt32(chsStr)); } else { objCardData.listChsInfo[1].Add(1); } //解析序号 16个字节 byte[] bArrSerialNo = new byte[16]; Array.Copy(bArrBag, iBeginIndex, bArrSerialNo, 0, bArrSerialNo.Length); objCardData.SerialNo = ScaleConverter.ByteArr2HexStr(bArrSerialNo).Replace(" ", ""); } bIfSucc = true; } } catch (Exception err) { strErrMsg = err.Message; } return(bIfSucc); }