I'm trying to have an Arduino Uno communicate via serial with a python script and I'm having issues with the responses. The arduino has a number of commands to execute based on whatever is received.
Everything works fine using the IDE's serial monitor, I introduce number, if it's a valid command I get a response if it's not valid whatever was sent is echoed back.
With this code no response is ever displayed. 3 happens to be a valid command, but invalid commands give me the same result. Looking at the arduino the RX LED never flashes, but the TX LED does, so it is receiving something, so I at least should get an echo back, but no. As far as I can tell Serial.available() is always 0.
As a sanity test I tried using docklight to send commands to the board,and it appears to work except I only the arduino only replies after 3 commands have been sent, which is incredibly weird.
Your description seems to be different from the code snippet you have shown us.
Python code sends a byte containing the ASCII code for the character '3', but your switch..case does nothing (at least it seems to do nothing, even if you show us just the '8' command) except for "default" (invalid?) commands, where it sends the command back to sender. Some more code lines (a full switch..case, just to start, and possibly variable definitions ans setup procedures) could help us to better understand what's happening. For instance: are you sure on setup() you set the same serial speed as the Python one ("Serial.begin(9600);")?
If from Python you send the command '3', it should switch the internal LED.
If it doesn't work, it's probably something either on the link (you crossed TX and RX, yes?) or on Python side like the open mode (must be 9600 baud, 1 stop, no parity, no Xon/Xoff, no CTS/RTS hardware handshake...). Check the python serial open statement.
As an addon, my code as previously worked on a Due. The RX and TX lines can't really be crossed since I'm only connected to the Arduino using the USB port, but I'll try the simplified version
this does not offer all options for debugging
the code should switch on off an LED in case something at all is received
If you happen to have a USB-to-TTL-interface you could connect this interface to IO-pins 0,1 and connect a serial terminal program to let print what gets received
or connect 1 LEDs to IO-pins and light up the ASCII-code of the received character
or use software serial or a microcontroller that hat to hardware UARTS to make visible what really is received
I don't have a TTL interface at hand right this instant, but I can get one tomorrow.
As for the LED toggle... the LED didn't toggle, but I suspect that its broken. What I did end up doing is add a serial print statement in one of the switch cases, which as triggered correctly and worked as expected through the serial monitor. This time I tried Tera Term as well which worked, but the python script still doesn't work.
Is there some encoding I should be doing on the python side? Right now I'm sending a literal '3' as bytes
The plot thickens. I decided to follow up on the issue that the arduino only replies on the 3rd try when using literally any other serial terminal other than the one in the IDE(TeraTerm and Docklight) and siply repeated my python code 3 times.
import serial
arduino=serial.Serial('COM25',9600,timeout=1)
arduino.write(b'3')
response=arduino.read(1)
arduino.write(b'3')
response=arduino.read(1)
arduino.write(b'3')
response=arduino.read(1)
print(response)
This appears to work consistently, if I remove the last write it won't work. I'm honestly stumped, does the serial terminal in the IDE send some invisible characters I'm missing? A LF or CR?
The Uno resets when you open the serial port; it can take some time. Maybe that time is the time that the python code sends the first two '3' and only the 3td one is detected by the Arduino.
Put a delay (2 seconds or so, maybe start with 5) in your python script after opening the port.
Although there is not much wrong with your choice of category where you posted the question, I've moved your topic to the section dedicated to communication with the PC.