Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

My problem: I am trying to open 18 Serial ports/connections. I need to then loop through each of the connections to send data to each port. Right now I have the following method of storing the serial connections.

serialPortlist = []

serialPortList.append(Serial('PORTNAME', BAUDRATE, PARITY, STOPBITS, TIMEOUT))

I then repeat the second line 17 more times and append each one to the list.

When it comes time to access the ports I would have assumed I could just do a loop like the one below:

for port in serialPortList:

    port.reset_input_buffer()

    header = port.read(2)

When I run the above I get the error: 

Error: TypeError: 'NoneType' object is not iterable.

I just want to be able to iterate through a list or array or something. If not this is going to be a super painful project.

Any ideas?

1 Answer

0 votes
by (36.8k points)

This is almost certainly a scope issue ... wherever you have defined serialPortList, is not visible to the place you are trying to iterate it ... instead, it has some default value of None or something (for sure serialPortList == None at the place you are encountering an error)

you can prove this easily

myList = []

for port in ["COM1","COM2"]:

    myList.append(serial.Serial(port))

#directly below where we instanciated it

for port in myList: # no problems here :)

    port.write("some payload\n") # assuming \n is the terminator

    print(port.readline()) # assuming \n is the terminator

my guess is you are doing something like

myList = None

def initializeList():

    myList = []

    for port in ["COM1","COM2"]:

        myList.append(serial.Serial(port))

def someOtherFunction():

    for port in myList: # ERROR myList is None!!!!

        ...

    

initializeList()

someOtherFunction()

 Do check out Data Science with Python course which helps you understand from scratch

Related questions

0 votes
1 answer
asked Jul 3, 2020 in Data Science by blackindya (18.4k points)

Browse Categories

...