07-28-2009 12:40 PM
Hi,
I'm reading data in from a little robot and want to convert the data into an I-16 number. I have attached a picture of the piece of code to which I'm referring. Oftentimes the numeric value of the returned ascii data is 255 which is not correct (I have been running tests so that I should be getting small negative values). Does there seem to be anything wrong with this piece of code? (I've checked the discussion forums and other links on ni.com and this seems to be correct..)
Also, if there is another way to convert from ASCII to I-16 that works or is easier, please let me know.
Thanks!
Solved! Go to Solution.
07-28-2009 01:08 PM - edited 07-28-2009 01:08 PM
Hi RachtheSabu,
It all depends on how your number is represented in the string (ie the data from the serial port). If it is just a text representation of a number, e.g. "-123" you could use the attached code snippet.
Basically, with the Type Cast vi you are currently using, you are telling LabVIEW to look at the string as if it were an integer, wheras in my code I have told LabVIEW to effectively "read" the string and determine what number it should be. In other words, you normally need to convert both the datatype AND the data.
For string to number functios, look in the palletes under Programming:String:String/Number Conversion - various other options include converting hexadecimal (ABF12C) and fractional strings (0.1234).
Shaun
07-28-2009 01:08 PM - edited 07-28-2009 01:11 PM
Off-hand I don't see anything wrong. What's the data that is returned from the VISA Read? Are you sure you're getting 2 bytes?
EDIT:
Oh, wait, I just saw that you said you're reading ASCII. Are you sure about this? If so, then you need to use the string conversion functions. I ask because on the write you seem to be writing out a byte string.
07-28-2009 01:12 PM - edited 07-28-2009 01:14 PM
How many bytes of data are you expecting back from the robot in response to your VISA Write? You have no delay between the VISA Write and Checking to see how many bytes are available. You probably are not getting all the data.
07-28-2009 01:19 PM
Wayne.C wrote:You have no delay between the VISA Write and Checking to see how many bytes are available.
Good point. I was focusing on just the data conversion part of it since the problem description implied (at least to me) that it usually worked.
07-28-2009 02:06 PM
I put in 25 ms waits after the write (before seeing how many bytes are at port) and I also look to see how many bytes I'm reading in. The time delays seemed to help - thanks for that! (which is strange because other times I haven't had problems...) I have two instances of the write, read, convert number in my code. The first time seems to be reading in usually only 1 byte. The second time it reads anywhere from 2 to 4 bytes and when 3 bytes are read in I always get the value of 255. I'm not quite sure how to handle this 255..Maybe I'm accidentally reading a space which I want to ignore. Is there an easy way to check for a space and if there's one to skip over reading the space but grab anything before or after (and still read the string if there is no space)?
I tried putting in the mentioned string conversion instead of the type casting, and I got a value of 0 everytime so I think the casting is working as I'd like.
The string contents being returned by the robot are similar to this : ÿù
Any suggestions on the 3 byte/space problem would be great - thanks for your help!
07-28-2009 02:13 PM
07-28-2009 02:19 PM
07-28-2009 02:22 PM
What space? If it is sending a 2-byte value that is an I16 number, then there shouldn't be any spaces in the data stream (unless of course a byte just happens to be dec 32, hex 20)
It would be best if you posted an example of the string that you are getting from the VISA read.
07-28-2009 02:30 PM