From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Handle Event for Characters At Serial Port in an eventstructure

I want to write an event driven application for serial communcation without polling for characters at serial port, instead I want to have an event case in the event structure for this event. Is this possible and how can i do it?
0 Kudos
Message 1 of 7
(3,728 Views)
Hey Stani,

I'm not sure what programming language you are using, but VISA support events. Typically, all you have to do is setup your VISA resource, and then enable the VISA event you want to use. In your case the Characters at serial port event. Then wait on the event. After, the event occurs you just need to perform the VISA read. Attached is a LabVIEW example that illustrates this.

-JoshuaP
0 Kudos
Message 2 of 7
(3,728 Views)
Thank you JoshuaP!
I am using LabView, thats allright.
The problem with the wait_on_event function is that nothing can work in parallel with that function (it looks like that function is polling for that event).
What would be nice is if i could register such an event for an event structure, so that i can handle it like e.g. an "Value Change" event for a control.
Currently i am using the timeout event case in the event structure to look for bytes at port (but this is also like polling).Do you think there is a better way to do this? Or do i Have do use a second Thread?
0 Kudos
Message 3 of 7
(3,728 Views)
You should be able to execute the example that I posted with an event structure in parallel. I modified the last example so that on thread is waiting for the serial event and then another thread is executing an event structure that just increments a counter. You should be able to increment the counter any time you want and the serial event will complete as soon as a message is read independent of one another.

Unfortunately, I don't know how to (and I am not sure you can) setup an event structure to work with VISA or DAQ events. If you put your VISA Wait on Event inside of your event structure it will hold and prohibit any other events from executing until it is done. However, if any event fires it will block the other
events until it completes. If you uncheck the box under the your event properties you can prevent the front panel from locking. This will at least queue up the events.

I hope this helps out. Attached are some examples.

-Josh
Download All
0 Kudos
Message 4 of 7
(3,728 Views)
Thank you, your information was very usefull.
Because of very different response times of my controlled device (ms to min) I've tried to avoid functions with timeout and have choosen a little different way. If you are interested show the attached vi

Stani
0 Kudos
Message 5 of 7
(3,728 Views)
Hey Stani,

The one problem that I can see with your implementation is that the CPU is spending a lot of time executing the top loop. When I ran your application my CPU usage was around 60-70%. Here is a way to use the VISA events and not have to deal with the timeout issue.

-Josh
0 Kudos
Message 6 of 7
(3,728 Views)
Yes,thank you, this was a proplem. If a put a wait for next ms multiple (whith consideration of the baudrate and the number of bytes selected for the Threshold)in the top loop i can solve the problem.
0 Kudos
Message 7 of 7
(3,728 Views)