1

I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.

The Arduino is programmed to read a string, parse it and take PWM action to run a car.

Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.

The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.

However if I write a simple program in a file write.py and execute it from the command line, nothing happens.

import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())

If I run this script from the macOS terminal using the command:

python write.py

nothing happens. What am I missing here?

1

3 Answers 3

1

A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.

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

1 Comment

This works. I was establishing the serial connection within the function so every time i was writing to Arduino a new connection was getting established, moving the connection declaration out function scope solved the problem.
0

It may be that the port is in text mode and will not send the data until a newline is sent:

data = '<1,150,0,0>\n'
ser.write(data.encode())

or flush() is called.

data = '<1,150,0,0>'
ser.write(data.encode())
ser.flush()

1 Comment

I have used a sketch shared in Arduino forum which has start marker and end marker configurable, newline should be applicable when i send serial write using Python Shell, it works perfectly when using serial monitor in Arduino IDE or Python shell. Will try adding flush in the code and see if that works.
0

The most probably thing happening here is that the data is not being sent to the serial port.

There is a simple method to check this. Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE. In the serial monitor type in <1,150,0,0> and press send.

The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.

If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link Access USB serial ports using Python and pyserial

If not, I am stumped.

Comments

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.