Page 3 of 3

Re: working with UART and Bytearrays

Posted: Sun Jan 24, 2016 2:34 am
by EasyRider
x

Re: working with UART and Bytearrays

Posted: Sun Jan 24, 2016 7:25 am
by dhylands
If the UART is opened with a non-zero read_buf_len (the default is 64) then the characters are collected using an IRQ. However there is currently no callback associated with the UART.

The way that I normally deal with this is that I have a "process" routine which is called from the main loop. It checks if characters are available and runs them through a state machine and either calls a packet_received function if a complete packet was detected, or just buffers the characters if the packet is incomplete.

Re: working with UART and Bytearrays

Posted: Sun Jan 24, 2016 7:59 am
by EasyRider
x

Re: working with UART and Bytearrays

Posted: Fri Feb 16, 2018 12:53 pm
by donikuy
Hi,

Is there any examples on UART IRQ? I would like to detect when certain characters are received.

Regards,
donikuy

Re: working with UART and Bytearrays

Posted: Fri Feb 16, 2018 7:39 pm
by dhylands
There is no callback at the python level for the UART IRQ on the pyboard. I normally have my main loop be a polling loop and call the any function http://docs.micropython.org/en/latest/p ... b.UART.any to see if any characters are available to be read and then read the characters one-by-one.

Re: working with UART and Bytearrays

Posted: Sat Feb 17, 2018 8:06 am
by pythoncoder
It's worth considering the uasyncio approach which treats UARTs as stream devices. The following example demonstrates full-duplex concurrent input and output using line-structured data. Test using a loopback (link X1 and X2 on a Pyboard).

Code: Select all

import uasyncio as asyncio
from pyb import UART
uart = UART(4, 9600)

async def sender():
    swriter = asyncio.StreamWriter(uart, {})
    while True:
        await swriter.awrite('Hello uart\n')
        await asyncio.sleep(2)

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.readline()
        print('Recieved', res)

loop = asyncio.get_event_loop()
loop.create_task(sender())
loop.create_task(receiver())
loop.run_forever()

Re: working with UART and Bytearrays

Posted: Fri Jan 25, 2019 2:24 pm
by lnsri22
Hi Dave and Peter!!

I am facing this issue of the byte order being changed or the bytearray is partially updated.


I am trying to interface a liquid level sensor that outputs a packet of length 9 bytes every 3 seconds.

'3E' being the start byte of the packet, various bytes represent the temperature and level of liquid along with the crc8 dallas checksum added at the end. Packet will look like '3E01071BBA01450EAB'(AB checksum)

I have tried the below so far with no success.

Code: Select all

idx = 0
bytes_read = 0
while idx < len(self.data):
	#print("Looking for self.data")
	if(self.uart.any()):
		bytes_read = self.uart.readinto(self.mv[idx:])
		print('Got {} bytes of self.data'.format(bytes_read), hexlify(self.data[idx:idx+bytes_read]))
		idx += bytes_read
This always prints

Got 9 bytes of self.data b'ab3e01071bba01450e'

The last byte is always placed at the beginning , it seems

Also the buffer is update partially(specific locations of the buffer is changed with the previous bytes being the native of previous instance of the buffer)

But I cross checked this with a serial terminal (Docklight) in my PC. It looks perfect

What am I missing??

Any help is greatly appreciated

Re: working with UART and Bytearrays

Posted: Tue Mar 12, 2019 4:05 pm
by kurtsvl
In micropython 1.10 UART.any() not works.

Re: working with UART and Bytearrays

Posted: Tue Mar 12, 2019 10:10 pm
by dhylands
Are you sure that the AB isn't from the previous packet?

If the sender sent most of the packet followed by a slight pause and then sent the AB, you could be picking things in a seeming incorrect order.

The sensor has no idea when you're reading the data, so if your code happened to start mid-packet then this would occur.

Normally, most packets have some type of start byte which is used to synchronize things. When you first start, you generally wind up throwing away characters until you get to a packet boundary. I always write my packet parsers to deal with one character at a time.

Re: working with UART and Bytearrays

Posted: Mon Sep 30, 2019 1:49 pm
by ta1db
pythoncoder wrote:
Sat Feb 17, 2018 8:06 am
Test using a loopback (link X1 and X2 on a Pyboard).
Worked flawlessly on Nucleo_F401RE as well with a single modification UART 4 -> 6 and with link PC6 to PC7 accordingly.

Code: Select all

import uasyncio as asyncio
-  uart = UART(4, 9600)
+ uart = UART(6, 9600) #or any other valid baudrate
Thank you very much.