LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

String to Number Conversion

Solved!
Go to solution

I am working to create a VI to control a stepper motor (HW34-696) through a drive (STAC6-S).  One of the things I would like to do is to send a command and recieve a response.  For example I write the command DI/r which should return the number of steps the motor has taken in the form of 1DI=100000/r.  I can easily remove the 1DI= from the string, but ultimately I would like to convert the numeric part of the string into a numeric in labview.  The VI the compay who manufactured the drive and motor has a VI which supposedly does this, but when I use their method which uses a String to HEX and then decimal to string I get 0.   

 

apm.PNG

 

I  am not sure what the purpose of the final conversion is. Also I would like to have the numeric part of the string (i.e. 100000) converted into numeric form so that I can do some additional calculations with it.  I have tried different string conversion methods, but none seem to work for me.   Below I have simplifed their code into the following. Any ideas would be a big help.  Thanks! 

front.PNG

back.PNG

0 Kudos
Message 1 of 5
(2,919 Views)
Solution
Accepted by topic author Cseaman

It sounds like the string you are trying to parse is of the form ID1=FFFFFF\r, where "FFFFFF" represents a numberic value returned as a Hexadecimal string.  You understand how to isolate the FFFFFF part.  The first of the two final String functions reads this hex string and returns to you its value (100000, in hex, = 1048576.  The final function really isn't necessary -- all it does is convert this number back to a string so you can look at it as a String Variable (you can also simply wire the "blue wire" to a numeric output, which, by default, will give the decimal value, 1048576).

 

You might look into using the Scan from String function, as well -- I generally prefer it to the functions you are using.

 

Bob Schor

Message 2 of 5
(2,900 Views)
Solution
Accepted by topic author Cseaman

As Bob said, you already seem to have everything working converting the hexadecimal string to a numerical representation.

 

 

As for why their code would produce a 0, they were only removing the first three characters for some IP= case and so still had the equals sign at the beginning of the string in the 1DI= case, so the conversion from hexadecimal string function failed to identify the string.



Message 3 of 5
(2,886 Views)

SInce your device terminates the strings with carriage returns you should set the termination character on the port to \r and get rid of the Bytes at Port nodes. They will cause you lots of unecessary problems.

 

That usually eliminates the need for delays between Writes and Reads because the VISA Read will wait for the termination character and then return immediately.

 

Do the configuration of the port and the closing of the session outside the loop where the writes and reads occur.

 

Lynn.

Message 4 of 5
(2,871 Views)

Thanks for all the suggestions. I have the code working.  I will see if I can make it more elegant now.

0 Kudos
Message 5 of 5
(2,855 Views)