So recently I find the need to send float number to Arduino, from a Python script, using serial communication.
Right now the way I'm using is just basically decomposed the number into 2 bytes (for numbers bigger than 255) and also another byte that indicates the sign of that number (positive or negative number).
But as time goes by, I find that this method is not very readable. It's a big project and I want it to be more maintainable for future developers.
So now I'm looking into what other ways I can do this. For example in Python, if I want to receive 2.55, I can just write:
Also, please confirm which direction the data is being sent. Initially, you said "send float number to Arduino, from a Python script" but then later in the post, you seem to be saying the data is going in the opposite direction, from the Arduino to the Python script...
PaulRB:
What's wrong with the code you just suggested?
Also, please confirm which direction the data is being sent. Initially, you said "send float number to Arduino, from a Python script" but then later in the post, you seem to be saying the data is going in the opposite direction, from the Arduino to the Python script...
It supposed to communicate both ways. The way I wrote (Pi to Arduino) it's good enough for me but I'm not sure how to do the same with the other way around.
vitsensei:
It supposed to communicate both ways. The way I wrote (Pi to Arduino) it's good enough for me but I'm not sure how to do the same with the other way around.
Ok. It would have been good to have understood that from the first post.
What kind of Arduino are you using? The basic 8-bit models have limited ram and program memory, so the functions to translate between float and string have been reduced to the minimum. The more modern 32-bit Arduino often have a wider choice, making things more user-friendly.