I have a very simple program that activates or deactivates an output according to serial command.
This is the program:
if (Serial.available() > 0) {
String msg = Serial.readString();
if (msg.equals("cl")) {
digitalWrite(OUT, 0);
}
else if (msg.equals("op")) {
digitalWrite(OUT, 1);
}
When I test it with the serial monitor it works fine. When I send the commands from a python script, the command to reset the output works but the command to set the output, doesn't.
Are you sending anything from Python besides those characters? No \r or \n terminator?
I presume in the serial monitor you have "no line ending" selected.
@razm I suggest you move your post out of the uncategorized section of the forum before a moderator sees it. Perhaps one of the networking sections would be more appropriate.
Please post your complete code - using the code tags - so we can see where the issue may lie.
For your information we get a lot of threads like XX does not work. The poster in this situation is always wrong. digitalWrite always works, the problem normally is that your code never gets to the point where digitalWrite is used.
Suspicious, indeed. Why should one two-character command get through, and the other not? Transmission time is identical. Your choice, but digging deeper would be wise.
You are reading the serial port when there is more that 0 characters, so quite possibly 1 character, not the 2 you are hoping for. I'm surprised it works at all.
I suggest you read the serial input basics topic.
I'm pretty sure that took care of it. I use Python to access Arduinos.
I have my Arduino send a "Ready" message to my Python script when it is ready to go. I don't presume it's there until I get that. Usually takes a couple seconds.