LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Empty serial port read buffer

Solved!
Go to solution

Hello everyone. I am currently programming a vi that takes an input string from a serial device and parses that string. The only issue I am having is that, (presumably) in between reads, when the buffer empties, the vi will parse this empty buffer and return an error message because the now empty string doesn't conform to the standard string that is expected. How do I go about fixing this, assuming I have interpretted the source of the error correctly?

 

Edit: I wanted to add that searching turned up advice that suggested setting the Visa read control to synchronous. This doesn't seem to have made a difference.

 

Edit 2: Attached the vi as well. Here's what's going on. The device being read is an infrared thermometer that has an operational range of 550-3000C. When the temperature is below 550, the output is "HCC -U-". This output is terminated by a carriage return and line feed. When the tempartue is above 550, the output is "HUCxxxx" where the x's represent the digits. This string is read and the parsed. If the HCC is found, it then searches for " -U-". If found, this is displayed. Otherwise "ERROR" is displayed. In the event that HUC is found, the 4 characters after it are displayed and graphed.

 

To observe what is happening, I have a text box to show the contents of the read buffer and then another to display the parsed string. There is a final text box (output string 2) that shows the temperature should a numerical value register. I have also thrown in a led that lights up when the string parses properly and turns off in the event that "ERROR" is displayed.

 

Here is what is happening: when the temperature is below 550, I first see HCC -U- in the read buffer display, -U- in the parsed display and my led lights up. Then, presumably the buffer is emptied because I see the display empty, the parsed display now shows ERROR and the led goes off.

0 Kudos
Message 1 of 13
(6,522 Views)

What do you have so far?

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 2 of 13
(6,520 Views)

Attached the vi to my original post now; should've done that sooner!

0 Kudos
Message 3 of 13
(6,516 Views)

You really should post your code, but it sounds like you are constantly reading from the serial port and parsing regardless of the data or no data.

 

You should enable termination character in the serial port setup, and set the term char to equal the termination of your data string.

 

Then read until the term char is encountered or a time out.

 

If timeout = true then do not parse

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 4 of 13
(6,515 Views)

So the pyrometer has a polling rate of 600ms. To make up for this, I've set a 500ms delay between opening the serial port and reading it (much like in the serial read/write example. I've also set the timeout to 500ms. Should I do this differently?

 

Alternatively, how do I determine if a timeout has occured?

0 Kudos
Message 5 of 13
(6,509 Views)

Try something like this.

ser.PNG

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 6 of 13
(6,502 Views)

Set the VISA read bytes to a much higher amount than you expect to read

 

Set the timeout to somewhat longer than it should take to get a 'good' read

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 7 of 13
(6,492 Views)

The error code filtering along with the larger bits at port and timeout setings seems to have fixed this at first glance! I'm going to do some more testing and will post back soon.

0 Kudos
Message 8 of 13
(6,476 Views)

Good to hear you are on your way.

 

You might want to also think about eventually adding some other error detection for cases where you do not get good data or only partial data.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 9 of 13
(6,470 Views)

Read in some number of bytes into a shift register.

Look for the preamble in the shift register.

If there's a "length" of the message that comes back, that makes it even easier.  Read that number of bytes out of the port, and there's your message.

Or search for the next preamble and pluck out one message from the other.

 

Either way, shift registers are your friends here.

 

If returned string is null, don't put anything into the SR.

0 Kudos
Message 10 of 13
(6,455 Views)