Зависание программы считывания данных с DHT22
- Войдите на сайт для отправки комментариев
Ср, 19/11/2014 - 21:23
Стояла задача, написать программу для ПК, которая выводила бы данные с датчика на экран.
Программа писалась на Visual Studio C# 2010.
Программа работает и показывает данные как и предпологалось, но через некоторое время зависает 0_0
Где может быть ошибка?
Код программы:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; // для работы с СОМ портом
using System.IO.Ports;
using System.Timers; // Для работы с таймером
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public String c;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Close();
Close();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Open();
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = serialPort1.ReadLine();
}
}
}
Скетч ардуинки (самый стандартный):
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(1000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
Что зависает-то, Ардуино или программа на C#? Если C# то Вам лучше обратиться на форум, где много программистов на C#.
Зависает именно программа для пк, ардуинка исправно шлет данные.
Может оно на IO стопорится?
И для DHT22 1 секунды задержки слишком мало. У него максимальная скорость опроса 0,5 герц.