While True loop Stopping

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
darkglove
Posts: 9
Joined: Wed Jul 22, 2020 11:12 am

While True loop Stopping

Post by darkglove » Wed Aug 12, 2020 4:21 pm

My While True loop runs once and then keeps stopping at

Code: Select all

data = stdin.buffer.read(1)
until it receives something else

I did try

Code: Select all

if stdin.buffer.read(1) != "": data = stdin.buffer.read(1)
but that doesn't help

If there is a problem with

Code: Select all

data = stdin.buffer.read(1)
then shouldn't it thow an exception? It doesn't...

I need it to keep looping so that is will run

Code: Select all

if received_msg:

Code: Select all

while True:
    try:
        print("Here 1")
        data = stdin.buffer.read(1)
        print("Here 2")
        gatheredData.extend(data)
        if str(data.decode()) == "\r":
            xbee.transmit(addr16 if addr16 else addr64, gatheredData)
            gatheredData = bytearray(b'')
        received_msg = xbee.receive()
        if received_msg:
            payload = received_msg['payload']
            print("%s" % (payload.decode()))
    except Exception as e:
        print("Nope")

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

Re: While True loop Stopping

Post by dhylands » Wed Aug 12, 2020 5:16 pm

stdin doesn't have an any() method, so you can either use the USB_VCP device or UART device depending on which microprocessor you're using.

http://docs.micropython.org/en/latest/l ... SB_VCP.any
http://docs.micropython.org/en/latest/l ... e.UART.any

I normally wrap these accesses up in my own class, along these lines:
https://github.com/dhylands/json-ipc/bl ... sb_port.py

Test code for host side
https://github.com/dhylands/json-ipc/bl ... al_port.py

You might also be able to use uasyncio (unfortunately I haven't played with this much so I'm not sure if it works with stdin or not).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: While True loop Stopping

Post by pythoncoder » Thu Aug 13, 2020 8:32 am

dhylands wrote:
Wed Aug 12, 2020 5:16 pm
...
You might also be able to use uasyncio (unfortunately I haven't played with this much so I'm not sure if it works with stdin or not).
As a cooperative scheduler uasyncio won't rescue you from a blocking method like stdin.buffer.read(1). Its stream I/O mechanism works well with UARTs. Doubtless it will work with the VCP interface but I haven't tried it.

The other option is to use threading but that is a last resort in my opinion.
Peter Hinch
Index to my micropython libraries.

darkglove
Posts: 9
Joined: Wed Jul 22, 2020 11:12 am

Re: While True loop Stopping

Post by darkglove » Thu Aug 13, 2020 9:51 am

Thank you both for your replies.

I'm newbing my way through this and still not sure which way to go

This code is running on an XBee3

The documentation says for UART to use stdin.

I can't use UART as it doesn't exist if I try to import it, these are in a 232 shield so that would mean the USB_VCP is out, right?

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

Re: While True loop Stopping

Post by dhylands » Thu Aug 13, 2020 1:42 pm

That same documentation also tells you how to do a non-blocking read.

darkglove
Posts: 9
Joined: Wed Jul 22, 2020 11:12 am

Re: While True loop Stopping

Post by darkglove » Thu Aug 13, 2020 2:52 pm

Of course it does.

So I now know what blocking means!

Thankyou

Code: Select all

data = stdin.buffer.read(-1)
if str(data) != "None":
    etc
Working

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

Re: While True loop Stopping

Post by dhylands » Thu Aug 13, 2020 4:07 pm

It would be more efficient to just use:

Code: Select all

  if data:
rather than converting data to a string, or perhaps using

Code: Select all

if data is not None:
Be aware that read may return more than 1 byte, so be prepared for that scenario.

darkglove
Posts: 9
Joined: Wed Jul 22, 2020 11:12 am

Re: While True loop Stopping

Post by darkglove » Thu Aug 13, 2020 5:18 pm

I did actually try

Code: Select all

 if data:
but it didn't work, though this might have been for another problem and I fixed the wrong thing...

I also thought about the

Code: Select all

if data is not None:
but stopped when it was working!

Will have a play to make more efficient.

Also, yes, I immediately noticed that I received multiple bytes which works better for me anyway.

Thanks

Post Reply