How to send a value from Arduino to Python and then use that value
Question: 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](http://arduino.cc/en/Main/ArduinoBoardMega), 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 model
    
    class MainWindow(model.Background):
        def on_SpdBtn_mouseClick(self, event):
           spd = self.components.SpdSpin.value
        def on_FBtn_mouseClick(self, event):
           spd = self.components.SpdSpin.value
           ser.write('@')
           ser.write('F')
           ser.write(chr(spd))
        def on_BBtn_mouseClick(self, event):
           spd = self.components.SpdSpin.value
           ser.write('@')
           ser.write('B')
           ser.write(chr(spd))
        def on_LBtn_mouseClick(self, event):
           spd = self.components.SpdSpin.value
           ser.write('@')
           ser.write('L')
           ser.write(chr(spd))
        def on_RBtn_mouseClick(self, event):
           spd = self.components.SpdSpin.value
           ser.write('@')
           ser.write('R')
           ser.write(chr(spd))
        def on_SBtn_mouseClick(self, event):
           spd = self.components.SpdSpin.value
           ser.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).

