LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

how to keep alive RS232 connection

Solved!
Go to solution

Hello everyone. I have a problem with RS232 communication. To activate the connection I have to send the string "RS232", then to keep it alive I have to send every second CR, but in the meantime, I must be able to send other character strings without delay on the same port (COM7). Can anyone help me?

Thanks 

0 Kudos
Message 1 of 5
(7,511 Views)
Solution
Accepted by topic author ViperNaples

RS232 standard does not require by itself to be kept alive someway, so I suppose is your remote device that requires it.

In every case, you could create a 1-sec timer and have its callback send CR periodically over the serial port.

 

To avoid conflicts between this task and the rest of the application you could use a lock: basically, after creating the lock, each function that wants to access the serial port must call CmtGetLock (), operate on the port and call CmtReleaseLock () afterwards. If the lock is owned by another function, other functions trying to get access are frozen waiting for the lock, so you must be careful in designing com-related function so that they are fast. If you do not want to have some function be blocked while waiting for the lock you can use CmtTryToGetLock instead of CmtGetLock. A few informations on locks can be found in this tutorial as well as in this chapter of the Programmer reference.



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?
Message 2 of 5
(7,506 Views)

Hi,

 

from my experiences in most cases the alive signal has to be send only if there is no other kind of communication. So it might be that sending a valid character string is also accepted as alive signal. Did you try it?

This would make the handling a little bit easier. You could install a timer that sends at least every second a CR. If you need to send a valid string, just disable the timer, send the string, reset the timer and enable it.

 

Another idea, if you don't also receive data from the connection, why don't you just open comport (COM7) at the start of your programm and then send RS232 to start communication and then the character string? Is it necessary to keep the data communication always alive? 

 

Greetings from Bremerhaven

 

Norbert

 

0 Kudos
Message 3 of 5
(7,465 Views)

"Another idea, if you don't also receive data from the connection, why don't you just open comport (COM7) at the start of your programm and then send RS232 to start communication and then the character string? Is it necessary to keep the data communication always alive?"

 

With "Connect" button i open COM7 and then send string "RS232". It's necessary to keep the connection alive, otherwise all the parameters that I set during the communication will be lost. 

Maybe there are only two solution:

  1. timer
  2. multithreading

thanks, best regards

0 Kudos
Message 4 of 5
(7,338 Views)

The RS232 library is thread safe - it will not intermingle the data from two ComWrt calls from separate threads, for example.

 

So, you could use an async timer, which is a high quality timer that runs on a separate thread (I think CVI uses the Windows media timer for these) and it's only task in life is to issue a CR to the port.  Even if it's delayed (locked out) in the rs232 library, the interrupt won't get lost or thrown away and you'll get the CR issued to the device.

 

The only issue here is if you are using more than one rs-232 library call to write data to your device - if you are, then you cannot rely solely on the thread safety of the rs232 library, but use a lock as Roberto suggested if you have multiple threads (i.e. use async timer).

 

If you use a regular timer (on the GUI) it runs on the main thread, so it won't intermingle the CR with your regular traffic unless you process events somewhere during your writes to the device.  With an async timer you're more likely to know that you're getting a callback and the opportunity to write a CR at exactly one second intervals.

 

Many systems incorporate a "heartbeat" that does the same thing as your CR is doing, but usually these aren't too agressive with the timing, and often are more than a simple CR.

 

I would think your device would accept regular incoming rs232 traffic as a "keep alive" signal, as one of the posters has suggested - if it doesn't, you might argue that you have a weak design on your device.

 

 

 

 

0 Kudos
Message 5 of 5
(7,331 Views)