Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
send bytearray over UART
#1
hello,
i thought that i post this question a half hour aggo, but i can not find it back ?

no problem i asked again:

i want to send a bytearray over UART, but i'm not verry succesfull so far.
problem is, how to fill uart.write() ?
you can see in the first part of my posted code, how i fill the array.
the 2e part is how i want to send it.
how i want to do it now is "byte by byte" but I think this is not the right method
can anybody tell me how i have to do this ?
thanks in advance.
if scanner_uart.any():	# is anything comming from the scanner ?
                r15 = bytearray(230)
                scanner_uart.readinto(r15)               
                print("r15",[byte for byte in r15])
            
            #************** SEND DATA TO 2E PIC0 **********************************
                
            MAX485.value(1)			# 1 = write and 0 = read MAX485    
                
            for i in range (229):
                
                byte_to_send = r15 [i]
                scanner_uart.write(b'\byte_to_send')		#
                time.sleep_ms (1)
#                print(r7[i])
                
            MAX485.value(0)			# 1 = write and 0 = read MAX485 
I'm not French, but from the netherlands
Reply
#2
note line 8, 1 is an integer 1, not 0x01 (bit 1), is this what you wanted?
what is the error you are getting?
Reply
#3
line 8 is a digital output, i change the name of this output to make it more clear (it was not a good name Doh )
I'm not French, but from the netherlands
Reply
#4
I do not understand your code. What is this supposed to do?
if scanner_uart.any():
I assume scanner_uart is a serial.Serial object, but serial.Serial objects don't have a method named "any".

Why do you do this?
r15 = bytearray(230)
scanner_uart.readinto(r15)   
instead of this?
r15 = scanner_uart.read(230)
And why do this:
            for i in range (229):
                 
                byte_to_send = r15 [i:i+1]  # Needs to be bytes, not int
                scanner_uart.write(byte_to_send) 
                time.sleep_ms (1)
Instead of:
scanner_uart.write(r15[:229])
Is the 1 ms wait important?
Reply
#5
(Sep-28-2024, 11:31 AM)deanhystad Wrote: I do not understand your code. What is this supposed to do?
if scanner_uart.any():
I assume scanner_uart is a serial.Serial object, but serial.Serial objects don't have a method named "any".
it waits till a mesage is comming on the UART

Quote:Why do you do this?
r15 = bytearray(230)
scanner_uart.readinto(r15)   
instead of this?
r15 = scanner_uart.read(230)
i think that is the same, only write shorter
Quote:And why do this:
            for i in range (229):
                 
                byte_to_send = r15 [i:i+1]  # Needs to be bytes, not int
                scanner_uart.write(byte_to_send) 
                time.sleep_ms (1)
Instead of:
scanner_uart.write(r15[:229])
Is the 1 ms wait important?
i think also it is the same but shorter, but shorter is simpler so better (but sometimes it doesn't become any easier to read).
the 1 mSec. was just for test some things, maybey i don't need this.
I'm not French, but from the netherlands
Reply
#6
Are you using micropython? That is the only place I can find deinit() and any() for something related to uarts.

The micropython documentation says this about deinit()
Quote:You will not be able to call init() on the object after deinit(). A new instance needs to be created in that case.
But I don't think you need to deinit the uart. This thread discusses changing the pins attached to the uart bus.

https://github.com/orgs/micropython/discussions/11506
Reply
#7
indeed micro python, but I thought that was clear, am I in the wrong section?
I'm not French, but from the netherlands
Reply
#8
In this thread you did not mention you are using micropython. That would be a good thing to mention next time. I found out you are using raspberry pi pico from reading this post:

https://python-forum.io/thread-43303-pos...#pid181814

And my response here, about remapping the UART to different pins, is a response to that post.

So if you want to read about remapping your UART to different pins, go look at this discussion:

https://github.com/orgs/micropython/discussions/11506
Reply
#9
@micropython

from machine import UART

uart = UART(1, rx=..., tx=..., baudrate=...)

send_buffer = bytearray(b"Hello World\r\n")
bytes_sent = uart.write(send_buffer)

print(f"{bytes_sent} bytes were sent over serial")
The return value of uart.write: https://docs.micropython.org/en/latest/l...UART.write
Larz60+ likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
sorry for the misunderstanding Cry , and thanks for the link. I'll take a good look at that.
I'm not French, but from the netherlands
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  raspberry pico remapping uart ? trix 0 432 Sep-28-2024, 08:44 AM
Last Post: trix
  bytearray weirdness or newbee newness Curbie 3 1,058 Sep-18-2024, 01:25 AM
Last Post: deanhystad
  UART how to read 300 bytes ? trix 7 1,279 Aug-22-2024, 12:54 PM
Last Post: trix
  UART write binary code trix 3 1,441 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 1,636 Dec-28-2023, 05:14 PM
Last Post: trix
  Value error when converting hex value to bytearray shubhamjainj 7 13,093 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 33,048 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 6,354 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 1,528 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 4,563 Nov-21-2022, 07:25 PM
Last Post: Chepilo

Forum Jump:

User Panel Messages

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