0

I would like to send such array for example via pyserial:

[49, 56, 48] or [82]

I tried such solution before sending:

print(list(str(180).encode()))

And it gives me array as I want. But when I tried to send it via pySerial, it said that I need to have buffer instead of list. In general I'm trying to get some analogue from kotlin:

byteArrayOf('O'.code.toByte())


"70".toByteArray()

Which gives me such arrays for sending them to Arduino. I tried sending maps, dicts, and so on, but did not succeed. Whether it's possible to have something similar in Python? python sending code:

import json
import struct

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

data = [49, 56, 48]
ser.write(json.dumps(data) + '\n')
ser.write(str('R').encode())
ser.write(str(80).encode())
12
  • 3
    encode() returns a byte array, so just use .write(str(180).encode()) Commented Jun 27, 2024 at 19:21
  • I tried that too :( and it did not reach the arduino script at all Commented Jun 27, 2024 at 19:28
  • 1
    Then you have a different problem. Commented Jun 27, 2024 at 19:34
  • 1
    Please post a minimal reproducible example. Commented Jun 27, 2024 at 19:38
  • Is the Arduino side perhaps expecting a newline or other terminator character? Commented Jun 27, 2024 at 22:01

1 Answer 1

0

This works for me on Windows. Obviously you need to replace my 'COM10'.

import serial
import time

ser = serial.Serial('COM10', 9600, write_timeout=1)

time.sleep(4)   # Wait for Arduino to reboot

# bytes_to_send = [65]
bytes_to_send = [65, 66, 67, 68, 69]

count = 0
while True:
    for byte in bytes_to_send:
        ser.write([byte])
        print(f"{count} Sent {byte}")
        count += 1

The four second delay is needed because the Arduino reboots when you open the serial port.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.