예제 #1
0
        /// <summary>
        /// 读取环境湿度
        /// </summary>
        /// <param name="Address">站号</param>
        /// <param name="Humidity">返回环境湿度</param>
        /// <returns>读取成功或失败</returns>
        public static bool GetHumidity(int Address, ref float Humidity)
        {
            string SendString = Auxiliary.NumToHex(Address, 1) + Auxiliary.NumToHex(4, 1) + Auxiliary.NumToHex(1001, 2) + Auxiliary.NumToHex(1, 2);

            byte[] SendData   = Auxiliary.CheckCRC(SendString);
            byte[] ReturnData = WriteData(SendData, 100);
            if (ReturnData == null)
            {
                for (int i = 0; i < HTU21D_Channel.Length; i++)
                {
                    if (HTU21D_Channel[i].Station == Address)
                    {
                        HTU21D_Channel[i].IsConnect = false;
                    }
                }

                return(false);
            }
            else
            {
                Humidity = Auxiliary.HexToNum(ReturnData[3].ToString("X2") + ReturnData[4].ToString("X2")) / 100f;
                for (int i = 0; i < HTU21D_Channel.Length; i++)
                {
                    if (HTU21D_Channel[i].Station == Address)
                    {
                        HTU21D_Channel[i].IsConnect = true;
                    }
                }
                return(true);
            }
        }
