CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Using *hardware*RS232 Handshaking - should be an easy topic!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

Using *hardware*RS232 Handshaking - should be an easy topic!
PostPosted: Mon Oct 03, 2005 10:37 am     Reply with quote

Hello people. I need to use RTS/CTS handshaking between PIC and another device which requires it. I have connected the h/s lines via a level shifter to IO pins.

The question is, if I am using printf statements to send the data out, are they easily modified to check for CTS to be active before transmitting? Will I have to write an alternative 'putc()' routine and point printf to it or is there an easier way?

Your help will be much appreciated.
____________________________________

The 'other device' by the way is an 'AR' RF power amplifier (500W). These are usually used in the broadcast industry, although mine is part of the RF system on a synchrotron. Anyone out there familiar with these amplifiers?!

Regards,
Neil.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Oct 03, 2005 1:13 pm     Reply with quote

Use of Hardware handshaking require to define wich is DTE and wich is DCE.
I assume that your project is the DTE and the equipment to be controlled is the DCE.

Quote:

The question is, if I am using printf statements to send the data out, are they easily modified to check for CTS to be active before transmitting?


Yes, you must do it.
Programs may use the RTS/CTS connection to check that a device is ready to
receive data.
Code:
       

    if(something_to_Txmit_to_DCE)
      {
        output_high(RTS);  //   RTS = TRUE;
        handshake_error = 200; //

        do
          {
            handshake_error--;
            delay_ms(10);   // check the DCE latency delay spec for this delay
          } while(input(CTS) && handshake_error ); 

        // DCE must respond pulling down the CTS control line.
 
        if(!input(CTS))   // If DCE is ready...
          {
            printf(" 12345...");
            something_to_Txmit = FALSE;
          }

        if(!handshake_error)
          {
            handshaking_error_handler();
          }
       }


If there is No CTS connection, the program should never send data, but wait a
long time or timeout with an error.
The RTS line may need to be looped back to the CTS input is you want to check
without the DCE.

Be aware also that when connect a DTE to DCE you must wire a cable in such a way
that the signal names must "match", usually named a "straight" cable.

Hope this help you,

Humberto
Ttelmah
Guest







PostPosted: Mon Oct 03, 2005 2:25 pm     Reply with quote

There are a couple of other 'comments'. A lot depends on how the device implements it's handshake. Generally, the 'device ready to receive data' line (CTS), should go inactive, with a 'margin'. So it is common on a buffered device to deactivate the line, when the internal buffer becomes 3/4 full, and reactivate when the buffer drops to perhaps 1/4 full. This is because sending devices, will themselves have buffers, and it is rare to be able to stop the data that is already in this buffer. If this is the case, then depending on the size of the buffer, and the sizes of your messages, it may be perfectly acceptable to just check the status before sending a message. If not, provided there is at least a two character buffer, then the answer is to 'encapsulate' the putc function. So (in 'schematic' form only), something like:
Code:

void handshake_putc(int8 chr) {
   while (!CTS) ;
   putc(chr);
}

//and use
printf(handshake_putc,"what you want to send");


Now this is nasty from the point of view of your routine, in that you sit waiting, if the CTS does not become active.
The alternative is to use a buffered transmit, with the transmit interrupt, and in the event that the CTS line goes inactive, you disable this interrupt, and generate a 'retest' with a timer interrupt instead.
A lot will depend on the exact behaviour of the transmitting device, with the 'hold' approach possibly being acceptable, if this is only likely to be for a fraction of a second while the device wakes up.

Best Wishes
neil



Joined: 08 Sep 2003
Posts: 128

View user's profile Send private message

Crossover V straight
PostPosted: Tue Oct 04, 2005 4:46 am     Reply with quote

Hi Humberto & Ttelmah. Thanks for your replies. with reference to this:
Quote:
Be aware also that when connect a DTE to DCE you must wire a cable in such a way
that the signal names must "match", usually named a "straight" cable.

I am actually using a home made 'fully loaded' null-modem cable. Ie: TX & RX crossed, RTS & CTS crossed, plus CD&DSR connected to DTR at other end and vice-versa. I have also included Ring Indicate, although this is not used.
The DTR (at the DTE end) is used by the AR amplifier to assert CD and DSR in order to detect an active connection. I have wired DTR to the +10V flying capacitor on my MAX232. I know this is a bodge, but it works OK.

Surely,if a straight cable was used, all the signals would collide? (I always have trouble with these conventions!)

On my PIC board, I had the forethought to include a crossover jumper, so TX/RX can be crossed if needed.

I think I am just getting muddled with the direction of the signals and which is DTE and DCE!!

I will try writing a routine to check CTS and transmit when allowed. Then, port it to the TX interrupt and include the timeout check. I am already using timer interrupts to drive my main loop (flag polling) so a timeout flag would be easy to put in.

Thanks for your continuing help.
Neil.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Tue Oct 04, 2005 7:42 am     Reply with quote

Quote:

Surely,if a straight cable was used, all the signals would collide? (I always have trouble with these conventions!)


Yes, you are right, sorry. The "straight" cable is used to connect DTE <> DTE.

The "null modem" cable is used to connect DTE <> DCE.
This is the wiring you�ll need for hardware handshaking.

Code:

        DTE (9 pin)                       DCE (9 pin)
        FEMALE                            MALE

        RD    2 ---------\ /------------- 2
        TD    3 <--------/ \------------> 3
        RTS   7 ---------\ /------------- 7
        CTS   8 <--------/ \------------> 8
        DSR   6 <---,              ,----> 6
        DCD   1 <---+----\ /-------+----> 1
        DTR   4 ---------/ \------------- 4
        SG    5 ------------------------- 5


Pls tell us what type of connector has your DCE. (Male/Female)

Quote:

I think I am just getting muddled with the direction of the signals and which is DTE and DCE!!



To know what type is, (DTE or DCE) try this: (I assume you have a 9 PIN connector)

1 ) In the DB9 connector measure the DC voltages between pins 2 & Gnd and between
pins 3 & Gnd. (Be sure the meter black lead is connected to Signal Ground and
the red lead to the pin you are measuring.)

2) If the voltage on pin 2 (TD) is more negative than -3 Volts, then it is a DTE,
otherwise it should be near zero volts.

3) If the voltage on pin 3 (RD) is more negative than -3 Volts, then it is a DCE.

4) If both pins 2 & 3 have a voltage of at least 3 volts, then either you are measuring
incorrectly, or your device is not a standard EIA-232 device. You are in troubles.

5) In general, a DTE provides a voltage on TD, RTS, & DTR, whereas a DCE provides
voltage on RD, CTS, DSR, & CD.


Humberto
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group