Transform int to python byte

Asked

Viewed 721 times

2

Hello, Need to transform a value int to byte.

I tried to do it this way:

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)

angulo = 90

ser.write(angulo)

#while 1 :
print ser.readline()

I need to send a value to the Arduino, at first, only accepted in byte. Thank you!

1 answer

5

Only use:

valor = bytes([90])

See more here.


To convert byte to integer, use:

int.from_bytes(meus_bytes, byteorder='big')

If it is big endian, and

int.from_bytes(neus_bytes, byteorder='little')

if it’s little endian.

  • Thank you! man, I need, now on Rduino, do the opposite, receive this value in bytes and turn into int. How to do?

  • See my issue.

  • value[0] - in Python 3 when you recover values of an byte object using dices, you see them as integers (no signal).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.