Without knowing what you entered, and seeing more of the Pi's output, all I can say is that the delays are useless, as is discarding serial data.
In the above example I entered "a" in python script and then it prints "Arduino: RX: a 97" but then arduino also thinks that string "RX: a 97" was sent to him over serial, since that is what he printed to serial. And the loop begins with python telling me "Arduino: RX: R 82", "Arduino: RX: X 88" and so on.
Why are you sending data TO the Arduino?
I need arduino to respond to raspberry pi instructions.
Hi, I tried your first script on a Mac with the standard USB connection and had different results. No endless loop (good), but on the other hand most characters sent by the Arduino were received by the python script, and only a few by the serial monitor (the two sets were complementary). If only the serial monitor or the python script are open the behavior is normal.
That seems normal, since Serial.read() removes the read byte from serial, as far as I know. So if you're running two serial monitors on the same computer, than its just a matter of who reads it first and deletes it so the second doesn't have anything to read.
In my case, I don't have arduino connected to raspberry pi with USB. They communicate over wires via GPIO RX/TX (pi) and RX/TX pins (on arduino). I have arduino connected to another computer with USB for uploading scripts and monitoring.
So I would say there was nothing wrong in your first attempt, and you should probably concentrate on configuration settings. Did you follow the other instructions given in the link (changes in inittab and console settings, connecting the ground)? I also wonder what are your results if you run only the serial monitor or the python script.
I've changed all the settings and connected the ground as described in linked post. It also doesn't change anything if I close down one serial monitor.
Thanks for the responses, you gave me think that it might not be the code. So I looked at the wiring (disclaimer: I'm better with software than with electrical circuits
I changed the wiring just a little and only the raspberry pi to arduino part. So instead of transistor I used diode, for that small protection.
pi(TX) >-------▶|-----|----[1k ohm]-------[ground]
With this circuit it works as it should, no magic loops. So the initial problem solved.
However, I'm not completely sure what actually
changed, why wouldn't it work properly with transistor and how should I fix it? If I change resistors or voltage in the circuit with transistor, it works even less, or am I missing something. One question answered two new appear