i am using the Arduino for programming a rc car - with PWM technique
i programmed to read a string, parse it and take PWM action to run a car.
therefore the Ardiuno's serial communication channel of my notebook is set up to receive the strings in the format
< A, B, C, D > i
need this setup with ABCD because these characters are needed to denote
car direction,
car speed,
car steering direction and finally
car steering position.
so far so good:
The problem is, when I send a string from the serial monitor of my notebook the string is received, parsed properly and command executed successfully. but 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,110,0,0>'
ser.write(data.encode())
If I run this script from the terminal of my Windows-notebook using the command:
python write.py
nothing happens. What am I missing here?
some considerations and musings:
what does the USB connection do here:
does a USB-connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) reset the Arduino.
How to avoid this behavior!? I guess that th data sent right after connection are lost because the Arduino boots.
should i try to establish the serial connection within the function - in order to recreate (or to set the) a new connection every time i am writing to Arduino
By the way: does the behavior in Windows is different to the behavior in Linux!?
Lots of questions - sorry if i put too much into this thread!?
Above all: this is a superb place of idea exchange and knowledge sharing. i love it.
keep up this great place.
does a USB-connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) reset the Arduino.
How to avoid this behavior!? I guess that th data sent right after connection are lost because the Arduino boots.
No, stablishing the connection does not reset the Arduino.
should i try to establish the serial connection within the function - in order to recreate (or to set the) a new connection every time i am writing to Arduino
No, you don't need tho create a new connection every time you write to arduino.
I think that the problem is the encode method in the python code. Try removing it.
If this doesn't work, try to monitor the serial port to see what's happening. In Linux you can do it with the interceptty command, and I'm sure that there are tools for doing it in windows.
I already tried using serial python, and it's a problem because it has 2 types of libraries, besides the problem of python 2.7 and the version above python 3.
Another problem is using script names with function names, always use names like myScriptWrite.py instead of write.py or print.py
Your Python program should open the connection, allow time for the Arduino to reset and then keep the connection open until it is completely finished with the Arduino.
Another option, which will not cause a reset, is to use a USB-TTL cable connected directly to the Rx Tx and GND of the Arduino. Of course with that you will need an external power source for the Arduino.
It is possible to disable the auto-reset feature but that can be inconvenient as it is needed when uploading programs.