c# 温湿度变送器传感器开发(一)

c# 温湿度传感器开发用的是tcp或者udp通信,这里介绍udp用法

代码就不上传了,想要可以私聊我,在开发过程中,不可避免出现问题,需要先调试走通,我是用的

c# 温湿度变送器传感器开发(一)_第1张图片

设备是用的这款

https://s.click.taobao.com/w4mLMew


新建一个工程,直接发送请求数据命令

 /// 
        /// 发送信息
        /// 
        /// 
        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 {

            }
           
        }

发送成功后,传感器会反馈给你结果,然后就是接收数据

 /// 
        /// 开关:在监听UDP报文阶段为true,否则为false
        /// 
        bool IsUdpcRecvStart = false;
        /// 
        /// 线程:不断监听UDP报文
        /// 
        Thread thrRecv;
        /// 
        /// 线程:监听是否停止工作
        /// 
        Thread thrWork;
        /// 
        /// 按钮:接收数据开关
        /// 
        /// 
        /// 
        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;
        }

        /// 
        /// 接收数据
        /// 
        /// 
        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;
                }
            }
        }



你可能感兴趣的:(c#)