Go Down

Topic: Issues with Bytes, and Values while Using Python 3 and Arduino (Read 201 times) previous topic - next topic


I am using Python 3 to send bytes to an Arduino over serial,  receive them back (and print them in my python monitor). They go through the micro controller and it seems there is some weird behavior applied to it during this path. For instance, when I do:

a = b'\7'
op(board).send(a) #This is syntax specific to the program that runs Python

(which sends a byte value of 7 to the board) and:


It prints out b'7' as expected. However, when I try the same code but with a = b'8' it prints out: b'9265' and I have no idea what the difference is.

Similarly, for anyone who has experience using Arduino and Python, is there any explanation as to what format I could use to easily send Serial data to / from the Arduino? It seems as though I can only send bytes objects, but I am not sure how these are interpreted by the Arduino. It looks as though the byte value is converted to its utf-8 "symbol" value (i.e. as a string) then sent to the Arduino, which reads it is a binary value.

However, when I send this read value back to Python, it then sends a byte object with the decimal value. Since this is the case and I may be receiving a stream of values (from an analog pin for instance) what is the best way to rapidly decode these byte values into decimals

In other words, this would be fine except I would have to call chr(val) on every value. If I pass in the value b'151' (meaning I want a decimal value of 151 the arduino echoes back b'495349' and using int(chr(val)) does not work.

The real problem is that I want to be able to easily send values from the GUI of my program, (for instance, an analog write value). I am not sure how I should format the bytes object when I send it. 


I suspect your problem has to do with confusion between signed and unsigned values. However since you haven't posted either your Arduino or your Python code I can do no more than guess.

This demo shows simple Python to Arduino communication. It just uses bytes.

It is also fairly simple to directly send values for Arduino LONG and INT variables and if that is what you are interested in I can provide some demo code.

By the way these examples are in Python 2.7 - I don't use Python much and I haven't tried v3.


Go Up