0

I want to send a message from Rpi to Atmega328 via UART and there must be only 8 bit message (address, address, direction, number of steps [5 bits]).

import RPi.GPIO as GPIO
import serial
import time,sys


var=0b11110000
SERIAL_PORT = "/dev/ttyS0"
ser=serial.Serial(SERIAL_PORT,baudrate=9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS)
time.sleep(1)
print("Sending")
ser.write(var)
ser.close()

But i got this

Sending
Traceback (most recent call last):
File "UART_2.py", line 11, in <module>
ser.write(var)
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 558, in write
return len(data)
TypeError: object of type 'int' has no len()

What should I do to send it correctly?

1 Answers1

0

It looks like you should give a byte array to ser.write instead of sending a single value. Try using var=[0b11110000] which has a length, that the integer 0b11110000 doesn't have.
If you can encode your byte as a string, you can also find more answers at pySerial write() won't take my string

B. Go
  • 1,436
  • 4
  • 15
  • 22