hello, i just tried arduino uno r4 wifi. i tried to read serial data from arduino uno r4 wifi connected to DHT22 sensor via visual studio, but serial data is not readable in visual studio. when i try to use arduino mega 2560 data is readable in visual studio. are there any special or different steps that need to be done so that arduino uno r4 wifi can be read in visual studio?
Arduino Code:
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.println(t);
}
Code Visual Studio:
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace Test_Data_Arduino
{
public partial class Form1 : Form
{
SerialPort serialPort;
public Form1()
{
InitializeComponent();
timer1.Interval = 1000;
timer1.Start();
serialPort = new SerialPort("COM6", 9600);
try
{
serialPort.Open();
}
catch
{
Console.WriteLine("Unable to open COM port - check it's not in use.");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
bool haveTemperature = false;
string temperature = string.Empty;
while (!haveTemperature)
{
temperature = serialPort.ReadLine();
if (!string.IsNullOrEmpty(temperature))
{
haveTemperature = true;
}
}
if (haveTemperature) {
textBox1.Text += "Temperature is:" + temperature + " °C" + Environment.NewLine;
}
}
}
}
have you modified the Visual Studio code between it working with the Mega and it failing with the UNO R4?
the UNO R4 Serial COM port would probably not be the same as the Mega - have you checked the port COM setting?
does the UNO R4 communicate with the IDE Serial Monitor OK?
did you remember to close the serial monitor when using Visual Studio?
I did modified the visual studio between Arduino uno r4 wifi. There's no problem in COM port setting and I work just fine in serial monitor. I always close the serial monitor when running the visual studio
are you sure the timer is ticking?
I always find it a good idea to display an * on the Console - it can be commented out when you know it is working
also display the temperature, e.g.
private void timer1_Tick(object sender, EventArgs e)
{
Console.WriteLine("*");
bool haveTemperature = false;
string temperature = string.Empty;
while (!haveTemperature)
{
temperature = serialPort.ReadLine();
Console.WriteLine("temperature " + temperature);
if (!string.IsNullOrEmpty(temperature))
{
haveTemperature = true;
}
}
why don't you use SerialPort DataReceived event to handle the received text?, e.g.
public partial class Form1 : Form
{
// delegate to transfer received data to TextBox
public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;
public Form1()
{
InitializeComponent();
this.myDelegate = new AddDataDelegate(AddDataMethod);
......
}
// data received from serial port - display on textbox
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
//textBox1.AppendText(serialPort1.ReadExisting()); // not thread safe
string s = serialPort1.ReadExisting();
textBox1.Invoke(this.myDelegate, new Object[] { s });
}
// display seral data on textbox
public void AddDataMethod(String myString)
{
textBox1.AppendText(myString);
}
One important distinction between those boards that may or may not have anything to do with your issue. When your VS program connects to the mega it will cause the mega to reset. This won't happen on the R4.
I tried using realterm and it reads data from arduino uno r4 wifi. but in the data that is read there is a CRLF at the end of the incoming data and I don't know where it came from.
this is a photo from realterm:
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace CsharpTerminal
{
public partial class Form1 : Form
{
// delegate to transfer received data to TextBox
public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;
public Form1()
{
InitializeComponent();
this.myDelegate = new AddDataDelegate(AddDataMethod);
serialPort1.Open();
textBox1.AppendText(serialPort1.PortName + " open" + Environment.NewLine);
}
// key on textbox pressed, read key and transmit down serial line
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if( serialPort1.IsOpen)
serialPort1.Write(e.KeyChar.ToString());
else
textBox1.AppendText("No COM port open" + Environment.NewLine);
}
// data received from serial port - display on textbox
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
//textBox1.AppendText(serialPort1.ReadExisting()); // not thread safe
string s = serialPort1.ReadExisting();
textBox1.Invoke(this.myDelegate, new Object[] { s });
}
// display seral data on textbox
public void AddDataMethod(String myString)
{
textBox1.AppendText(myString);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
serialPort1.Close();
}
}
}