UART Reading



  • Hi,

    I have been trying to read from a dust sensor (Amphenol SM-UART-04L).

    When I'm communicating the UART as below, it's giving output.

    while i < 60:
             if uart.read(1)==b'B': #hex 0x42
                data=uart.read(32)
                print(Data:", data)
    

    But when I have been trying it like this:

    while i < 60:
            data=uart.read(32)
            if uart.read(1)==b'B': #hex 0x42
               print(Data:", data)
                
    

    I am getting error: TypeError: 'NoneType' object is not subscriptable
    Is there any protocol to follow to read from UART? Why the second one isn't working out?
    Below is the communication protocol from the datasheet:
    UART-1.PNG
    UART-2.PNG
    Please enlighten me.
    Thanks



  • The error is self-explanatory. You are trying to subscript an object which you think is a list or dict, but actually is None. This means that you tried to do:

    None[something]
    

    This error means that you attempted to index an object that doesn't have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. 'NoneType' object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn't define the getitem method . This is a design principle for all mutable data structures in Python.



  • This post is deleted!


  • @Oridroo You need to check if there is data to read. Could be done with "if uart.any() >= 32" or check if data was received with "if data != None".

    Johan


Log in to reply
 

Pycom on Twitter