1 year ago

#371163

test-img

Lazuli

Microcontroller serial stops working when mu serial is closed

I am trying to control some NeoPixel lights, and because the Raspberry Pi can only control one light at any given time, I decided I would just get a microcontroller to do it, specifically the Adafruit ItsyBitsy M4 Express, and just send serial data through the USB connecting the two to control them. I got that working, using PySerial to send data to the microcontroller. However, after the RPi script sends data 8 times, it stops working and I have to reload the script. Additonally, by stop working, I mean that the script gets stuck sending data and never ends. The strange part is that if I open the MU serial after my script has started running, I can A: see the data being sent in, and B: it never stops. It goes past the 8 issue and keeps going. RPi Code:

import serial
import time
from random import randint
a = time.time() #Unimportant, was looking to see how quickly Serial could be re-defined
try:
    ser = serial.Serial(port='/dev/ttyACM1', baudrate=115200, write_timeout=0.05)
except:
    ser = serial.Serial(port='/dev/ttyACM0', baudrate=115200, write_timeout=0.05) 
print(time.time() - a)
EnterKey = "\x1A\r\n"
e = 0
while True:
    e+=1 
    d = []
    for number in range(0, 30):
        d.append([randint(0, 255), randint(0, 255), randint(0, 255)])
    d = ((str(d)) + EnterKey).encode()
    ser.write(d)
    time.sleep(1)
    print(e)

Microcontroller code:

import board
import supervisor
import neopixel
import time
a = neopixel.NeoPixel(board.D5, 30, auto_write=False)
supervisor.diable_autoreload()
while True:
    b = input("automated")
    RST = []
    c = ""
    R3T = []
    started = False
    for value in b:
        if started == True:
            if value == '[':
                started2 = True
                ending = False
            elif value == ']':
                if ending == True:
                    started=False
                else:
                    RST.append(int(c))
                    c = ""
                    ending = True
                    started2 = False
                    R3T.append(RST)
                    RST = []
            elif started2 == True:
                if value.isdigit():
                    c += value
                elif value == ",":
                    RST.append(int(c))
                    c = ""
        elif value == '[':
            started = True
    for value in range(0, 30):
        a[value] = R3T[value]
    a.show()
    print(a)

I tried being lazy and just re-defining the serial object after a write timeout, but that does not work. (Errno 16] Device or resource busy). I then went around looking for Raspberry Pi settings seeing if anything was getting in my way, but found nothing. I ultimately gave up and came here. If you have any ideas, please tell me!

python

usb

microcontroller

pyserial

mu

0 Answers

Your Answer

Accepted video resources