Exemplo n.º 1
0
        /// <summary>
        /// 查询温湿度 值
        /// </summary>
        /// <param name="iAddr"></param>
        /// <returns></returns>
        public int[] AskWsd(int iAddr)
        {
            int[] Wsd = new int[2] {
                25, 55
            };
            //serialWsdYg.ReadExisting();
            SendWsd[4] = 0;
            SendWsd[3] = (byte)iAddr;
            for (int i = 0; i < 4; i++)
            {
                SendWsd[4] = (byte)(SendWsd[4] + SendWsd[i]);
            }
            //txt 发送
            string strContent = SerialPortUtil.ByteToStrHex(SendWsd);

            txtLog.WriteTxt("Wsd.txt", "Send", strContent, false);

            byte[] bRece   = new byte[7];
            int    iRetLen = mainSerial.SendReceByte(SendWsd, ref bRece, 300);

            //txt 接受
            if (iRetLen > 0)
            {
                strContent = SerialPortUtil.ByteToStrHex(bRece);
                txtLog.WriteTxt("Wsd.txt", "Rece", strContent, true);
            }

            byte ReceCheck = 0;

            for (byte bLen = 0; bLen < 6; bLen++)
            {
                ReceCheck = (byte)(ReceCheck + bRece[bLen]);
            }
            if (iRetLen == 7 && bRece[0] == 0x2A && bRece[1] == 0x44 &&
                bRece[2] == bRece[3] && bRece[4] == bRece[5] && bRece[6] == ReceCheck)//2A 44 B3 B3 C9 C9 66
            {
                //温度
                byte b = bRece[2];
                if ((b & 0x80) == 0x80)
                {
                    Wsd[0] = 0 - b & 0x7f;
                }
                else
                {
                    Wsd[0] = b;
                }
                //湿度
                Wsd[1] = bRece[4];
            }
            else//不等于 7位校验 正确
            {
                Wsd[0] = 25;
                Wsd[1] = 55;
            }
            return(Wsd);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 设置空调厂家
        /// </summary>
        /// <param name="bAddr"></param>
        /// <param name="bTemp"></param>
        public bool OtherAirSetType(byte bAddr, byte bFactory, int iCode)
        {
            bool blRet = false;

            if (bAddr >= 10)
            {
                Byte bHAddr = (byte)(bAddr % 10);
                OtherAirType[3] = (byte)(bHAddr + 0x30);//地址高位
            }
            else
            {
                OtherAirType[4] = (byte)(bAddr + 0x30); //地址低位
            }
            OtherAirType[5] = 0x02;                     //设置空调厂家
            OtherAirType[7] = bFactory;;
            OtherAirType[6] = 0x00;
            //校验
            OtherAirType[9]  = (byte)(OtherAirType[5] ^ OtherAirType[6] ^ OtherAirType[7] ^ OtherAirType[8]);
            OtherAirType[11] = (byte)iCode;
            OtherAirType[10] = (byte)(iCode >> 8);

            //txt 发送
            string strContent = SerialPortUtil.ByteToStrHex(OtherAirType);

            txtLog.WriteTxt("Air.txt", "OtherAirCode Send", strContent, false);

            byte[] bRece   = new byte[14];
            int    iRetLen = mainSerial.SendReceByte(OtherAirType, ref bRece, 300);

            //txt 接受
            if (iRetLen > 0)
            {
                strContent = SerialPortUtil.ByteToStrHex(bRece);
                txtLog.WriteTxt("Air.txt", "OtherAirCode Rece", strContent, true);
            }

            if (iRetLen == 14)
            {
                byte ReceCheck = (byte)(bRece[5] ^ bRece[6] ^ bRece[7] ^ bRece[8]);
                if (bRece[0] == OtherAirType[0] && bRece[1] == OtherAirType[1] && bRece[2] == OtherAirType[2] && bRece[3] == OtherAirType[3] && bRece[4] == OtherAirType[4] &&
                    bRece[5] == 0x02 && bRece[9] == ReceCheck && bRece[12] == 0x01 && bRece[13] == 0x0D)
                {
                    blRet = true;
                }
            }
            return(blRet);
        }
Exemplo n.º 3
0
        public bool OtherAirControl(byte bAddr, OtherAirControlType controlType, byte btContent)
        {
            bool blRet = false;

            if (bAddr >= 10)
            {
                Byte bHAddr = (byte)(bAddr % 10);
                OtherAirSend[3] = (byte)(bHAddr + 0x30);//地址高位
            }
            else
            {
                OtherAirSend[4] = (byte)(bAddr + 0x30); //地址低位
            }
            OtherAirSend[5] = (byte)controlType;        //控制类型
            OtherAirSend[6] = btContent;
            //校验
            OtherAirSend[9] = (byte)(OtherAirSend[5] ^ OtherAirSend[6] ^ OtherAirSend[7] ^ OtherAirSend[8]);

            //txt 发送
            string strContent = SerialPortUtil.ByteToStrHex(OtherAirSend);

            txtLog.WriteTxt("Air.txt", "OtherAir Send", strContent, false);

            byte[] bRece   = new byte[12];
            int    iRetLen = mainSerial.SendReceByte(OtherAirSend, ref bRece, 500);

            //txt 接受
            if (iRetLen > 0)
            {
                strContent = SerialPortUtil.ByteToStrHex(bRece);
                txtLog.WriteTxt("Air.txt", "OtherAir Rece", strContent, true);
            }

            if (iRetLen == 12)
            {
                byte ReceCheck = (byte)(bRece[5] ^ bRece[6] ^ bRece[7] ^ bRece[8]);
                if (bRece[0] == OtherAirSend[0] && bRece[1] == OtherAirSend[1] && bRece[2] == OtherAirSend[2] && bRece[3] == OtherAirSend[3] && bRece[4] == OtherAirSend[4] &&
                    bRece[5] == OtherAirSend[5] && bRece[6] == OtherAirSend[6] && bRece[7] == OtherAirSend[7] && bRece[8] == OtherAirSend[8] && bRece[9] == ReceCheck &&
                    bRece[10] == 0x01 && bRece[11] == OtherAirSend[11])
                {
                    blRet = true;
                }
            }
            return(blRet);
        }
Exemplo n.º 4
0
 public static void TxtWriteByte(string strFileName, string strType, byte[] bContent, bool blNewLine)
 {
     if (frmMain.blDebug)
     {
         string strContent = SerialPortUtil.ByteToStrHex(bContent);
         string strFile    = IsTodayFile();
         strFile += strFileName;
         string str = strType;
         str += ":  ";
         str += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
         str += "  ";
         str += strContent;
         str += "\r\n";
         if (blNewLine)
         {
             str += "\r\n";
         }
         WriteFile(strFile, true, str);
     }
 }
Exemplo n.º 5
0
        /// <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);
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void ReadRunState(ref int iCardIndex, ref int iCardCountLast, ref string sCardNo, ref DoorsState doorState, bool blAdd)
        {
            bSendDoor[1] = MainControl.bDoorAddrL;
            bSendDoor[2] = MainControl.bDoorAddrH;
            bSendDoor[3] = 0x81;
            bSendDoor[4] = 0x10;
            bSendDoor[5] = (byte)(iCardIndex & 0xff);
            bSendDoor[6] = (byte)((iCardIndex >> 8) & 0xff);
            bSendDoor[7] = (byte)((iCardIndex >> 16) & 0xff);
            bSendDoor[8] = (byte)((iCardIndex >> 24) & 0xff);
            for (int i = 9; 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", "State 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", "State Rece", strContent, true);//01 01 01 0F 11 8C
            }

            #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] == 0x81 && bRece[4] == 0x10 &&
                    bRece[31] == bL && bRece[32] == bH && bRece[33] == 0x0D)
                {
                    JudgeDatetime(bRece);
                    // 刷卡数
                    int iCardCount = bRece[12];
                    int i          = bRece[13];
                    i          *= 256;
                    iCardCount += i;
                    i           = bRece[14];
                    i          *= 65536;
                    iCardCount += i;
                    if (iCardIndex == 0 && iCardCountLast == 0)
                    {
                        //第一次运行
                        iCardCountLast = iCardCount;
                        iCardIndex     = iCardCount;
                        iCardIndex++;
                    }
                    else
                    {
                        if (iCardCount > iCardCountLast)
                        {
                            //卡号
                            iCardCountLast = iCardCount;
                            iCardIndex     = iCardCount;
                            iCardIndex++;
                            string str       = bRece[19].ToString();
                            string strCardNo = IcCardFormat(3, str);
                            i          = bRece[18];
                            i         *= 256;
                            i         += bRece[17];
                            str        = i.ToString();
                            str        = IcCardFormat(5, str);
                            strCardNo += str;
                            //记录状态
                            byte bRecordState = bRece[20];
                            if (blAdd)// blAdd  为添加用户时
                            {
                                sCardNo = strCardNo;
                            }
                            else
                            {
                                if (bRecordState == 0x00 || bRecordState == 0x01)//有效IC卡
                                {
                                    sCardNo = strCardNo;
                                }
                            }
                        }
                    }

                    //        //门磁状态
                    byte bMc = bRece[26];
                    if ((bMc & 0x10) == 0x00)
                    {
                        doorState = DoorsState.开门;
                    }
                    else
                    {
                        doorState = DoorsState.关门;
                    }
                }
            }

            #endregion
        }