/// <summary> /// 判断时间 误差大于1分钟 校时 /// </summary> /// <param name="ReceBuff"></param> private void JudgeDatetime(byte[] ReceBuff) { int iYear = ConvertBCDToInt(ReceBuff[5]); iYear += 2000; int iMonth = ConvertBCDToInt(ReceBuff[6]); int iDay = ConvertBCDToInt(ReceBuff[7]); int iHour = ConvertBCDToInt(ReceBuff[9]); int iMinute = ConvertBCDToInt(ReceBuff[10]); int iSecond = ConvertBCDToInt(ReceBuff[11]); DateTime dtRece = new DateTime(iYear, iMonth, iDay, iHour, iMinute, iSecond); TimeSpan ts = DateTime.Now - dtRece;//记录历史温湿度数据间隔 if (ts.TotalSeconds >= 59 || ts.TotalSeconds <= -59) { DateTime dtNow = DateTime.Now; bSendDoor[1] = MainControl.bDoorAddrL; bSendDoor[2] = MainControl.bDoorAddrH; bSendDoor[3] = 0x8B; bSendDoor[4] = 0x10; int i = dtNow.Year; if (i >= 2000) { i -= 2000; } bSendDoor[5] = ConvertBCD((byte)i); bSendDoor[6] = ConvertBCD((byte)(dtNow.Month)); bSendDoor[7] = ConvertBCD((byte)(dtNow.Day)); byte b = ConverWeekToInt(dtNow.DayOfWeek.ToString()); bSendDoor[8] = ConvertBCD(b);//周 bSendDoor[9] = ConvertBCD((byte)(dtNow.Hour)); bSendDoor[10] = ConvertBCD((byte)(dtNow.Minute)); bSendDoor[11] = ConvertBCD((byte)(dtNow.Second)); int iCheckSun = 0; for (i = 1; i < 31; i++) { iCheckSun += bSendDoor[i]; } bSendDoor[31] = (byte)(iCheckSun & 0x00ff); bSendDoor[32] = (byte)(iCheckSun >> 8); //txt 发送 string strContent = SerialPortUtil.ByteToHex(bSendDoor); txtLog.WriteTxt("Door.txt", "Time Send", strContent, false); byte[] bRece = new byte[34]; int iLength = doorSerial.SendReceByte(bSendDoor, ref bRece, 500); //txt 接受 if (iLength > 0) { strContent = SerialPortUtil.ByteToHex(bRece); txtLog.WriteTxt("Door.txt", "Time Rece", strContent, true);//01 01 01 0F 11 8C } if (iLength == 34) { iCheckSun = 0; for (i = 1; i < 31; i++) { iCheckSun += ReceBuff[i]; } byte bL = (byte)(iCheckSun & 0x00ff); byte bH = (byte)(iCheckSun >> 8); if (ReceBuff[0] == 0x7E && bRece[1] == MainControl.bDoorAddrL && bRece[2] == MainControl.bDoorAddrH && ReceBuff[3] == 0x8B && ReceBuff[4] == 0x10 && ReceBuff[31] == bL && ReceBuff[32] == bH && ReceBuff[33] == 0x0D) { if (frmMain.blDebug) { string str = "校时成功"; MessageUtil.ShowTips(str); } } } } }
public bool AddPower(string strIcNo) { bool blRet = false; #region 添加 权限 发送 bSendDoor[1] = MainControl.bDoorAddrL; bSendDoor[2] = MainControl.bDoorAddrH; bSendDoor[3] = 0x07; bSendDoor[4] = 0x11; bSendDoor[5] = 0x00; bSendDoor[6] = 0x00; //IC卡号 try { string strTemp = strIcNo.Substring(3, 5); int iTemp = Convert.ToInt32(strTemp); bSendDoor[7] = (byte)(iTemp & 0xff); bSendDoor[8] = (byte)((iTemp >> 8) & 0xff); strTemp = strIcNo.Substring(0, 3); bSendDoor[9] = Convert.ToByte(strTemp); } catch (Exception ex) { MessageUtil.ShowTips(ex.Message); } //门号 bSendDoor[10] = 0x01; //起始年月 2000年01月01 bSendDoor[11] = 0x21; bSendDoor[12] = 0x00; //终止年月 2050年12月31 bSendDoor[13] = 0x9F; bSendDoor[14] = 0x65; //时段 bSendDoor[15] = 0x01; //密码 40 E2 01 bSendDoor[16] = 0x40; bSendDoor[17] = 0xE2; bSendDoor[18] = 0x01; for (int i = 19; i < 31; i++) { bSendDoor[i] = 0; } int iCheckSun = 0; for (int i = 1; i < 31; i++) { iCheckSun += bSendDoor[i]; } bSendDoor[31] = (byte)(iCheckSun & 0x00ff); bSendDoor[32] = (byte)(iCheckSun >> 8); //txt 发送 string strContent = SerialPortUtil.ByteToHex(bSendDoor); txtLog.WriteTxt("Door.txt", "AddPower Send", strContent, false); byte[] bRece = new byte[34]; int iLength = doorSerial.SendReceByte(bSendDoor, ref bRece, 500); //txt 接受 if (iLength > 0) { strContent = SerialPortUtil.ByteToHex(bRece); txtLog.WriteTxt("Door.txt", "AddPower Rece", strContent, true);//01 01 01 0F 11 8C } #endregion #region 接受 判断 if (iLength == 34) { iCheckSun = 0; for (int i = 1; i < 31; i++) { iCheckSun += bRece[i]; } byte bL = (byte)(iCheckSun & 0x00ff); byte bH = (byte)(iCheckSun >> 8); if (bRece[0] == 0x7E && bRece[1] == MainControl.bDoorAddrL && bRece[2] == MainControl.bDoorAddrH && bRece[3] == 0x07 && bRece[4] == 0x11 && bRece[31] == bL && bRece[32] == bH && bRece[33] == 0x0D) { if (bRece[5] == 0x01) { blRet = true; } else { blRet = false; } } else { blRet = false; } } else { blRet = false; } #endregion return(blRet); }