LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Change buffered string to number

Solved!
Go to solution

Hi,

I have sent the 1byte from PIC microcontroller (Asynch. UART)  via serial port in to labview in to VISA. The VISA received it successfully. The output from the VISA is buffer string what I can display on string indicator as ASCI or hex code. This works fine.

My problem is that, if I convert this output string from VISA to Decimal number, the converter shows 0. For a test I wired hex constant to this converter and that worked fine.

Question: What is the difference between VISA output string and the constant string? What I did wrong? How can I convert the VISA output string to the number, that I can process it by numerical functions (numeric, comparison...etc). I have tried to use Scan from string too, but I have got an Error 85. (Possible reason: The input string does not contain data in the expected format)

I am new to LabVIEW. Any help? Thanks.

Download All
0 Kudos
Message 1 of 17
(4,994 Views)

I don't know the details of the type of data you are looking to receive.  But have a look at this article and the "type cast" function in LabVIEW.  

 

https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019LOVSA2&l=en-CA

---------------------
Patrick Allen: FunctionalityUnlimited.ca
0 Kudos
Message 2 of 17
(4,969 Views)
Solution
Accepted by topic author romiadam

Hi romiadam,

 

use StringToU8Array, followed by IndexArray…

 

What is the difference between VISA output string and the constant string?

The display style settings…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 17
(4,951 Views)

GerdW, excellent.

I did not get it first how to do it, but then I found another article

https://forums.ni.com/t5/LabVIEW/String-data-to-boolean-data/m-p/3267432#M953640

where you used an image for explanation. That helped me a lot. Now it works as I wish. More over now I understand why it showed 0 and did not worked.

The 1 byte I send to Labview from PIC is converted by VISA as array of strings (it can be send to Labview 2 or more bytes). In my case it have sent only one byte, but it was stil  as array. So I need to convert it as you said to IndexArray and by index number select which index I need. In my case it is 0, because I send only one byte. If I send more bytes, than I need to select which byte I need to display. And similar way it works for boolean, if I need to check only one bit of the sent byte. See on the picture.

Thanks a lot for help.

 

0 Kudos
Message 4 of 17
(4,931 Views)

NOTE -- I started writing this before seeing GerdW's Reply and your response.  Some of what I say below might not apply, but please read it anyway ...

 

I think I figured it out, and can explain it to you.

 

But first, a question -- What can you tell us about how the PIC Microcontroller communicates with the PC?  Do you send it a Command (via the VISA Port), and it then returns a (possibly-multi-byte) Result?  This is how many VISA devices work -- until you give them a Command, they do nothing, but once you say, for example, "Who are you?", they respond with (typically) an ASCII string, almost always terminated by <CR><LF>.

 

The VISA Configure Serial Port VI has three "top" Parameters -- TimeOut (default 10 sec), Termination Character (default <LF>), and Enable Termination Character (default T).  You did not wire the last two, so your VISA Read was set to "Read until the number of bytes requested, or the termination character, was read, whichever comes first".  This is normally what you want for doing multi-byte Reads -- you specify a read of 1000 (or other suitably-too-large) number of Bytes, knowing you'll only get a dozen or so, and when the String gets read, the entire String (and not just the first character) will be present.  Incidentally, if you look at earlier Forum posts for the last 2-3 Weeks that involve VISA Issues, you will almost certainly see other Newbies making the same mistake of reading only one byte instead of the entire string being returned.

 

But this is not the only problem you have, and doesn't explain why you get 0 when you do the one-byte read.  And I think I can also explain this ...

 

Go back to the VISA Configure Serial Port VI and look at the first of the three "top" inputs.  The only one you modified was to set TimeOut from its default of 10 seconds to 1 second.  So what is happening in this code?  You start by asking LabVIEW to read a byte from your PIC Controller, not knowing if it is sending you anything.  But VISA will try to read it, and after 1 second, if nothing is there, will Time Out and return a default value (probably <NUL>, ASCII 0).  If so, then the code is "doing what you told it to do", rather than "doing what you want it to do".

 

