LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreading rs232 in CVI

Is there example code for multithreading communication with the rs232 port?
0 Kudos
Message 1 of 17
(6,116 Views)

Hi aml1138,

If you have experience with rs232 communication and then you can study multi-threading from the documentation, available sample code and other web resources.

Combining the two libraries in a single application is not very difficult. It is even trivial for basic applications.

For advanced requirements thread-safe arrays and stuff like that may come into picture.
In that case, CVI has already got many tools to cover those aspects.

 

If you can explain your application with a little bit more detail people can help you better.

 

S. Eren BALCI
IMESTEK
Message 2 of 17
(6,084 Views)
In any case, if you happen to need access to the serial port from different threads you must be aware that this physical resource is unique in the system, so you must provide some way of interlocking access to it. You may find useful the locking functions provided by CVI, located in the Utility library, class Multithreading >> Thread lock.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 17
(6,077 Views)

The CVI RS-232 Library is thread-safe, meaning that you can call those functions to operate on the same port from multiple threads without doing any explicit locking of your own. The library internally manages a lock for each port in use.

 

Mert A.

National Instruments

0 Kudos
Message 4 of 17
(6,059 Views)

Mert, what you say is true with reference to the pure rs232 communication, but from an application point of view a good operation cannot be guaranteed from the library only. My usual scenario is to have several devices connected to a single rs232 port with a periodic polling task that asks each one for its working state. At a given time the operator wants to send an asyncronous message to one of the devices connected. Without an explicit interlock that device can be handling a request from the polling thread (reading the request, handling and answering it), thus being unable to serve the operator request. On the other hand, handling the operator's request may take some time (usually more than serving the state poll) that makes the device unable to serve the periodic polling requests.

In this scenario I find an explicit handling of a lock is the only reasonable way of managing the system.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 17
(6,051 Views)

Yes, you make a good point. At a higher level, in order to ensure that the communications do not get interleaved or otherwise misdirected between threads, it may be necessary to create and use locks to avoid simultaneously querying a device from multiple threads. I was not considering this issue in my response.

 

Mert A.

National Instruments

Message 6 of 17
(6,052 Views)

Here’s what I need to do.  I have two rs232 devices streaming data. I development code in LabVIEW were my application collects data in asynchronous parallel loops.  These are the timed loops that allow you to assign the tasks to a specific core.  A separate (MAIN) loop processes the info and user input.

 

I was looking for a CVI example that shows how to do the same thing;  spawn threads for each rs232 port and a thread to handle the user input.  I looked at the examples that come with CVI and I still don’t quite get it.  I was hoping someone had a simple example that I can plagiarize from.

0 Kudos
Message 7 of 17
(6,051 Views)

AFAIK there is no source code similar to your application, so you'll need to pick some suggestions from various places and put them together...

CVI documentation has several documents and examples describing multithreaded applications, from which you can take the mechanism of spawning a thread. Inside the thread you can open the serial port and then handle the communication with your device.

You do not need a separate thread to handle the user interface since the main thread (i.e. the one were the main () function is) can be in charge of it.

If you need to pass values from the communications threads to the main thread you can use the Thread Safe Queues, a mechanism that preserves the separation between threads and permits passing data between them.

To control the application you could use PostDeferredCallToThread  to send commands from the main thread to the communications threads (at least issuing a close command that permits to terminate communications and close the ports in a safe manner): this mechanism is considerably slower than a thread safe queueso it can be used for limited data interchange.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 17
(6,038 Views)

old thread, same problem.

 

i have an application where i have to comunicate with RS232 device, streaming or polling mode are possible, and sometimes i have to write a value to the device.

 

i have done in this way but is not fully working:

 

  1. open com port
  2. create thread that start polling (write request, read without callback)
  3. open panel where user can set the value
  4. if user set the value i write to the device directly

sometimes, when i change the value fast, the device won't update. i think that no message is sent.

i've tried to put a lock but my application sometimes freezes.

any suggestion?

 

Davide Vittorio G. - TLGB S.R.L.
Italian SW Developer
0 Kudos
Message 9 of 17
(5,098 Views)

The freeze could be due to some incorrect lock handling: can you post a code skeleton that shows how you are handling the lock?

 

When you operate quickly on the UI, you may be sending commands too fast to remote device, i.e. you may be sending some command when the device is not able to read and interpret it. This could be true especially when the device is in streaming mode: polling mode generaly permits you to place commands in the correct moment. Normally device documentation lists all constraints that are present in controlling the device.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 10 of 17
(5,092 Views)