Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

read serial without polling

Is there a way to read the serial port without polling? I am aware of the bytes at port feature. But that also needs to be polled, probably at a slower rate. I am wondering if there is something like the event structure that just waits until data arrives to run.
0 Kudos
Message 1 of 19
(6,023 Views)
Put another way, is there a mechanism within labview that can be triggered when the first data reaches the serial receive buffer without polling it?
0 Kudos
Message 2 of 19
(6,013 Views)
If you can see the data arrival by detecting specific bytes (length) are arraived, you can see how many bytes are in host PC's read buffer, by using VI_ATTR_ASRL_AVAIL_NUM attribute.
 
This attribute can be checked by viGetAttribute() function if your app uses VISA library for serial port access, like this: 
 
vs = viGetAttribute( vi, VI_ATTR_ASRL_AVAIL_NUM, &val);
 
The above coding is C langauge style but LabVIEW version must be also available. Your app must POLL this function call, but it returns immediately even if your device is very slow or even if no data arraived.
 
 
0 Kudos
Message 3 of 19
(6,006 Views)
I am not an expert in C, but this method seems equivalent to polling the 'bytes at port' property of the VISA device which is what I am trying to avoid. I am thinking something like an interrupt function that will be excuted only when the first data gets to the port.
0 Kudos
Message 4 of 19
(6,001 Views)
Hello isaoz,

You can use a VISA Event to that will trigger when a byte is received at the serial port.  You will need to use "serial character" as your event type.  You can look at this document and the attached example for more information on how to do this.

Please let me know if you have any questions.  Have a great weekend. 
O. Proulx
National Instruments
www.ni.com/support
0 Kudos
Message 5 of 19
(5,983 Views)

Hi O. Proulx,

Looks like exactly what I was looking for. I will test it and get back to you soon. Thank you very much.

0 Kudos
Message 6 of 19
(5,977 Views)

Hi O_proulx,

This is exactly the mechanism I was looking for but the VISA 'wait on event' vi itself seems to be polling the port at a slow rate as I was unable to get samples faster 9 per second. Is the vi polling every 100ms? I have tried several codes ending up with pretty much the same result. The serial device is sending a 5 byte data every 5ms at 19200baud. Attached is my vi.

0 Kudos
Message 7 of 19
(5,869 Views)
What happens if you get rid of all that extra code and just use a VISA Read set to return 5 bytes? According to what you have programmed, you are expecting the data to be terminated with a LF character and so you don't really need the VISA event or the Bytes at Serial Port. If it terminated with a LF, then you can put just about any number you want for the number of bytes to read. The read will automatically terminate as soon as the LF is detected.

Message Edited by Dennis Knutson on 04-25-2008 07:46 AM
0 Kudos
Message 8 of 19
(5,855 Views)

Hi Dennis,

I am currently testing the vi with hardware sending 5 bytes of data terminated with LF every 5ms for simplicity. But the real hardware will send varying size of bytes at varying rates. Of course I could poll it at the fastest rate. I was hoping for an efficient vi where the processor would be used only when data arrives. The 'visa wait event' vi does that but it does not seem fast enough for data arriving at frequencies higher than 10 Hz. PRETTY SLOW! Can someone who is an expert on 'visa event' VIs confirm this? Am I doing something wrong here. The VI I attached earlier does not really do much else than reading the data as fast as possible and displaying it.

0 Kudos
Message 9 of 19
(5,825 Views)
Take out the VISA Enable Event and VISA Disable Event. Also don't understand why you have a while loop that executes once around the event code.
0 Kudos
Message 10 of 19
(5,822 Views)