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

3 篇文章 0 订阅
2 篇文章 0 订阅

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

下位机系统使用的是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);//注册事件

 

   

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值