Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7303

General • badger 2040 usb serial data?

$
0
0
I am trying to send a image file using serial over usb, to display an image directly on the screen, this should be possible right?

I am several iterations in to something that sounded simple, host machine is linux, the Pico is on /dev/ttyACM0 using the none-badger-os firmware

any time I try run my script to send data it will constantly fail at sending with KeyboardInterrupt errors

Received 1475/4736 bytes
Traceback (most recent call last):
File "<stdin>", line 56, in <module>
KeyboardInterrupt:

there was a revision where I tryed to make it ignore KeyboardInterrupt but it would constatnly catch KeyboardInterrupt and never get to 4736 bites, it was supposed to write what it got too before it failed but that never worked either.

anyway this is (one) version of the code I been using I made so many iterations I lost track of which version was what...

receiver script

Code:

import badger2040import sysimport time# Initialize the Badger 2040badger = badger2040.Badger2040()# Expected image size (296x128 pixels, 1-bit, 4736 bytes)IMAGE_SIZE = 4736# Function to display an image from bytesdef display_image(image_data):    try:        print("Attempting to display image...")        badger.clear()        badger.set_image(image_data)        badger.update()        print("Display updated successfully")    except Exception as e:        print("Error displaying image:", e)        badger.set_pen(15)  # White background        badger.clear()        badger.set_pen(0)  # Black text        badger.text("Error displaying image!", 10, 60, 1)        badger.update()# Function to display waiting messagedef display_waiting():    badger.set_pen(15)  # White background    badger.clear()    badger.set_pen(0)  # Black text    badger.text("Waiting for image", 10, 60, 1)    badger.update()# Flush serial buffer to clear stray datadef flush_serial():    try:        while sys.stdin.buffer.read(1):            pass  # Read and discard any available bytes    except:        pass# Main loopwhile True:    # Display waiting message    display_waiting()        # Flush serial buffer to avoid stray data    flush_serial()        # Buffer to store incoming image data    image_data = bytearray()        # Read data from USB serial with timeout    start_time = time.ticks_ms()    print("Waiting for serial data...")    while len(image_data) < IMAGE_SIZE:        try:            char = sys.stdin.buffer.read(1)            if char:                image_data.append(char[0])                print(f"Received {len(image_data)}/{IMAGE_SIZE} bytes")            else:                time.sleep(0.01)  # Brief delay if no data        except KeyboardInterrupt:            print("Interrupted by user")            break        except Exception as e:            print("Serial read error:", e)            break                # Timeout after 10 seconds (10000 ms)        if time.ticks_ms() - start_time > 10000:            print("Timeout waiting for serial data")            break        # Check if we received a complete image    if len(image_data) == IMAGE_SIZE:        print("Full image received, displaying...")        display_image(image_data)        time.sleep(2)  # Wait for display to settle    else:        print(f"Incomplete image: {len(image_data)} bytes received")        badger.set_pen(15)        badger.clear()        badger.set_pen(0)        badger.text("Incomplete image!", 10, 60, 1)        badger.update()        # Delay to prevent CPU overload    time.sleep(0.1)

and the sending script

Code:

import serialimport time# serial port (e.g., 'COM3' on Windows, '/dev/ttyACM0' on Linux)port = '/dev/ttyACM0'ser = serial.Serial(port, 115200, timeout=1)# Load a 1-bit image (must be exactly 4736 bytes)with open('image.raw', 'rb') as f:    image_data = f.read()    if len(image_data) != 4736:        print(f"Error: Image is {len(image_data)} bytes, expected 4736")        ser.close()        exit(1)# Send the image data in chunks to avoid buffer overrunsprint("Sending image...")chunk_size = 128  # Send 128 bytes at a timefor i in range(0, len(image_data), chunk_size):    ser.write(image_data[i:i + chunk_size])    ser.flush()  # Ensure data is sent    time.sleep(0.05)  # Small delay to avoid overwhelming receiver    print(f"Sent {i + chunk_size}/{len(image_data)} bytes")print("Image sent")time.sleep(2)  # Wait for receiver to processser.close()
I even wondered if maybe usb-3 wasn't playing nice so I connected it to usb 2 with no real change.

any ideas what could be causing it not to work, I spent half a day on it because I am stumped at this point?

Statistics: Posted by iamfox — Mon Apr 28, 2025 11:15 pm



Viewing all articles
Browse latest Browse all 7303

Trending Articles