I have wrote a python skipt to send a command to my arduino throgh the serial port if a specific twitter post gets a like. The command is just a 1 and woks if i send it with the serial monitor but not if it´s send from the python skript.
Python Skript to send:
ser = serial.Serial('COM6', 9600) # open serial port
print(ser.name) # check which port was really used
ser.write(b'1') # write a string
ser.close()
johnwasser:
How do you send a binary 1 with Serial Monitor?
I don't know enough Python to know if "ser.write(b'1')" is the right syntax to send a binary 1.
I would send commands in ASCII to make the serial line easier to log.
that sends a string - ser.write(b'hello') would send Hello for example but then ASCII or another encoding is depending on your python version and how it's configured if I remember correctly
Woodpeckerfpv:
1 gives me 31 and a gives me 61 using the python skript I get nothing
So the "ser.write(b'1')" isn't getting any data to your sketch. That sounds like a programming or hardware error somewhere. If Serial Monitor on COM6 works and Python on COM6 doesn't, I'd think the problem was in the Python program.
The Python code in the Original Post is not sufficient.
If it is sending data to an Uno, Mega or Nano it will cause the Arduino to reset when it opens the serial port and it does not wait for the Arduino to reset before trying to send data.
If you don't want the Arduino to reset then the Python program should keep the serial port open until it is completely finished with the Arduino.