LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview RS232 Hexadecimal to Number Conversion

Hello,

I am having issues communicating with a gas analyzer via RS232 in Labview.

I have a gas analyzer that can record data for 10 different gases via RS232 and analog voltage output. Each gas has an output between 4-20mA on the analog output, where 4mA is 0% concentration of a gas, and 20mA is the max %. For example, a reading of 20mA for Nitrogen would equal ~80%, and a reading of 20mA for Oxygen would read ~20%. I currently have it setup capturing data with the NI USB-6008 DAC module, but am having issues with some of the gas percentages displaying incorrectly, which I believe might have something to do with the analog output.

Right now, I am trying to capture data via RS232 in labview but am having some issues. I found a Basic Serial Write and Read program that I've tried modifying to meet my needs. I need to send the hex command 11 01 01 ED every time I want to read data and it will output something like this: 16 15 01 00 00 00 00 00 04 00 00 00 82 07 FE 00 00 00 00 00 0F 1E 8B 91. After the the 6th digit, each group of 4 digits represents gas percentage in hex. "07 FE" represents the hex value for Oxygen. Converting this number to decimal, you get 20.46, which is 20.46% concentration of the gas.

I am able to write and read the hex data successfully, but am having trouble converting it to decimal. When I enter the read command, Labview outputs the string above like this: 1615 0100 0000 0000 0400 0000 8207 FE00 0000 0000 0F1E 8B91. I do not need the first 6 and last two digits, but do need the rest. I'm not exactly sure what the best approach would be here, but I was thinking of removing these digits from the string and regrouping the rest into groups of 4 digits. I'm not sure how to successfully do this, but I've made some code in attempt to fix this.

I've attached a screenshot of the output correlation with the gas data under the "GasData" png file. I've also attached 2 VI's that I've made under "Basic Serial Write and Read Hex" and "arrayEx". The first one is the main program that I'm trying to use to communicate via RS232, and the arrayEx.vi is something I've been messing around with to get a better grasp on arrays. Right now, I enter the hex output from the analyzer into the "String" and "Delimited String" fields, and "\s" into the delimiter field. The "output array" stores each digit into one row, where as the "string array" stores 4 digits into one row. If I could figure out how to remove the first 6 and last 2 digits, or just regroup everything in the way I need, I believe I can handle the rest. I've only been using labview for a few months now, so my knowledge is limited and any help would be greatly appreciated.

Thank you!

0 Kudos
Message 1 of 15
(939 Views)

Hi luka,

 

I guess you receive a string, which is shown hex-formatted in your message:

Just two functions to get an array of integer values!

I also guess you can do the scaling to 0.01% on your own…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 15
(930 Views)

@luka.zz wrote:

Right now, I am trying to capture data via RS232 in labview but am having some issues. I found a Basic Serial Write and Read program that I've tried modifying to meet my needs. I need to send the hex command 11 01 01 ED every time I want to read data and it will output something like this: 16 15 01 00 00 00 00 00 04 00 00 00 82 07 FE 00 00 00 00 00 0F 1E 8B 91. After the the 6th digit, each group of 4 digits represents gas percentage in hex. "07 FE" represents the hex value for Oxygen. !


Where exactly is the 6th digit? Which digit is the start of 07 FE? The string control of your second VI is in normal format? Shouldn't that be hex???

Message 3 of 15
(926 Views)

See if this can give you some ideas.

 

altenbach_0-1658769781538.png

 

We don't really know if negative values are allowed (gas percentages are typically not negative...:) ) . If so, the empty array constant array should be I16. Check the documentation. The reason I also show the header output is because you might actually want to validate the header and generate an error if it differs from the expected value (not shown).

Message 4 of 15
(920 Views)

Note that you could use an advanced format of "%.2f%%" for the output array. You might also want to coerce it to [0..100].

 

altenbach_1-1658770128786.png

 

 

Message 5 of 15
(911 Views)

This is what I was looking for. Thank you!

It seems you already figured it out, but by first 6 digits and last 2 digits, I meant the first 6 and last 2 characters in the output string. In this case,  "1615 01" and "92". Also, you are correct in that negative numbers are not allowed.

The arrayEx.vi was not the main program, just something I was experimenting with. I wanted to show some of the attempts I made.

I've updated my code and added a while loop to acquire and graph data continuously until stopped. The issue I am having now is that it seems like only one gas is being plotted in a zigzag fashion. Is it possible to have all gases have there own plots in one single graph, or to seperate a couple gases in one graph and a couple in another? Current graph looks like Graph1 attached, but I would ideally like it to look like Graph2. I would also like to change the x-axis (time) to display a range from 0 to 20 minutes, but when I change it and click ok, nothing happens and it reverts back to 1023.

