I have a Python application running on a PC that needs to interface with an Arduino.
All communication is initiated by the PC; the Arduino should only respond.
Communications must exchange texts and values.
Help please, nothing works for me.
I have a Python application running on a PC that needs to interface with an Arduino.
All communication is initiated by the PC; the Arduino should only respond.
Communications must exchange texts and values.
Help please, nothing works for me.
General concepts
Numeric values must be represented as a string to be passed and converted to a numeric for use on both platforms. It is possible to send binary data, if you wish, but it is uselessly complicating your life.
All Arduino-PC communication must be in "UTF-8", which requires encoding/decoding the data on the PC side for sending/receiving.
Finally, you always have to send a '\n' at the end of the string, to mark its end. This is required both ways.
python
We will use
pyserial
to communicate from PC with the Arduino.First we must open the serial port that communicates with Arduino. This requires identifying the serial and its speed, which must be compatible with the counterpart:
In this case, the serial port is
/dev/ttyUSB0
, which is platform dependent, and the speed is 9,600 bauds.To send a string, you must first encode it to "UTF-8" using
encode()
:To receive a response, use
readline()
. This method reads from the serial until it detects a\n
. If it doesn't find it, it blocks. The response comes in UTF-8, and must be encoded to transform it into a Python string:If the answer expresses a numerical value, it must be converted appropriately, using
int
orfloat
.Arduino
Arduino requires that the function
loop
execute in each pass in the minimum possible time, which implies that the signal must be read character by character.Although Arduino has a method
readString()
that returns a data typeString
from the serial, it is not recommended to use it. This method blocks until a timeout\n
is detected , which introduces all sorts of random errors. Also, it complicates encoding.The general pattern is:
First, we declare
buffer
asString
outsideloop
so that it retains its value during execution. In this variable we accumulate the read characters until we detect the\n
one that marks the end.If there are characters to read in the serial, we will read and accumulate them one by one, for speed and simplicity.
Upon detecting the
\n
, we process the buffer and clear it, to receive the next text.Everything received by Arduino is text; if we want to consider it as a value, the class
String
offers several functions liketoInt()
,toFloat()
, etc.When sending responses from the Arduino to the PC we don't need to encode anything (it works in ASCII, UTF-8 compatible) and the final carriage return is automatically added by the
println()
.show
Arduino
This Arduino code reads from the serial. The texts are returned transformed to uppercase; the values are accumulated and for each one received, it returns the accumulated sum.
[Of course, it doesn't include error handling.]
python
This code sends text and values to the Arduino, receives the responses and displays them on the console.
[Of course, it doesn't include error handling.]
Result