Reading Nan From Serial Device?


particleman

Recommended Posts

Hello, 

I'm working with a servo control (serial communication described here: http://www.pololu.com/docs/0J40/5.e) and have it partially working (I can send commands), but am unable to parse data sent from it. I am trying to 'Get Position', for which you send 0x90 and the servo channel number (1 in this case). 

 

I am using the following sequence:

 

// see info on serial communication w/ Maestro here: http://www.pololu.com/docs/0J40/5.e
global servo1
global string servoout
 
device.maestro.Purge()
      try
               device.maestro.Write(format("%c",144)+format("%c",servo1)) //command configuration 144 = 0x90
               servoout = device.maestro.Read(2) //documentation indicates outputs will be 2 bytes...
      catch()
            ?strLastError
            Delay(0.5)
      endcatch
?servoout
 
And when I run it, I see the following in the Comm monitor:
 
Tx: \x90\x01
Rx: \xD0\x07
 
so I figure this should be written to the 'servoout' string in this sequence, which I could then parse and translate into a position. However, I have only managed to get 'NaN' as a string. I've tried 'read(0)' and 'readuntil(10)' and various other configurations and I either get a time-out error or 'Nan'. 
 
I figure this might be a formatting or synchronization issue, but I'm a bit at a loss for what to try next. 
 
Any ideas?
 
Thanks!

 

Link to comment
Share on other sites

 First, consider using chr()/chra() and asc()/asca() instead of format("%c",...)

 

Most likely you declared servoout as a number:

 

global servoout

 

and then tried to declare it as a string like you did above.  My guess is that if you simply save and restart DAQFactory you'll be good.  Once you've declared a variable you can't change its type, which is one advantage of privates since they go away.  If you want to make your globals go away, you can do:

 

clearGlobals()

 

or like I recommended, simply restart.

 

Note also that if the response is \xD0\x07 then doing ? servoout isn't going to display anything because those aren't valid ASCII characters.  Instead, do:

 

? showHidden(servoout)

 

so it will print it in slash notation (albeit in decimal, not hex).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.