I am new to the Arduino and bought me an UNO for learning / testing purposes.
During my first little project that involves serial communication between PC and Arduino connected to a USB 2.0 port
I encountered some strange behavior that I can not explain.
Searching the internet unfortunately did not gave me suitable informations.
The Loop-Back Test was successfully done.
I wrote a very small program to show the behavior.
On PC side I am using a very small program written in C#
static void Serial_handler(object sender, SerialDataReceivedEventArgs e)
{
var sp = (SerialPort)sender;
var av = sp.BytesToRead;
while (av-- > 0)
Console.WriteLine("async " + sp.ReadByte());
}
static void Main(string[] args)
{
var com = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One)
{
WriteTimeout = 2000,
ReadTimeout = 2000,
Handshake = Handshake.None
};
com.DataReceived += Serial_handler;
com.Open();
string data = "1234";
com.Write(data.ToString());
Console.ReadLine();
com.Close();
}
What this should do is to send "1234" to the Arduino and receive those 4 byte back.
This is working with a baud set to 9600. But if I change this to 115200 the PC side receives "240" right after opening the port.
Of course the baud was set equal for every test in the Sketch and on PC side.
For 9600 the output is:
async 49
async 50
async 51
async 52
For 115200 the output is:
async 240
async 240
async 240
async 240
async 49
async 50
async 51
async 52
Without sending "1234" and starting the C# program the following is received:
async 240
async 240
async 240
async 240
Using putty with 115200 and opening the COM shows a similar problem. I was using the Sketch from above with 115200 and entered "1234". This was the output:
"▒34"
Any ideas where these bytes come from after opening the port?
When a PC program opens the COM port it causes the Uno to reset. This is deliberate and is to facilitate uploading new programs. Your PC program should open the COM port and then allow enough time for the Arduino to reset before starting to send data. And it should keep the COM port open until it is completely finished with the Arduino.
In my programs I get the Arduino to send a message from setup() - for example Serial.println("Arduino is ready") - and I get my PC programs to wait until they receive that message before sending data. This has the added advantage that any garbage that might have been in the serial communication chain is discarded.
This Simple Python - Arduino demo should give you the general idea about the PC-Arduino communication. You should be able to replace the Python code with equivalent functionality in any other programming language. The Python code should work on a Windows PC if you modify it for the Windows serial port references
What I don't understand is, why does this only happen on 115200?
Shouldn't the controller send these bytes on connect/reset at every setting?
What I forgot to write is, that even the serial monitor in the web IDE is not working properly when set to 115200.
Using the same small Sketch as mentioned in my first post I do not get any response when sending.
9600 works perfect, using 115200 no reply is shown in the monitor.
Using the following Sketch where I added Serial.println("Arduino is ready"); produces this output in serial monitor:
"Arduino is r" or "<" or "["
When I am in serial monitor and press constantly reset on the UNO itself this is shown:
<<<e<eeJe[J
[J
Within C# program I do not receive the string "Arduino is ready" until I press reset on the UNO.
In serial monitor this gives perfect responses on every connect for 9600.
No 3rd party software is used and the Sketch is more than clear. So in my opinion this should work. Am I wrong?
To me this unfortunately looks not deterministic. The behavior should always be the same in every baud rate with putty, serial monitor and the (very easy) C# program.
Another strange thing is that the ASCIITable example in the web editor is working in 9600 and 115200,
even though the Sketch on the UNO is not changed. (115200)
Within C# program I do not receive the string "Arduino is ready" until I press reset on the UNO.
That makes me wonder if, in fact, your C# program is failing to cause the Arduino to reset. You probably need to set DTR and RTS as part of the COM port initialisation.
This past summer, I wrote a program in VB.NET to communicate with the Arduino Mega at 115200.
The Arduino Mega sent "Ready" in setup() .
I used VB.NET serial port's default DataReceived event handler.
It was not necessary to set DTR or RTS as part of COM port initialization.
No problems.
Thank you very much for your replies. I really appreciate that.
Meanwhile I tried the UNO at another PC. Same behavior.
Robin2:
From Reply #2 ...
That makes me wonder if, in fact, your C# program is failing to cause the Arduino to reset. You probably need to set DTR and RTS as part of the COM port initialisation.
...R
I just added "DtrEnable = true" to the initialization in the C# program and now it is working.
Now I can "see" (LED is blinking, off and on) that the UNO is reset on connect. It takes one to two seconds until I receive the welcome message without preceding data. Tried that with different baud rates.
Though in putty the UNO is not reset when opening the port and in the web IDE serial monitor nothing is received. A reset is done on connect in the web ide's monitor.
As a summary: It looks like the UNO is not reset when opening the COM Port without DTR set.
I think I can live with that since I now know how to deal with it.
Is this a normal behavior or could this be an error with this specific UNO?