Hello, community.
I had an idea to use Serial I/O connection for communication between PC and Arduino Due in a way that from the computer I can send commands which can or do not have to return some values back to PC.
My code did not work as expected. Step by step I started to remove line by line to verify where the problem appeared. Beneath you can find the tests and results.
Test1:
char input;
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
input = (char)Serial.read();
Serial.print("We read:");
Serial.println((byte)input, BIN);
}
delay(1000);
}
Serial port monitoring (with timestamps):
0:12:07.437 -> We read:11011111
0:12:09.439 -> We read:11011111
0:12:11.437 -> We read:11111111
0:12:11.437 -> We read:11111111
0:12:13.415 -> We read:11110111
0:12:15.425 -> We read:11011111
0:12:17.446 -> We read:1111111
0:12:19.442 -> We read:10111111
0:12:21.427 -> We read:11111111
0:12:21.427 -> We read:11111111
0:12:23.431 -> We read:11011111
0:12:25.446 -> We read:10111111
0:12:27.415 -> We read:11111111
0:12:29.423 -> We read:11111111
0:12:29.466 -> We read:11111111
0:12:31.446 -> We read:1111111
0:12:33.424 -> We read:11111111
0:12:33.424 -> We read:11111111
You may think that everything is okay. The trick is that I do not type anything for sending to Arduino. Those bytes seem to be sent somehow automatically being filled by 1s in each bit. And sometimes 0 is set at some random bit.
Do you want to be amazed more? I was wondering how we could drop in the while loop if I did not type anything?! Therefore I added one more line to the code for debugging.
Test2:
char input;
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
input = (char)Serial.read();
Serial.print("We read:");
Serial.println((byte)input, BIN);
}
delay(1000);
Serial.println(Serial.available()); // THIS IS A LINE I ADDED
}
Serial port monitoring (with timestamps):
0:13:36.388 -> 0
0:13:37.390 -> 0
0:13:38.372 -> 0
0:13:39.387 -> 0
0:13:40.395 -> 0
0:13:41.382 -> 0
0:13:42.384 -> 0
0:13:43.397 -> 0
0:13:44.378 -> 0
0:13:45.356 -> 0
0:13:46.397 -> 0
0:13:47.383 -> 0
0:13:48.393 -> 0
0:13:49.372 -> 0
0:13:50.399 -> 0
0:13:51.384 -> 0
Honestly, I am very confused. When I force to print if there is something in buffer, mistakenly random readings disappear! Is it not a great proof of quantum physics and observer effect?!
Okay, jokes apart.
What can cause this behaviour? How to avoid this issue?
I thought that the problem is in damaged USB-cable. But one small repair in code changes the behaviour as you have seen. Additionally I tried a new cable. The problem remained.
Reprograming the Arduino board constantly reproduces the results.
The board I am using: Arduino Due, programming port.
I used Arudino IDE 2.0 for programming..
Please, let me know if I am missing to tell you some other important information.
And I am sorry if I posted the topic in a wrong category. Answer to my question was not found by me here.
Thank you in advance!