Bottom line -- when dealing with a Remote Device and VISA Communication, the following Protocol usually works best:

  • Configure VISA, using as many of the Default Settings as possible.  The top three should be left unwired for the most commonly-used (and easiest to work with) setting.  You rarely need to set anything other than Baud Rate.
  • Before doing a VISA Read, send the Device an appropriate command (via a VISA Write) that causes it to respond with data.
  • Read the VISA Response using a Very Large (e.g. 1000) number of Bytes to get the entire <LF>-terminated String.  In most cases, this will be an ASCII representation of the data, i.e if the result is the number 123 (in decimal), you will get the string "123", corresponding to the Hex sequence 31 32 33 0D 0A (the last two characters are <CR><LF> -- many devices send <CR> before sending <LF>, and I don't remember if the VISA Read passes these to you or not).  You will need to translate the (numeric) String into a Number -- I strongly advise you to learn "Scan from String", as it is much more flexible than the individual String conversion VIs you are using.

Bob Schor

Message 5 of 17
(4,923 Views)

Hi Bob,

Thank you for your reply.

I give you more information about What I was doing. I am learning to use VISA, so I will welcome any advice (Learn to Scan from String)...

I programed in my PIC a small program blinking diode at frequency 1Hz. 500ms ON and 500ms OFF. As soon as the LED diode was turned ON onthe next line is a comant (Loading transition register) to send in to the PC a hex number 4C and when LED was turned OFF there was command (Loading transition register) to sent a hex number 4D. (PIC UART as default sends Start bit, 8bit Data, Stop Bit = in total 10 bits). So the data was sent in to PC every 500ms, thus I changed TimeOut to 1sec. As soon as I used those string converter with arrays it worked, so there was no time for TimeOut. (I think, I am not sure, I am newbie in this.) It works, but I am not sure whether it is a right way (I will learn "Scan from String" )

 

I have expected from VISA that it will receive a hex number and I can "work with it" Compare, Add, Multiply,...etc. But I have realized that the VISA has converted it as you said, as a command, Letter "L" and letter "M". And thus I needed to convert it back in to number properly.

 

Now I am only learning how to use VISA for comunication with PIC microcontroller. Next stage is to learn how to send data from Labview in to MCU.

Later I would like send  1 or more bytes from Labview in to PIC (request something to do) and then expect an answer 1 or more bytes (it will be constants number of bytes received) from PIC to Labview. For example I toggle the switch in the labview, and the MCU will turn on the heater and the MCU will frequently send the temperature reading from heater in to labview. This is just an quick example to understand what I would like to do in Labview.

 

0 Kudos
Message 6 of 17
(4,903 Views)

Hi romiadam,

 

I have expected from VISA that it will receive a hex number and I can "work with it"

Why did you expect that? The LabVIEW help clearly states you get a string from VISARead!

When you want to do calculations with this string then you need to convert the string to a number. The way you need to convert mainly depends on the data source (YOUR PIC executing YOUR software)…

 

Next stage is to learn how to send data from Labview in to MCU.

There is a VISAWrite function…

 

Later I would like send 1 or more bytes from Labview in to PIC (request something to do) and then expect an answer 1 or more bytes (it will be constants number of bytes received) from PIC to Labview.

Suggestion: use human-readable commands (i.e. no binary/hex/whatever coded data). Always append a NL (NewLine char, ASCII-10) to your command and answer strings: then you can use the default settings (apart from Baudrate, as mentioned before by Bob) for your serial communication!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 17
(4,867 Views)

Hi,

Thank you for help. I am back.

In mean time I learned a bit more about VISA read and tested the different codes (trial error, manuals, tutorials, youtube videos...) and I can say that my question "Change buffered string to number" is actually solved.

Currently I am in state that PIC sends two temperature readings (from two sensors)  string in format  T1:21.6   T2:35.4 ... (my choice of this format, where T1 indicates it is temperature 1, T2 - Temperature 2) and in the labview each measurement is sent to specific temperature indicator as a numeric value (distinguished by T1, T2 ...) and displayed on Waveform chart. I think it is a nice progress.

 

I have few questions:

 

Q1. I do not understand the sampling in the While loop in this program. Why? As I know the sampling time of while loop, if it is not set by e.g. Wait(ms), depend on PC frequency and resources. But If my while loop has not time limitation and there is VISA READ function, and if I send  two bytes every 250ms from PIC, the waveform chart is updated every 250ms. Why? If I send data from PIC every 500ms, the waveform chart is updated every 500ms. If I stop sending any data from PIC, the waveform chart is updated every 1 second, because the Timeout of VISA serial is 1000ms. If I set it to 10 sec then at no data transition from PIC to Labview the waveform chart will be updated every 10 sec. How can I set the waveform chart sampling that it will be updated constantly at e.g. every 500ms, regardless of input data sampling rate? Is it possible? I do not mind if waveform chart will miss some data.

 

Q2. This question might be dependent of Q1. I do not understand fully VISA Read function timing. How fast VISA READ scans (sampling) the COM port? Or is it independent of While loop timing? If I will set the while loop timing to 10ms and VISA READ function is inside of this while loop, will it read  data from COM port correctly? I tested it and does not. It missed some strings. Please see my block diagram. (Note: the waves on the Waveform chart are correct. I am sending there pair of temperatures to see value change on indicators 22.1 and 22.4 and 35.6 and 34.8)

Q3:

To have a clean program I have crated the SubVI. I wanted to use it as a one SubVI where the two input constants 84 and 49 as T1 and 84 50 as T2 will be set for Sub VI and I will get different output on indicator. But it does not works that way. It accepted only one pair of he constants 84 and 49 or 84 and 50 and the both indicators shows the same number. The only solution was to use two SubVIs as SubVI1 and SubVI2 and then it works. So this main VI uses now two SuVIs. But If I will have 10 temperature reading (10 rooms) then his program will need 10 SubVIs what is not practical. Is there any better way how to use only one SubVI? Or I misunderstand how the SubVI works?

 

 

Download All
0 Kudos
Message 8 of 17
(4,804 Views)
Solution
Accepted by topic author romiadam

@romiadam wrote:

Q1. I do not understand the sampling in the While loop in this program. Why? As I know the sampling time of while loop, if it is not set by e.g. Wait(ms), depend on PC frequency and resources. But If my while loop has not time limitation and there is VISA READ function, and if I send  two bytes every 250ms from PIC, the waveform chart is updated every 250ms. Why? If I send data from PIC every 500ms, the waveform chart is updated every 500ms. If I stop sending any data from PIC, the waveform chart is updated every 1 second, because the Timeout of VISA serial is 1000ms. If I set it to 10 sec then at no data transition from PIC to Labview the waveform chart will be updated every 10 sec. How can I set the waveform chart sampling that it will be updated constantly at e.g. every 500ms, regardless of input data sampling rate? Is it possible? I do not mind if waveform chart will miss some data.


The VISA Read waits until it a) times out, b) reads the termination character, or c) reads the number of specified bytes.  It is when ANY of those conditions is met.  So if you send data every 500ms, the read will take 500ms, and therefore setting your loop rate.  If you don't send any data, then the VISA Read will timeout, making your loop rate 10 seconds.

 


