C#温湿度上位机监控系统--Arduino采集DHT11传感器温湿度数据

做毕业设计做的难受,就写了个温湿度上位机学习学习。

下位机系统使用的是arduino采集DHT11数据,核心代码如下:

int humidity;
int temperature;

void read_dht11()
{
  uint8_t bits[5];
  uint8_t cnt = 7;
  uint8_t idx = 0;
  for (int i = 0; i < 5; i++) bits[i] = 0;
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  delay(18);
  digitalWrite(pin, HIGH);
  delayMicroseconds(40);
  pinMode(pin, INPUT);
  unsigned int loopCnt = 10000;
  while (digitalRead(pin) == LOW)if (loopCnt-- == 0);
  loopCnt = 10000;
 while (digitalRead(pin) == HIGH)if (loopCnt-- == 0);
  for (int i = 0; i < 40; i++)
  {
    loopCnt = 10000;
    while (digitalRead(pin) == LOW)if (loopCnt-- == 0);
    unsigned long t = micros();
    loopCnt = 10000;
    while (digitalRead(pin) == HIGH)if (loopCnt-- == 0);
    if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
    if (cnt == 0){cnt = 7; idx++; }
    else cnt--;
  }
  humidity    = bits[0];
  temperature = bits[2];
}

上位机采用C#语言 Winform做的界面,主要使用了 SerialPort,Chart,Timer类

SerialPort用于串口数据接收

Chart用于显示实时波形

 

Timer用于定时刷新图形界面

串口数据接收需开启新线程代码如下:

Control.CheckForIllegalCrossThreadCalls = false;  //新建线程可以访问UI
serialPort1.DataReceived += new SerialDataReceivedEventHandler(Receoved);//注册事件

 

   

你可能感兴趣的:(C#,DHT11,Arduino,C#)