I am trying to write a character through serial to Arduino which runs a code to control a IR-led for remote-control purpose.
Running code in Arduino-terminal works, the led does its job. The Arduino-code works.
Running line by line in shell works, no problem.
But when i run "python remote.py" it does not work.
I tried an extra piece of code to make sure its the same version of python being run by both shell and interpreter at the command prompt.
import sys
sys.stdout.write("Hello from Python %s\n" % (sys.version,))
And it results in "Python 3.7.3" which is the version of shell when i start that.
This is the entire Python code:
import serial
ser = serial.Serial('COM7', 9600)
ser.write('a'.encode())
ser.close()
I realise it is not very "Pythonic" or even entirely correct, but to simplify and to try and have as little code as possible i cut out everything that did not had to be in. Since the code runs in shell, its some how correct?
The Arduino code is here: https://pastebin.com/7T2yFzrL
I use Python 3.7.3 under Windows 10 with Notepad++
I found this thread: Arduino Python3 script, but i do not understand how my code can work in shell and not in script? It could be the codepoint (What that means I dont understand) but in that case how do i correct it? No errors are shown either. I hope someone can steer me in the right direction.
/Best regards Magnus
Tried an addition to my code after suggestion from T. Feix new code like this:
#! python3
import serial
try:
ser = serial.Serial('COM7', 9600)
ser.write('a'.encode())
ser.close()
except input() as ex:
print(ex)
unfortunately it does not return anything, no error no nothing?
My code on the Arduino expects an char and the Python seems to send a Byte, is that perhaps the error and if so how do i change either type?
Have now changed the expected datatype on the Arduino to Byte, the code still works in terminal but no luck as i run the Python script.
try .. except. For the except typeinput(). Then you can read the error message and tell us.