<tr id="ohcdf"></tr>
    <ins id="ohcdf"><option id="ohcdf"></option></ins>

    <ins id="ohcdf"></ins>
  1. <tr id="ohcdf"></tr>

      <small id="ohcdf"></small><tr id="ohcdf"><small id="ohcdf"><delect id="ohcdf"></delect></small></tr><code id="ohcdf"></code>
        潤宇軟件
        首 頁 企業簡介 項目案例 軟件定制 行業軟件 解決方案 企業資訊 服務專區 客服中心
        業務介紹:西安軟件公司、軟件開發、軟件定制、軟件外包
        軟件 方案 文章
          潤宇軟件 >> 新聞資訊  >> 解決方案

        溫濕度傳感器開發

        發布時間:2020/11/26  瀏覽次數:次  字體【    】

        濕度傳感器開發用的是tcp或者udp通信

         /// <summary>
                /// 發送信息
                /// </summary>
                /// <param name="obj"></param>
                private void SendMessage(object obj)
                {
                    string message = (string)obj;
                    //00 B0 D0 2A B7 3C FF 03 00 00 00 06
                    byte[] mybyte = new byte[12];
                    mybyte[0] = 0x00;
                    mybyte[1] = 0xB0;
                    mybyte[2] = 0xD0;
                    mybyte[3] = 0x2A;
                    mybyte[4] = 0xB7;
                    mybyte[5] = 0x3C;
                    mybyte[6] = 0xFF;
                    mybyte[7] = 0x03;
                    mybyte[8] = 0x00;
                    mybyte[9] = 0x00;
                    mybyte[10] = 0x00;
                    mybyte[11] = 0x06;
                    IPEndPoint remoteIpep = new IPEndPoint(
                        IPAddress.Parse(ConfigurationManager.AppSettings["IP"]), Int32.Parse(ConfigurationManager.AppSettings["Port"])); // 發送到的IP地址和目標端口號
                    udpcSend.Send(mybyte, mybyte.Length, remoteIpep);
                    udpcSend.Close();
         
                }
                private void sendMessage() {
                    // 實名發送
                    try
                    {
                        IPEndPoint localIpep = new IPEndPoint(
                     IPAddress.Parse(ip), Int32.Parse(ConfigurationManager.AppSettings["CurrentPort"])); // 本機IP,本機端口號
                        udpcSend = new UdpClient(localIpep);
         
                        Thread thrSend = new Thread(SendMessage);
                        thrSend.Start();
                    } catch {
         
                    }
                  
                }

         

         /// <summary>
                /// 開關:在監聽UDP報文階段為true,否則為false
                /// </summary>
                bool IsUdpcRecvStart = false;
                /// <summary>
                /// 線程:不斷監聽UDP報文
                /// </summary>
                Thread thrRecv;
                /// <summary>
                /// 線程:監聽是否停止工作
                /// </summary>
                Thread thrWork;
                /// <summary>
                /// 按鈕:接收數據開關
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private void btnRecv()
                {
                    if (!IsUdpcRecvStart) // 未監聽的情況,開始監聽
                    {
                        IPEndPoint localIpep = new IPEndPoint(IPAddress.Any, Int32.Parse(ConfigurationManager.AppSettings["CurrentPort"])); // 本機IP和監聽端口號
                        udpcRecv = new UdpClient(localIpep);
                        thrRecv = new Thread(ReceiveMessage);
                        thrRecv.Start();
                        IsUdpcRecvStart = true;
                        Trace.Write("UDP監聽器已成功啟動");
                        this.tbOut.AppendText("UDP監聽器已成功啟動"+"\n");
                    }
                    else                  // 正在監聽的情況,終止監聽
                    {
                        //thrRecv.Abort(); // 必須先關閉這個線程,否則會異常
                        //udpcRecv.Close();
                        //IsUdpcRecvStart = false;
                        //Trace.Write("UDP監聽器已成功關閉");
                    }
                }
                public static string byteToHexStr(byte[] bytes)
                {
                    string returnStr = "";
                    if (bytes != null)
                    {
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            returnStr += bytes[i].ToString("X2");
                        }
                    }
                    return returnStr;
                }
                /// <summary>
                /// 接收數據
                /// </summary>
                /// <param name="obj"></param>
                private void ReceiveMessage(object obj)
                {
                    IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0);
                    while (isOpen)
                    {
                        try
                        {
                            byte[] bytRecv = udpcRecv.Receive(ref remoteIpep);
                            dealData(bytRecv);
                            string message = byteToHexStr(bytRecv);
                            status = GetStatus.SUCCESS;
                            //FileUtils.SaveFile("\\log\\logData.txt", message);
                            //Trace.Write(message+"\n");
                            //this.tbOut.AppendText(message);
                        }
                        catch (Exception ex)
                        {
                            Trace.Write(ex.Message);
                            break;
                        }
                    }
                }

          關閉本頁
        西部IT網合作伙伴 合作伙伴
        陜西省 | 榆林 | 延安 | 銅川 | 渭南 | 商洛 | 寶雞 | 漢中 | 安康 | 咸陽
        網站首頁 | 關于我們 | 售后服務 | 項目合同 | 查看留言 | 在線留言 | 客服中心
        © 版權所有:西安潤宇軟件科技有限公司 
        公司地址:西安市絲路國際創意夢工廠4號樓 聯系電話:029-87878512 手機:13468700578 聯系人:李先生
        Copyright ® 2009-2015 RunYusoft.com Inc. All Rights Reserved 
        技術支持:西安潤宇軟件科技有限公司  陜ICP備11000720號
        精品久久一区二区三区无码,久久亚洲AV成人无码国产精品,久久久久精品a毛片,亚洲精品无码九九九九