LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

delay between toggle of RTS and start of serial data

Hello,
Below is a snippet of my code, as is it won't run. I am trying to support
an instrument that may be
configured for RS-232 or RS-485 (using a dongle for the latter).
The dongle runs half-duplex and uses the serial port signal RTS to switch
from transmit to receive.

On my computer, the whole serial data takes <4 ms at 9600 baud.
My control of the RTS signal seems to be poor when starting to send the
data, but good when finishing up.

The RTS signal is repeatably released just after the last serial character
exits the serial port.
However, the time between asserting the RTS signal and the 1st character
being sent is inconsistant.
It varies between 0 ms and 25 ms approx, perhaps more.

I have yet to actually try interfacing to the actual instrument, perhaps
this may not be an actual problem.

I welcome suggestions to correct this error, or placate my fears because of
similar experience.

Thanks,

--------8<--------8<--------8<--------8<--------8<--------8<--------8<------
--8<--------


RS232Error = OpenComConfig (comport, "", baudrate,
parity, databits, stopbits, inputq, outputq);

if (RS232Error == 0)


port_open = 1;
SetComTime (comport, 0);

FlushInQ (comport);

FlushOutQ (comport);

Delay(1);

ComSetEscape (comport, CLRRTS); // just to see the line toggle once
Delay(0.1);
ComSetEscape (comport, SETRTS);

}


strcpy(send_data,"S80");

for (q=00;q<30;q++)
{
printf("\n%d...",q);
FlushOutQ (comport);
m=0;

Delay(2);
SendAscii();
printf("%d bs:",bytes_sent);
for (n = 0; n {
printf("%d ",array1[n]);
}

}


void SendAscii (void)
{

char stat;

int stringsize = StringLength (send_data);
int bytes_sent;

ComSetEscape (comport, CLRRTS); // set the line negative voltage

// used a scope and saw a variable delay appear at this location,
// delay varies between 0 ms and 25 ms approx


bytes_sent = ComWrt (comport, send_data, stringsize);

while (GetOutQLen (comport)); // wait for internal buffer to empty

do
{
stat = inp(portbase + LSR);
stat = stat & 0x40;
}
while (stat == 0); // wait for hardware buffer to empty

ComSetEscape (comport, SETRTS); // set the line positive voltage

}
0 Kudos
Message 1 of 3
(5,523 Views)
When I need to handle serial communications, I prefer using the native SDK functions that deal the matter.

In regards to the use of the RTS line to control the RS-485 bus data direction, the Win32 SDK provides an attribute to the device-control block for the communication port (DCB) that suits your needs.
You just have to set the "fRtsControl" field to "RTS_CONTROL_TOGGLE" and let the low-level serial driver do the rest during data transmission.

In short: in order to use SDK functions in CVI you have to setup it using a custom installation.
In all modules that call SDK services include the header prior to the CVI ones.

To setup serial communications:
1) Open the COM port using "CreateFile" (the returned file handle is the handle to the communications resource). The file name is the name of the COM port ("COM1", "COM2", etc.). However I suggest using the "\\\\.\\COM1" names that work also on COM ports above COM9.
2) Setup the I/O data buffer size with "SetupComm"
3) Define communication attributes (baud rate, parity, RTS control mode, etc.) by filling a DCB structure and calling "SetCommState"
4) You may also want to define comminication timeouts with "SetCommTimeouts"

For data transmismission use "ReadFile", for data reception use "WriteFile".

At the end of the program do not forget to call "CloseHandle" to free the COM port.

For details please refer to the SDK documentation (there are a lot of other features you can exploit with SDK, such as overlapped I/O.

***********************

PLATFORM NOTES
I used the RTS_CONTROL_TOGGLE attribute for an RS-232/RS-485 converter.
According to my experience under Windows NT everyhing works fine
Under Windows 95 this solution sometimes does not work (maybe there is a problem in the serial device driver or there are undocumented platform-specific issues).

I have never tested Win 98/Me/2000 about the RTS toggle feature.

********************************

SUGGESTED SOLUTION
To avoid any problems use an RS-232 to RS-485 converter that automatically detects the data direction. In this way your software won't need to handle the RTS line.

Bye
Giovanni
0 Kudos
Message 2 of 3
(5,523 Views)
A couple of quick remarks here:

The RTS_CONTROL_TOGGLE switch only works with Windows NT and 2000 - it's not available in 95/98/Me. (It also may not work if you have a multiport serial card - I know some of the older Rocketports that I've used didn't support it.)

The truth of the matter is that as long as your computer is the "master" station on the RS-485 link, the delay between raising RTS and the beginning of data shouldn't cause any problems. If the computer isn't the master station, you will be running the risk of having the master station time out when it polls the computer.

Hope this helps.
0 Kudos
Message 3 of 3
(5,523 Views)