1

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.

7
  • 4
    What does "it does not work" mean? Commented Dec 31, 2019 at 10:44
  • The code seems to run, that is the Arduino receives something and the onboard led blinks, but nothing happens on the IR-led. Though it does not blink the same on Arduino when i run the two different pieces of code. No idea why? Commented Dec 31, 2019 at 10:50
  • Command prompt automatically closes the window when the execution is stopped. Errors can occur, but the cmd propmt immediately closes the window after occurence. What to do: Wrap your whole code in a try .. except. For the except type input(). Then you can read the error message and tell us. Commented Dec 31, 2019 at 10:50
  • Alternatively try using IDLE Python. You can find it the same website you downloaded Python. Commented Dec 31, 2019 at 11:00
  • I open a command prompt window before i run the so it stays open after execution, but i tried 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) but it does not show any errors. Have i got this wrong as well? Commented Dec 31, 2019 at 11:23

2 Answers 2

2

The problem is solved, i found an answer i did not find before.

pySerial write() works fine in Python interpreter, but not Python script

It was the fast execution of the script, and a little delay of 2 seconds did the difference.

I thank you all who have had suggestions to a solution and intend to wrap this up. Happy new year to you all!

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was saying. Use flush() to wait till character is actually transmitted!
0

I have tired your program with a simple loop-back. It is working as expected:

import serial

try:
    ser = serial.Serial("/dev/ttyUSB0")
    print("Bytes Written: %d" % (ser.write("b".encode())))
    ser.flush()
    print(ser.read())
    ser.close()
except Exception as e:
    print(e)

Output:

Bytes Written: 1
b'b'

I have called flush to make sure data has been written to the physical port. See this link for more information.

Flush of file like objects. In this case, wait until all data is written.

Looks like there could be some issue with your wires or the arduino code.

5 Comments

Tried your code and it does output the first line "Bytes Written...." but then it hangs and i have to close the command prompt? If something is wrong with either Arduino-code or the wiring why is it working from the shell or the Arduino terminal?
Have removed the read and run it again. Now it runs without any comment, no error. But still no reaction from Arduino. I am using Windows 10 and i suspect you use Linux, can that be a problem?
I think you are seeing TX/RX LED blinking. It means Arduino is receiving the bytes.
Yes i agree, but is there a way to check what the Arduino receives? If Python is sending something more then just the expected character then the problem is there. Perhaps i can write something on the Arduino to somehow parse the incoming serial data? But, again how do i know what is coming?
Yes. There are two ways. Either retransmit the data received by arduino via other serial port (check SoftwareSerial) or you can echo back the stream in your arduino and can read it in python!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.