Download All
0 Kudos
Message 6 of 15
(874 Views)

1. DO NOT USE THE BYTES AT PORT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (still not enough emphasis)

This is the main reason I hate the Simple Serial example.  It should not be used in 99.9% of applications to tell the VISA Read how many bytes to read.  In this case, just tell the VISA Read to read 24 bytes (the full length of the returned data message).

 

2. Your chart needs a 2D array in order separate the different channels.  In this case, just add a Build Array before the chart.  You may need to transpose the 2D array in order to get the channels arranged properly.

 

3. If you are going to use a display mode other than "normal", you really need to show the Display Style.  Right-click on the constant/control/indicator and choose Visible Items->Display Style.

 

I cleaned up your VI.  See if this works for you.


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
Download All
Message 7 of 15
(862 Views)

Sorry, I cannot look at your VIs because I don't have LabVIEW 2021 on the current machine.

 

Charts typically graph one plot when a scalar is wired, but if you wired a plain 1D array, it simply assumes that you update multiple points of one plot. One solution is to convert to a 2D array as Gerd already showed.( Another possibility is to convert the N element 1D array to a cluster with N elements, but then you need to make sure that the number of plots never changes because you need to fix the cluster size at compile time.)

Message 8 of 15
(820 Views)

Crossrulz's vi helped me successfully graph all gases on one graph, each with their own plot.

The only issue I'm having now is separating array elements and plotting them on different graphs. My work around for now is adding more graphs with all gas plots and unchecking the "visible" box in the properties>plots menu for each plot I want to hide. Ideally, I'd like to graph CO, CO2, & H2 onto one graph, and N2 & O2 on another.

I've tried combining them with the array subset & build array functions, and checking "Concatenate Inputs" then graphing, but it did not work. I've also tried using the insert into array function by inserting H2 into a subarray of CO & CO2 and plotting, but that did not work either.

This is the best solution that I have right now, but it still does not work as I need it to:
ReadGasData1.PNG

 





 

 

 

 

 

 

 

 

 

 

It takes the original array as an input and grabs index 4 (H2), and index 0 (CO) & 1 (CO2) and outputs them as 2D sub arrays. I can only read the first two gas values (H2 & CO2) in the digital display, and it does not plot anything. I'm not sure if I even need the sort 2D array function, but it works better than anything else right now. Maybe I should separate each of the 3 gases into their own sub array, sort, and graph?

I will also need to export and save these values into an excel file. When I was using the usb-6008 daq module and daq assistant function, I was able to split the signals and export to excel like this:

ExportToExcel.PNG



 

 

 

 

 

 

 

It does not seem like this will work for the array values. What is the recommended way to do this?

I've attached my updated vi. If you open it, you can ignore the graphs that are not connected right now.

0 Kudos
Message 9 of 15
(788 Views)

Hi luka,

 


@luka.zz wrote:

It takes the original array as an input and grabs index 4 (H2), and index 0 (CO) & 1 (CO2) and outputs them as 2D sub arrays. I can only read the first two gas values (H2 & CO2) in the digital display, and it does not plot anything. I'm not sure if I even need the sort 2D array function, but it works better than anything else right now. Maybe I should separate each of the 3 gases into their own sub array, sort, and graph?


Why do you use the 2D array for all those "grab index" purposes?

Why not use the "original" 1D array???

Why use those Sort functions when you want to index an element from your array - there's a function called "IndexArray"!?

Why do you use ExpressVIs again to build a graph? Why not use BuildArray as you should have learned by now???

 

Did you learn the basics of array handling in LabVIEW yet?

 


@luka.zz wrote:

I will also need to export and save these values into an excel file. When I was using the usb-6008 daq module and daq assistant function, I was able to split the signals and export to excel like this:

ExportToExcel.PNG



 

 

 

 

 

 

 

It does not seem like this will work for the array values. What is the recommended way to do this?


Simple programming rule (not just for LabVIEW): whenever you need to repeat an operation you should use a loop!

Why don't you use a loop (with a shift register) to set the signal properties???

 

But:

  • Why don't you define the signals "correctly" for your DAQmx task? That's the place where you define the signal name! No need to do this again and again when trying to save data to a file!
  • Again: get rid of all those ExpressVIs and DDT wires! Use the waveforms provided by DAQmxRead!

 


@luka.zz wrote:

I've attached my updated vi. If you open it, you can ignore the graphs that are not connected right now.


Unfortunately you use LV2021, which not all of us already use. It would be much easier (atleast for me) when you would downconvert your VI to LV2020 (File->Save for previous)…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 15
(779 Views)