digitalWrite doesn't work sometimes

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.

is OUT equals to any particular pin. Did you set the pinMode for the particular pin

@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.

No. Just 'op' or 'cl'
But 'cl' works and 'op' not

Is
#define OUT 8
pinMode(OUT, OUTPUT);

check whether you are sending any spaces with it

It might help if you post the Python and Arduino code.

import serial
import sys, time



def main():
        ser = serial.Serial()
        ser.baudrate = 9600
        ser.port = "COM3"
        ser.open()
        time.sleep(0.1)
        command= 'op'
        ser.write(command.encode('utf-8'))
    
if __name__ == '__main__':
    main()

Possibly, post both versions of the Python, and what about the Arduino code?

You realize when you connect to the Arduino, it will reset the Arduino.
Are you allowing enough time for that to happen?

Good point. Thanks
It wasn't enough time

You should never post in the Uncategorised section.
It says so in the description of the section. Therefore I have moved your post here.

You might want to look at this How to get the best out of this forum before you proceed any further.

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.

Serial.readString() will wait by default for 1000 milkseconds, so the use here is at all that unreasonable.

In any case, I took this

to mean the problem has been solved. We haven't heard since, @razm - you good?

a7

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.

:grinning: :grinning: :grinning:

Thank you for the clarification :slightly_smiling_face:

One milksecond is 0.99999999999 milliseconds.

I have a # define in my own header

# define unsinged unsigned

Not gonna say what else. :expressionless:

a7