예제 #2
0
        /// <summary>
        /// 向串口写入数据
        /// </summary>
        /// <param name="SendData">需发送的数据</param>
        /// <param name="TimeOut">超时</param>
        /// <returns>返回Byte数组报文</returns>
        private static byte[] WriteData(byte[] SendData, int TimeOut)
        {
            try
            {
                lock (LockValue)
                {
                    byte[]    GetBuffer;
                    Stopwatch TimeOutDelay = new Stopwatch();

                    if (AutoLoad == true || ExitRead == false)
                    {
                        if (SP.IsOpen == false)
                        {
                            SP.Open();
                        }
                    }
                    else
                    {
                        return(null);
                    }

                    if (SP.BytesToRead != 0)
                    {
                        SP.DiscardInBuffer();
                    }

                    SP.Write(SendData, 0, SendData.Length);
                    int ReturnDatByte = ReadDataBit(SendData);
                    if (TimeOut != 0)
                    {
                        TimeOutDelay.Start();
                    }
                    do
                    {
                        Thread.Sleep(1);
                    }while (SP.BytesToRead != ReturnDatByte && TimeOutDelay.ElapsedMilliseconds <= TimeOut);
                    if (TimeOutDelay.ElapsedMilliseconds < TimeOut)
                    {
                        GetBuffer = new byte[SP.BytesToRead];
                        SP.Read(GetBuffer, 0, SP.BytesToRead);
                        if (Auxiliary.CheckCRC(GetBuffer))
                        {
                            return(GetBuffer);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch
            {
                return(null);
            }
        }
예제 #3
0
        private static string WriteData(string SendData, int TimeOut)
        {
            try
            {
                lock (LockValue)
                {
                    string    RetData = "";
                    byte[]    GetBuffer;
                    Stopwatch TimeOutDelay = new Stopwatch();

                    if (SP.IsOpen == false)
                    {
                        SP.Open();
                    }
                    if (SP.BytesToRead != 0)
                    {
                        SP.DiscardInBuffer();
                    }
                    byte[] WriteData = Auxiliary.CheckCRC(SendData);

                    SP.Write(WriteData, 0, WriteData.Length);
                    int ReturnDatByte = ReadDataBit(WriteData);

                    if (TimeOut != 0)
                    {
                        TimeOutDelay.Start();
                    }
                    do
                    {
                        Thread.Sleep(1);
                    }while (SP.BytesToRead != ReturnDatByte && TimeOutDelay.ElapsedMilliseconds <= TimeOut);

                    if (TimeOutDelay.ElapsedMilliseconds < TimeOut)
                    {
                        GetBuffer = new byte[SP.BytesToRead];
                        SP.Read(GetBuffer, 0, SP.BytesToRead);
                        if (Auxiliary.CheckCRC(GetBuffer))
                        {
                            if (GetBuffer[1] == 04)
                            {
                                for (int i = 0; i < GetBuffer[2] / 2; i += 2)
                                {
                                    RetData += Convert.ToInt32(GetBuffer[3 + i].ToString("X2") + GetBuffer[4 + i].ToString("X2"), 16).ToString() + "-";
                                }
                            }
                        }
                    }
                    else
                    {
                        RetData = null;
                    }
                    return(RetData);
                }
            }
            catch
            {
                return(null);
            }
        }
예제 #4
0
        /// <summary>
        /// 测试连接可行性
        /// </summary>
        /// <returns></returns>
        private static bool TestConnect(int Address)
        {
            string SendString = Auxiliary.NumToHex(Address, 1) + Auxiliary.NumToHex(4, 1) + Auxiliary.NumToHex(1000, 2) + Auxiliary.NumToHex(1, 2);

            byte[] SendData   = Auxiliary.CheckCRC(SendString);
            byte[] ReturnData = WriteData(SendData, 100);
            if (ReturnData == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
예제 #5
0
        private static bool Get(int Address, ref SUTOBody[] Body)
        {
            string SendString = Auxiliary.NumToHex(Address, 1) + Auxiliary.NumToHex(3, 1) + Auxiliary.NumToHex(0, 2) + Auxiliary.NumToHex(18, 2);

            byte[] SendData   = Auxiliary.CheckCRC(SendString);
            byte[] ReturnData = WriteData(SendData, 250);
            if (ReturnData == null)
            {
                _IsConnect = false;
                return(false);
            }
            Body[Address - 1].Temperature        = Auxiliary.HexToFloat(ReturnData[5].ToString("X2") + ReturnData[6].ToString("X2") + ReturnData[3].ToString("X2") + ReturnData[4].ToString("X2"));
            Body[Address - 1].Humidity           = Auxiliary.HexToFloat(ReturnData[9].ToString("X2") + ReturnData[10].ToString("X2") + ReturnData[7].ToString("X2") + ReturnData[8].ToString("X2"));
            Body[Address - 1].Pressure           = Auxiliary.HexToFloat(ReturnData[13].ToString("X2") + ReturnData[14].ToString("X2") + ReturnData[11].ToString("X2") + ReturnData[12].ToString("X2"));
            Body[Address - 1].PressureDewPoint   = Auxiliary.HexToFloat(ReturnData[17].ToString("X2") + ReturnData[18].ToString("X2") + ReturnData[15].ToString("X2") + ReturnData[16].ToString("X2"));
            Body[Address - 1].DewPoint           = Auxiliary.HexToFloat(ReturnData[21].ToString("X2") + ReturnData[22].ToString("X2") + ReturnData[19].ToString("X2") + ReturnData[20].ToString("X2"));
            Body[Address - 1].MixingRatio        = Auxiliary.HexToFloat(ReturnData[25].ToString("X2") + ReturnData[26].ToString("X2") + ReturnData[23].ToString("X2") + ReturnData[24].ToString("X2"));
            Body[Address - 1].AtHumidity         = Auxiliary.HexToFloat(ReturnData[29].ToString("X2") + ReturnData[30].ToString("X2") + ReturnData[27].ToString("X2") + ReturnData[28].ToString("X2"));
            Body[Address - 1].AtAbsoluteHumidity = Auxiliary.HexToFloat(ReturnData[33].ToString("X2") + ReturnData[34].ToString("X2") + ReturnData[31].ToString("X2") + ReturnData[32].ToString("X2"));
            Body[Address - 1].VolumeRatio        = Auxiliary.HexToFloat(ReturnData[37].ToString("X2") + ReturnData[38].ToString("X2") + ReturnData[35].ToString("X2") + ReturnData[36].ToString("X2"));

            _IsConnect = true;
            return(true);
        }