LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

ascii to I16

Solved!
Go to solution

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!

0 Kudos
Message 1 of 23
(4,756 Views)

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. 

String to Integer.png

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

 

Message Edited by shew82 on 07-28-2009 02:08 PM
0 Kudos
Message 2 of 23
(4,725 Views)

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.

Message Edited by smercurio_fc on 07-28-2009 01:11 PM
0 Kudos
Message 3 of 23
(4,723 Views)

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.

Message Edited by Wayne.C on 07-28-2009 02:14 PM
0 Kudos
Message 4 of 23
(4,709 Views)

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.

0 Kudos
Message 5 of 23
(4,693 Views)

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!

0 Kudos
Message 6 of 23
(4,675 Views)
If you read three bytes, that is obviously not an I16 (2 bytes). Why don't you just set the byte count to read to be 2?
0 Kudos
Message 7 of 23
(4,672 Views)
I know that I should be reading 2 bytes (the company with the robot specifies a signed 16-bit value and specifieds the data bytes to be 2 signed bytes).  Is there a way to skip over a space if it accidentally gets read?
0 Kudos
Message 8 of 23
(4,667 Views)

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.

0 Kudos
Message 9 of 23
(4,662 Views)
If you should be reading 2 bytes, then do so. Don't use the VISA Bytes at Serial Port to determine how many to read. It seems to me that you are using it inappropriately.
0 Kudos
Message 10 of 23
(4,659 Views)