Serial bytes

This is the place for queries that don't fit in any of the other categories.

Serial bytes

Postby imagiro1 » Thu Apr 24, 2014 3:03 am

This is my first python project.

I am connecting to a device where I send it a command and it spits back bytes. The bytes make up an array of 784 16-bit values that are sent one byte at a time.

I am able to connect to the device and send the command and receive the bytes. But I'm having trouble parsing the data. Here's the code I'm using to display the numbers.

Code: Select all
#Read some bytes
idx = 0
totalRead = 0
while( node.isOpen() ):
    if (idx == 0):
        b0 = node.read(1)
        idx = 1
    else:
        b1 = node.read(1)
        if( (len(b0) != 0) or (len(b1) != 0) ):
            reading = ((ord(b1) << 8) + ord(b0))
            print reading
        #reading = ( (b1 << 8) + b0)
        totalRead = totalRead + 1
        idx = 0
   
print ("Total Read")
print (totalRead)


The code never reaches the print ("Total Read"). I'm also open to suggestions (I welcome them with open arms) for a better way to collect data. I have the ability to create a start/stop byte if that will help. Thanks in advance.
Last edited by Mekire on Thu Apr 24, 2014 3:48 am, edited 1 time in total.
Reason: First post lock.
imagiro1
 
Posts: 6
Joined: Thu Apr 24, 2014 2:27 am

Re: Serial bytes

Postby rrashkin » Thu Apr 24, 2014 1:36 pm

How does the node get closed?
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Serial bytes

Postby imagiro1 » Thu Apr 24, 2014 1:40 pm

It's suppose to close the connection after it prints the total.
imagiro1
 
Posts: 6
Joined: Thu Apr 24, 2014 2:27 am

Re: Serial bytes

Postby rrashkin » Thu Apr 24, 2014 1:45 pm

But it never leaves the while loop as long as "node.isOpen", right? So that's a problem. As for the rest, I would read all the bytes in as a string (a byte is a character after all) and then use the struct module to unpack the values.
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Serial bytes

Postby imagiro1 » Thu Apr 24, 2014 2:15 pm

Correct. I was hoping it would timeout eventually and close the connection. But it appears, the device continues to send bytes. I'm guessing to keep the connection alive. I might look into making a dummy boolean for that while loop. And set a timer that sets it false after a period of time.

Do you have an example of the struct module method?
imagiro1
 
Posts: 6
Joined: Thu Apr 24, 2014 2:27 am

Re: Serial bytes

Postby rrashkin » Thu Apr 24, 2014 3:40 pm

___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Serial bytes

Postby imagiro1 » Thu Apr 24, 2014 4:11 pm

Great! Thanks for the help.
imagiro1
 
Posts: 6
Joined: Thu Apr 24, 2014 2:27 am


Return to General Coding Help

Who is online

Users browsing this forum: Yoriz and 3 guests

cron