How to send a value from Arduino to Python and then use that value

2024/10/12 8:18:29

I am in the process of building a robot that is remote controlled using Python to send control messages via the Internet through a simple GUI.

I have gotten part of my code working pretty well, the GUI and control systems, but I am stuck. I am trying to use a parallax ping sensor to get distance to objects information from an Arduino Mega, and send that value to my Python control script to be displayed on the remote GUI.

The main problem that I am having is how to integrate Python code that will use the already established COM port with the Arduino and send a message to tell the Arduino to poll the ping sensor and then send to a Python program which will receive the value, and then let me insert that value into my GUI.

I already have this code to control the Arduino, and it works, with my simple GUI.

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)from PythonCard import modelclass MainWindow(model.Background):def on_SpdBtn_mouseClick(self, event):spd = self.components.SpdSpin.valuedef on_FBtn_mouseClick(self, event):spd = self.components.SpdSpin.valueser.write('@')ser.write('F')ser.write(chr(spd))def on_BBtn_mouseClick(self, event):spd = self.components.SpdSpin.valueser.write('@')ser.write('B')ser.write(chr(spd))def on_LBtn_mouseClick(self, event):spd = self.components.SpdSpin.valueser.write('@')ser.write('L')ser.write(chr(spd))def on_RBtn_mouseClick(self, event):spd = self.components.SpdSpin.valueser.write('@')ser.write('R')ser.write(chr(spd))def on_SBtn_mouseClick(self, event):spd = self.components.SpdSpin.valueser.write('@')ser.write('S')ser.write('0')def on_PngDisBtn_mouseClick(self, event):ser.write('~')ser.write('P1')ser.write('p2')app = model.Application(MainWindow)
app.MainLoop()

What I would really like to do is improve the above code and add a button to click to tell Python to send a message to the Arduino to check the ping sensor and return the value. I am very literate with the Arduino code, but I just started playing with Python in the last two weeks.

Answer

Basically, you'd just send a suitable command to the Arduino, much like you're already doing, but then wait for the Arduino to send something back; the python end of it might look something like this

ser.write('foo')
retval = ser.readline() # read a complete line (\r\n or \n terminated), #or you could use read(n) where n is the number of bytes you want (default=1)
ping_data = retval.strip() # strip out the newline, if you read an entire line

of course, that'll get you a string, you'll probably want to convert it to an int or float in order to use it in calculations later (use int(ping_data) or float(ping_data) for strings, or struct.unpack in case its a byte sequence that needs unpacking to something sane first, but it all depends on how you represent the sensor data).

https://en.xdnf.cn/q/69676.html

Related Q&A

recaptcha wasnt solving by anticaptcha plugin in selenium python

Ive recently started using selenium for a project Ive been working on for a while that involves automation. One of the roadblocks in the plan was the ReCaptcha system, so I decided to use anti-captcha …

Python - check for class existance

Is there a way to check if a class has been defined/exists? I have different options on a menu, and some of them require inherited variables, so if you try to select that function before you have set …

getting a matplotlib colorbar tick outside data limits for use with boundaries keyword

I am trying to use a colorbar to label discrete, coded values plotted using imshow. I can achieve the colorbar that I want using the boundaries and values keywords, which makes the maximum value of the…

Apache Airflow - customize logging format

Is it possible to customize the format that Airflow uses for logging?I tried adding a LOG_FORMAT variable in $AIRFLOW_HOME/airflow.cfg, but it doesnt seem to take effectLOG_FORMAT = "%(asctime)s …

How can I set up Celery to call a custom worker initialization?

I am quite new to Celery and I have been trying to setup a project with 2 separate queues (one to calculate and the other to execute). So far, so good. My problem is that the workers in the execute que…

Why does print(__name__) give builtins?

Im using pycharm.2017.1.2. I installed anaconda2 with py3 environment. in Pycharm, Im using Python3 interpreter, and the code is simply:print(__name__)In Python console in Pycharm, it prints builtins.I…

List Comprehensions and Conditions?

I am trying to see if I can make this code better using list comprehensions. Lets say that I have the following lists:a_list = [HELLO,FOO,FO1BAR,ROOBAR,SHOEBAR]regex_list = [lambda x: re.search(rFOO,…

Python in operator time complexity on range()

I have the following function:def foo(length, num):return num in range(length)Whats the time complexity of this function? Noting that range() creates a Range object on Python 3, will the time complexi…

Pandas read data from a secure FTP server in Python 3

I am looking for a neat solution to read data (using either read_csv or read_sas) to a Pandas Dataframe from a secure FTP server in Python 3. All the examples I can find are many lines and some for Pyt…

How to read XML header in Python

How can I read the header of an XML document in Python 3?Ideally, I would use the defusedxml module as the documentation states that its safer, but at this point (after hours of trying to figure this …