j9首页|官网首页

润宇软件
首 页 企业简介 项目案例 软件定制 行业软件 办理方案 企业资讯 办事专区 客服中心
商业介绍:j9九游会软件公司、软件开辟、软件定制、软件外包
软件 方案 文章
  润宇软件 >> 新闻资讯  >> 办理方案

温湿度传感器开辟

公布>###nbsp; 欣赏次数:次  字体【    】

湿度传感器开辟用的是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网互助同伴 互助同伴
陕西省 | 榆林 | 延安 | 铜川 | 渭南 | 商洛 | 宝鸡 | 汉中 | 安康 | 咸阳
网站首页 | 关于j9九游会 | 售后办事 | 项目条约 | 检察留言 | 在线留言 | 客服中心
© 版权一切:j9九游会润宇软件科技有限公司 
公司地点:j9九游会市丝路国际创意梦工场4号楼 联系>###nbsp;>### 联系人:李老师
Copyright ® 2009-2015 RunYusoft.com Inc. All Rights Reserved 
技能支持:j9九游会润宇软件科技有限公司