Unable to read data from GPS using UART

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Unable to read data from GPS using UART

Post by nikhiledutech » Thu Jan 25, 2018 10:31 am

I am currently using micro python on STM32Fdisc board, attempting to interface with GPS module. I have tried your code and also tested my code but i am unable to read data from GPS.
Do you have any idea about this problem, why it happen ?

Here is the code:
import pyb
import pyb,micropython
from pyb import LED, Pin, UART

rx = bytearray(250)
i = None
uart = UART(2,115200)

data = bytearray(255)
j = 0
#Capture Data in the Rx buffer
for i in range(255):
# x = uart.readchar()
# print(x)
if i == 255:
I printed the buffer and the value is 0. So do you have any idea what can be the reason ? I have checked the GPS module its working using C/C++ and also the UART is working fine.

Posts: 40
Joined: Sun Jun 26, 2016 9:15 am

Re: Unable to read data from GPS using UART

Post by Iyassou » Fri Sep 06, 2019 12:08 pm

Replying to this old thread in case you didn't find the answer or any likely problems.

You initialise the rx buffer with size 250, and then call pyb.UART.readinto on that buffer 255 times.

The documentation for the readinto states that if you specify a buffer only (and not a buffer and integer), it will read len(buffer) bytes into it. So calling the readinto method on rx with no integer results in 255 overwrites of rx with whatever is on the UART bus.

There is also no delay between the reads. You've initialised the UART bus with an id of 2 and a baudrate of 115200, so that means the timeout is at the default value of 0.

Because of this, the UART times out when there's no immediately available data on the bus: this is why rx ends up being empty. Adding a timeout value similar to the wait time between new GPS sentences should fix this.

Amending your code to the following should yield better results:

Code: Select all

from pyb import UART

rx = bytearray(250)
uart = UART(2, baudrate=115200, timeout=1000) # timeout in ms

Post Reply