First of all I am very new to programming so bear with me.
At work I have an arduino controlling a set of valves and pumps. At the moment we are developing an automation sequence using batch files sending commands to the arduino.
I wanted to use python with a simple GUI instead to control the arduino but I can’t get it to work. First I managed to make a GUI and tried to use it to simply start the batch files, one for starting the sequence and one for stop. That actually worked . But apparently Python gets unresponsive until the batch file has run the complete sequence, so I can’t use the stop command anyway.
Therefore I decided to instead try making python communicate with the arduino directly and avoid using the batch files. But when I tell Python to write data to the com port I get no reaction from the arduino.
One observation though:
If I turn on the lamp using the batch file, the lamp turns off if i run the python script.
So my question is: How do I take the command from my batch file and use it in Python?
Can the problem be that it is a mix of string and integers?
Unfortunately I don’t have access to the arduino code…
A typical command is:
:SET,255,0,100,100,100,0,255,0,0n
An example command looks like this in the batch file:
echo :SET,255,0,0,0,0,64,0,0,0n>com4
This just turns on a light.
And the test batch file looks like this:
` echo off
echo Opening COM4 port
echo –
mode com4: baud=115200 data=8 stop=1 parity=N to=off xon=off odsr=off octs=off rts=off idsr=off dtr=off
timeout 1
echo :SET,255,0,0,0,0,64,0,0,0n>com4
echo -
echo ----- Done -----
goto:eof`
I tried to make something simple in python just to test sending data:
import serial import time SerialObj = serial.Serial('COM4') SerialObj.baudrate = 115200 SerialObj.bytesize = 8 SerialObj.parity ='N' SerialObj.stopbits = 1 time.sleep(1) SerialObj.write(b':SET,255,0,0,0,0,64,0,0,0n') SerialObj.close()
But it doesn’t work.
I have also tried other ways of writing data:
SerialObj.write(bytes(b':SET,255,0,0,0,0,64,0,0,0n') SerialObj.write(':SET,255,0,0,0,0,64,0,0,0n'.encode()) SerialObj.write(str(":SET").encode() + b'255,0,0,0,0,64,0,0,0n')
But no reaction.
Tobbe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.