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

五迷三道 提交于 2019-12-03 08:14:52

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).

mrkva

Maybe check out the Pyduino project:

pyduino is a library which allows you to communicate with Arduino boards loaded with the Firmata protocol from within Python. It currently supports version 2 of the Firmata protocol.

First, let me say that the prior answers are good, helpful, and directly relevant. My comments are more general and apply to anyone implement a bidirectional data flow to and from an Arduino. The basic idea is to design your data flow so that it is human typeable for the data going to the Arudino sketch and human readable for the data coming from the Arduino sketch. This won't always be possible, but frequently it is.

The key idea is to run your Arduino sketch with the Serial Monitor a few times. You can find the Serial Monitor under Tools in the IDE menu. You can also type Ctrl-Shift-M to invoke the Serial Monitor.

The Serial Monitor displays what the Arduino sketch sends back to you. However, it also lets you type in data that gets sent to the Arduino sketch. In other words you test and debug both sides of the serial data flow, just using the Serial Monitor.

Look at what shows up. It will frequently be quite helpful assuming your sketch tries to send data back via Serial.print(). A few notes. Make absolutely sure that the baud rate set inside the Serial Monitor exactly matches the baud rate in your sketch (9600 is a good choice in almost all cases).

The second note is critical. Bringing up the Serial Monitor forces a reset on the Arduino board. Your sketch starts over (always). This is a good thing because it gives you a fresh run each time. Note that you can force a reset, just by setting the baud rate to 9600 (even if it is already 9600). This lets you run many tests inside the Serial Monitor without having to restart the Serial Monitor each time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!