Python Forum
UART how to read 300 bytes ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UART how to read 300 bytes ?
#1
hello,

I actually have to read a row of 300 bytes from a home made scanner.
I send a request to send it, and then I get the 300 bytes sent.
Of course I have to store it somewhere.
is that possible in one go?
or is that limited?
I read somewhere that there is a maximum of 32, but i am not really sure what that actually means.
thank you.
I'm not French, but from the netherlands
Reply
#2
Should be no problem getting 300 bytes in one go, unless there is a limitation in the device you are using. Is this a raspberry pi or something similar? What is the format of the data in the 300 bytes? You might have to unpack the bytes to get in a format you can use.
Reply
#3
the data is comming from a atmega32,
and i wil received it in a raspberry pico.

if it is possible i want to store it binair, just "1" and "0".
I'm not French, but from the netherlands
Reply
#4
There is no such thing as binary in python. You can get a str which is the binary representation of an int, but that is the characters "0" and "1", not one bit values.

Will the data be 300 0/1 values or 300 bytes, each having 8 bits of 0/1 data?
Reply
#5
Yeah, there might be a solution for it.
Reply
#6
Quote:Will the data be 300 0/1 values or 300 bytes, each having 8 bits of 0/1 data?
that last one, 300 bytes with 8 bits in it.
I'm not French, but from the netherlands
Reply
#7
from machine import UART


def get_scanner_row(uart: UART) -> bytearray:
    """
    Send request to scanner, receive 300 bytes and return them.
    """
    response_buffer = bytearray(300)
    
    # https://docs.micropython.org/en/latest/library/machine.UART.html#machine.UART.write
    uart.write(b"your command to get the resopnse of 300 bytes\r\n")

    # https://docs.micropython.org/en/latest/library/machine.UART.html#machine.UART.readinto
    # blocking call until response_buffer is full 
    uart.readinto(response_buffer)
    return response_buffer


# https://docs.micropython.org/en/latest/library/machine.UART.html#class-uart-duplex-serial-communication-bus
uart = UART(1, 9600)

# blocks until 300 bytes are received
# the response is a bytearray
response = get_scanner_row(uart)

# printing the data byte for byte
# in binary form
for index, value in enumerate(response):
    print(f"Byte {index:>3d}: {value:0b}")
Code is not tested. Just as a starting point, how to send data via micropython.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Smile 
that's clearly code written by an expert.
I'll see if I can figure that out Smile
thanks for your help.
I'm not French, but from the netherlands
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  send bytearray over UART trix 9 1,966 Sep-30-2024, 01:08 PM
Last Post: trix
  raspberry pico remapping uart ? trix 0 520 Sep-28-2024, 08:44 AM
Last Post: trix
  UART write binary code trix 3 1,767 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 1,779 Dec-28-2023, 05:14 PM
Last Post: trix
Question How to understand the received bytes of ser.read? pf2022 3 3,500 Mar-24-2022, 11:37 AM
Last Post: pf2022
  How to clean UART string Joni_Engr 4 3,907 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 9,146 Jul-17-2021, 04:19 PM
Last Post: bill_z
Smile send hexadecimal package variable UART santos20 2 2,887 Oct-30-2020, 11:40 AM
Last Post: Larz60+
  wrong data reading on uart fahri 6 4,997 Sep-29-2020, 03:07 PM
Last Post: Larz60+
  replace bytes with other byte or bytes BigOldArt 1 12,412 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020