@romiadam wrote:

Q2. This question might be dependent of Q1. I do not understand fully VISA Read function timing. How fast VISA READ scans (sampling) the COM port? Or is it independent of While loop timing? If I will set the while loop timing to 10ms and VISA READ function is inside of this while loop, will it read  data from COM port correctly? I tested it and does not. It missed some strings. Please see my block diagram. (Note: the waves on the Waveform chart are correct. I am sending there pair of temperatures to see value change on indicators 22.1 and 22.4 and 35.6 and 34.8)


One of the rules of data flow: A loop cannot iterate until everything in the loop has completed.  So just adding a 10ms wait in the loop will not do anything if everything else in the loop takes 500ms.  So, back to above, the VISA Read will limit your loop rate.

 


@romiadam wrote:

Q3:

To have a clean program I have crated the SubVI. I wanted to use it as a one SubVI where the two input constants 84 and 49 as T1 and 84 50 as T2 will be set for Sub VI and I will get different output on indicator. But it does not works that way. It accepted only one pair of he constants 84 and 49 or 84 and 50 and the both indicators shows the same number. The only solution was to use two SubVIs as SubVI1 and SubVI2 and then it works. So this main VI uses now two SuVIs. But If I will have 10 temperature reading (10 rooms) then his program will need 10 SubVIs what is not practical. Is there any better way how to use only one SubVI? Or I misunderstand how the SubVI works?


For your initial problem, you need to make sure your VI is set to have "Preallocated clone reentrant execution".  You set this in the VI Properties under "Execution".  What Reentrancy allows is for many copies of the VI to be in memory.  With the Preallocated Clone setup, each place you call that VI will have its own memory space and therefore keep its own variables.

 

But for the long term, I highly recommend you change your data format coming from the PIC.  Make it so that you output all of the temperatures in a single message.  The way I would do this is make the data comma delimited.  For example, it T1 is 21.6 and T2 is 35.4, make your string be "21.6,35.4" and end it with a line feed (0xA, 10).  Now on the LabVIEW side, you can just use the Spreadsheet String To Array to convert that string into an array containing all of your channel data.  Then borrowing from one of my nuggets (Sporadic Waveform Chart), you can use a FOR loop to build up an array of waveforms and write that to the chart.  So now when you add more channels, your LabVIEW code can already handle it with no updates!  This is one of those "extensible" goals of code.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 9 of 17
(4,783 Views)

,

 

Thank you for clear answer on all 3 Q. Your advise of data format sending from PC to Labview is very helpfull. It makes sence. I did not know where to start, so I have started "on blind" at format T1:xx.x.

0 Kudos
Message 10 of 17
(4,770 Views)