Strange packet received

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
niclas@hedhman.org
Posts: 3
Joined: Fri Mar 06, 2020 12:34 am

Strange packet received

Post by niclas@hedhman.org » Fri Mar 06, 2020 12:55 am

Hi,
I am using MicroPython on PyCom module LoPy4, and in-house compile;
Pycom MicroPython 1.18.2.r7 [df9f237-dirty] on 2019-06-13; LoPy4 with ESP32

I am not a Python programmer, but very experienced in many other languages, so even if my Python questions look stupid, please don't treat me like an idiot.

I have encountered a strange situation that happens "sometimes" and the frequency of it occurring seems to be LoPy unit dependent, i.e. some of those I have do it much more commonly and others not at all.

Code: Select all

        received_packet = self.lora_sock.recv(512)
        
        if isinstance( received_packet, bytes ) :
            packet_length = len(received_packet)
And I get the error;

Code: Select all

TypeError: object of type 'function' has no len()
So, can a 'bytes' instance also be a function, and lacking the len() function? Could "received_packet" possible "change" from "bytes" to "function" between the isinstance() call and the len() call?

And yes, I am not dreaming this. The program is quite short, output goes to serial port and that is captured to log file on a Raspberry Pi "as-is", and the whole stacktrace in it and the line number matches the source code.

Any help is greatly appreciated.

// Niclas

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

Re: Strange packet received

Post by dhylands » Fri Mar 06, 2020 4:15 am

Without seeing your code, I'll guess that your variable does contain a function. If you did this:

Code: Select all

data = foo
but you really meant to do this:

Code: Select all

data = foo()
then data will contain the function foo and not the results of calling the function foo.

niclas@hedhman.org
Posts: 3
Joined: Fri Mar 06, 2020 12:34 am

Re: Strange packet received

Post by niclas@hedhman.org » Sat Mar 07, 2020 8:59 am

Nah. The code I posted is literally the 3 lines (+a blank in between) from the code. The LoRa module (from pycom-micropython-sigfox repository) is written in C, and interfaces with Python in ways I have no idea.

My question could perhaps be phrased; How is it possible that "bytes" doesn't have "len()" function sometimes?

The whole function in question looks like below. And I am clueless how it can pass the "if isinstance" and then fail on "len()"

Code: Select all

    def loraCycle(self):
        received_packet = self.lora_sock.recv(512)
        if isinstance( received_packet, bytes ) :
            packet_length = len(received_packet)
            if packet_length >= 8:
                pycom.rgbled(0x002000)
                rssi = self.lora.stats()[1].to_bytes(2, "little")
                snr = int(self.lora.stats()[2]).to_bytes(2, "little")
                print('SensorData:' + ",".join(map(str, received_packet + rssi + snr)))
                pycom.rgbled(0x000000)
            else:
                if packet_length  > 0 :
                    rssi = self.lora.stats()[1].to_bytes(2, "little")
                    snr = int(self.lora.stats()[2]).to_bytes(2, "little")
                    print('Unexpected:' + ",".join(map(str, received_packet + rssi + snr)))
                    self.pingcounter = self.pingcounter + 1
                if self.pingcounter > 1000 :
                    print('ping')
                    self.pingcounter = 0
        else:
            print( "Unpexpected return value:" )
            print( type(received_packet) )
            print( received_packet )

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

Re: Strange packet received

Post by pythoncoder » Sat Mar 07, 2020 9:25 am

how it can pass the "if isinstance" and then fail on "len()"
Indeed. Very odd. Here is how I'd set about debugging it

Code: Select all

    def loraCycle(self):
        received_packet = self.lora_sock.recv(512)
        if isinstance( received_packet, bytes ) :
            try:
                packet_length = len(received_packet)
            except TypeError:
                print('Error', type(received_packet), received_packet)
            # Rest of code
Peter Hinch
Index to my micropython libraries.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: Strange packet received

Post by Christian Walther » Sat Mar 07, 2020 10:07 am

niclas@hedhman.org wrote:
Sat Mar 07, 2020 8:59 am
How is it possible that "bytes" doesn't have "len()" function sometimes?

Code: Select all

>>> bytes = type(print)
>>> a = print
>>> isinstance(a, bytes)
True
>>> len(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'function' has no len()
I have no reason to suspect that that’s what’s happening here, but it’s a possibility.

niclas@hedhman.org
Posts: 3
Joined: Fri Mar 06, 2020 12:34 am

Re: Strange packet received

Post by niclas@hedhman.org » Thu Mar 26, 2020 6:07 am

@pythoncoder
I eventually got around to dig into this and took your advice...

Code: Select all

    def loraCycle(self):
        received_packet = self.lora_sock.recv(512)
        if isinstance( received_packet, bytes ) :
            try:
                packet_length = len(received_packet)
            except TypeError:
                print('Error', type(received_packet), received_packet)
            # Rest of code

Code: Select all

Error <class 'bytes'> b''
Isn't that type expected??

Post Reply