Hello. I’m kind of new to Python and Arduino but I started to work recently with these two, so I need some help.
This is the scenario:
We are working with an Arduino Uno and a Rpi 4. They are conencted via USB cable and we connect to the Rpi 4 via work Wireless network.
We use Windows computers, so we are connecting to Rpi 4 via Putty.
The Arduino Uno has a code that is prepared to send a JSON object to its serial port when it catches a string “getData” on its serial port! So, my colleague asked me to write a script to automatically send this string and fetch the JSON object into a variable.
At work I was able to get the JSON object in Putty even if completely out of format, but I got it, using 2 separate scripts (goal is to use only one). I don’t have a screenshot of the result from work, though!
On my free time, I’m also trying to do the same at home, but I use Debian instead of Windows and therefore, I use minicom instead of Putty. Other than that, I just wrote a small program to try to mimic the program my colleague uploaded on Arudino at work. Meaning that if I send the string “getData” to Arduino serial port via minicom, I get the JSON object back at minicom.
Something like this:
We don’t see the string “getData” here because I deactivated
local Echo in minicom.
If I type something else I just get a “Not detected” message.
So, the goal is, with a single script, to mimic this behavior. Send “getData” to Arduino serial, wait for the JSON object, fetch it into a variable and print it out to the terminal window.
So far, I was not able to reproduce this behaviour with a single script, so I tried to split the script in 2 so that I could run both at the same time. I don’t want to detail all the things I tried because the post will become huge and nobody will read it.
So, I have this code for now, in a single script:
import serial ser = serial.Serial( port = '/dev/ttyUSB0', baudrate = 115200, timeout = 10, xonxoff = False ) ser.reset_output_buffer() ser.reset_input_buffer() val = 0 command = '' command = input("Enter command: ") val = ser.write(command.encode(encoding = 'ascii', errors = 'strict')) print("Waiting bytes: ", ser.in_waiting) print("Bytes written: ", val) in_data = '' in_data = ser.read_until(b'}') print(in_data)
This is the result, after the
timeout of 10 secs:
$ python3 serialWriteRead.py Enter command: getData Waiting bytes: 0 Bytes written: 7 b''
The problem is that I can’t even catch the “getData” string reaching the Rx line of Arduino on my scope (at home), using the script! I can’t also catch it if I use minicom where I can see the Tx (??) led blinking per each charater of the string “getData” that I type in minicom window. I can only catch the JSON object reply when using minicom, on my scope!
When I added the (??) above, I meant that I was expecting to see the string “getData” on the Rx line of the Arduino and the JSON object on the Tx line of the Arduino. What I see is, while using minicom, is the opposite: “getData” on Tx line and the JSON object on the Rx line. But while using the script above, I only see Tx line blinking. I assume is on the
ser.write() command! No Rx line at all!
So, what can I do/change to try to make this work?