LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VISA Read Byte count switching between 2 values

Solved!
Go to solution

I'm fairly new to LabVIEW programming so if there's a better way to do the programming for this VI let me know. I have been going through a lot of tutorials already so I'm trying to apply what I've learned so far as of now.

 

But anyway my setup is that I'm using a cRIO-9068 and my goal is to have a user-input of decimal numbers to be sent over through the serial port 1 (RS232) and converted into hex format at the response end. My colleagues were saying they wanted a text file to be created first and then we can read in from that file all those decimal numbers at once so that we receive the hex numbers at the end simultaneously ( for an external board that needs hex input). I already created a separate VI for creating that text file with no problem but my issue comes with the VI that's converting the decimal integers into hex. I tried debugging and taking out the conversion to see what I get if I just send over the decimal integers as is with no conversion and right now the byte count for write VISA is showing me correctly 44 bytes from the 11 rows of numbers in my text file. Currently, what is happening when I select my boolean switches to ON for write and read, and run the program after setting up my Port on the cRIO and the text file in the file path, I'm getting a response at the end that is switching between two values of giving me everything from my text file successfully and nothing at all. Also, the byte count from the read VISA is also switching between 44 and 1 simultaneously so I think it has something to do with that but I'm not sure what it is exactly. I'm hoping someone can look at my program to see if they know what the possible cause of this and how I can fix it so it just shows my read response as the text file of decimal numbers indefinitely until I click stop for my while loop. Also, I was using the example Continuous Serial Write and Read.VI as a template and just modified it. And I did run the Continuous Serial Write and Read.VI as it was originally and got the string successfully but my program is a little different than that so I had to modify it. I also did a loopback test on MAX and it said it was working successfully so I'm just trying to make my program to work with this serial port. I attached my two VI's one for the serial communication programming to do the conversion and the other for creating the text file. Also, the text file if you wanted to use the same numbers as me. 

 

My serial configuration is:

8N1, 115200 Baud

Download All
0 Kudos
Message 1 of 13
(4,349 Views)

Actually, the problem here is very simple (although it took me a while to find...)

 

Your text file ends with a \n character - which is then being read as a valid termination character.

If you set the "End Write on Term Char" false, then you just get the 44 bytes you want. However, then you can't send them one by one in a loop of some sort, which maybe you'll want? If you need the Write Term Char, you should remove the \n from your text line, since effectively you're sending (with mixed tabs and spaces, by the way)

 

1\s2\t\s3\t\s\s4\s\t\s9\n\n and that's parsed as 

First read: 1\s2\t\s3\t\s\s4\s\t\s9\n

Second read: \n.


GCentral
0 Kudos
Message 2 of 13
(4,279 Views)

 


@cbutcher wrote:

Actually, the problem here is very simple (although it took me a while to find...)

Your text file ends with a \n character - which is then being read as a valid termination character.


...

and for this reason I never send the term char anymore but explicitly add it myself to all my VISA (or other coms) code and write most of the strings in \code format. - possible Rube Goldberg, but easier to debug.

 

I've seen it too many times and I've got bitten by different termination chars too.

 

James

CLD; LabVIEW since 8.0, Currently have LabVIEW 2015 SP1, 2018SP1 & 2020 installed
0 Kudos
Message 3 of 13
(4,261 Views)

Is there a way to exclude that \n when creating my file to read in? Because right now I'm wiring the numbers I put in to a write delimited spreadsheet.vi

0 Kudos
Message 4 of 13
(4,230 Views)

Probably not, but you can use the Trim Whitespace (String Palette) function to remove trailing carriage returns and other whitespace characters when you read it out of the file.

 

There's currently a pretty active discussion in the forums about the Trim Whitespace VI included with LabVIEW and some faster implementations - if you care you can take a look, but the built-in version is probably fine for most usage.


GCentral
0 Kudos
Message 5 of 13
(4,222 Views)

@NickBl wrote:

Is there a way to exclude that \n when creating my file to read in? Because right now I'm wiring the numbers I put in to a write delimited spreadsheet.vi


You are setting ASRL End Out to Term Char, so it is appending a termination character to your write.

 

Oops, I see that this is in the file you are sending.  I guess you have to eliminate it manually.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 13
(4,219 Views)

@cbutcher wrote:

Probably not, but you can use the Trim Whitespace (String Palette) function to remove trailing carriage returns and other whitespace characters when you read it out of the file.

 

There's currently a pretty active discussion in the forums about the Trim Whitespace VI included with LabVIEW and some faster implementations - if you care you can take a look, but the built-in version is probably fine for most usage.


I think the Trim Whitespace.vi would eliminate the first newline, too.  I guess you could reverse the string, search and replace (once) for the newline character, replace it with an empty string , then re-reverse the string.  You could also just lop off the last character.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 7 of 13
(4,217 Views)

Trim Whitespace has an enum input to specify if you want front and back, just front or just back, if you only wanted to lose the end, you can specify "end of string". 


GCentral
0 Kudos
Message 8 of 13
(4,211 Views)

True, but you can't specify how many white spaces to remove from an end.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 9 of 13
(4,204 Views)

Actually I'm hoping to see if I can just run this continuously in a while loop without it ending the read on a termination char but instead of a stop control. Is that possible in serial communication or does it need to stop by a termination char? When I run it and set my end read on termination char to false the program will encounter an error at the VISA read.

0 Kudos
Message 10 of 13
(4,196 Views)