UART - read the data

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Yury
Posts: 2
Joined: Thu Jun 04, 2015 8:07 am

UART - read the data

Post by Yury » Thu Jun 18, 2015 3:44 pm

Dear all,
I've the pyboard + simcom.
I initialize the GSM module and wanted get some data by HTTP (ex: 50kB).

my code:

Code: Select all

self.device = UART(2)
self.device.init(115200, read_buf_len=64, timeout=2000, timeout_char=5000)

....

        self._write("AT+HTTPREAD")

        http_data = "http.data"
        retries = 0
        with open(http_data, "wb") as temp:
            while retries < 10000000:
                try:
                    retries += 1
                    if self.device.any():
                        buff = self.device.read(64)
                        temp.write(buff)
                except Exception as e:
                    Log().write(e)
                    continue
....
when I receive a http content I can't receive all data.

How can I configure UART without lost some data during receive that data?

Thanks.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART - read the data

Post by dhylands » Thu Jun 18, 2015 5:59 pm

In general, you should never assume that the UART will return you all of the data in a single read.

You can adjust timeouts to make it more likely to return the entire response in one read.

If you look at the docs http://docs.micropython.org/en/latest/l ... #uart.init , there are 2 timeouts associated with the UART.

One is the amount of time to wait for the first character, and the timeout_char is how long to wait between characters. Setting timeout_char too large will increase the time it takes for you to get the data you did receive (since it has no idea what the last character is - it will wind up waiting for timeout_char miliseconds after the last character on every read - except when you fill the buffer).

I generally write receiving code to read as much data as is available, and then process it one character at a time, and then loop.
Otherwise something will happen one day where even if your timeouts are tuned, you'll wind up getting your read split into multiple parts.

Post Reply