CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Cts

  1. #1
    Join Date
    Sep 2005
    Posts
    63

    Talking Cts

    Hi
    how can i set THE CTS bit in an serial communications in Framework 1.1?

  2. #2
    Join Date
    Oct 2005
    Posts
    23

    Re: Cts

    Hello there ; According to my knowledge ,
    Carrier Detect (CD; pin 1) ,
    Data Set Ready (DSR ; pin 6),
    Ring Indicator (RI ; pin 9) and
    Clear To Send (CTS ; pin 8)

    are all inputs to your Computer , From e.g. the Modem.

    In other words , CTS is a singnal From e.g. the Modem , and to you ,and is a signal that you may now send data.

    So you cannot set CTS. But you can set Request to Send (RTS)

    I downloaded the 101 samples from Microsoft DownLoadCenter ( Just search for 101 samples on Google) , and found a sample there on how to program the COM - port. I never managed to receive anything ,until I did the corrections suggested by Darwen. However ,the reading of the input lines mentioned above worked.

    Here are the very few code-lines you need to read CTS etc ;

    I have removed everything else but reading the signals above

    Lets say you put all these codelines in a file ,like e.g rs232.cs and include it as a part of your project ,
    PHP Code:
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;

    public class 
    Rs232
    {
       
    IntPtr hportIntPtr.Zero;

    #region "Enums";
       
    private enum Lines
       
    {
          
    SetRts 3,
          
    ClearRts 4,
          
    SetDtr 5,
          
    ClearDtr 6,
          
    ResetDev 7,  // Reset device if possible
          
    SetBreak 8,   //  set {the device break line.
          
    ClearBreak // Clear the device break line.;
       
    }
    #endregion

    #region "Win32API";
       
    [DllImport("kernel32"SetLastError=true)]
       static 
    extern unsafe bool CloseHandle(IntPtr hObject);

       [
    DllImport("kernel32"SetLastError=true)] 
       static 
    extern unsafe IntPtr CreateFile(string FileName,
                                                   
    uint Access,
                                                   
    uint Mode,
                                                   
    uint Attrib,
                                                   
    uint Disposition,
                                                   
    uint Flags,
                                                  
    int hTemplateFile);

       [
    DllImport("kernel32.dll")] 
       public static 
    extern bool GetCommModemStatus(IntPtr hFileref int Status) ;
        
       [
    DllImport("kernel32.dll")] 
      private static 
    extern bool EscapeCommFunction(int hFilelong ifunc);
    #endregion

    #region "Methods";
        
    public void Close()
        {
            
    CloseHandle(hport);
        }
        public 
    int GetBits()
        {
            
    int lpModemStatus 0;
            
    GetCommModemStatus(hportref lpModemStatus);
            return(
    lpModemStatus);
        }

        public 
    void SetRts()
        {
            
    EscapeCommFunction(hport, (long)Lines.SetRts);
        }
        public 
    void ClearRts()
        {
            
    EscapeCommFunction(hport, (long)Lines.ClearRts);
        }
        public 
    int Open()
        {
            const 
    uint GENERIC_READ 0x80000000;
            const 
    uint OPEN_EXISTING 3;

            
    hport CreateFile("COM1",GENERIC_READ,0,0,OPEN_EXISTING,0,0);
            if (
    hport != IntPtr.Zero) return 1; else return 0;
        }
    #endregion

    Now that you have rs232.cs as a part of your project ,you may use it in other parts of your code.

    Lets say your main form is Form1.cs ,and you want to read the status lines there...from now on all happens in Form1.cs;

    First put this in the beginning, were the rest of the stuff is declared;

    PHP Code:
        private Rs232 m_CommPort = new Rs232(); 
    Now the first thing you must do is to open the comport;
    PHP Code:
       int result m_CommPort.Open(); 
    Now that the comport is open ,you may read the status lines as many times as you want ;

    PHP Code:
       int Status m_CommPort.GetBits(); 
    Now the variable "status" will change as CD , CTS etc changes. ( this is bit oriented).

    If you want to know whether CTS is high or low ,you must test the corresponding bit;

    PHP Code:
    int ClearToSend true;      // These variables are just included for explanatory purposes
    int DataSetReady true;
    int RingIndicator true;
    int SetCarrierDetect true;


    if( (
    Status 0x10) >0)
       
    ClearToSend=true;
    else
       
    ClearToSend=false;

    if( (
    Status 0x20) >0)
       
    DataSetReady=true;
    else
       
    DataSetReady=false;

    if( (
    Status 0x40) >0)
       
    RingIndicator=true;
    else
       
    RingIndicator=false;

    if( (
    Status 0x80) >0)
       
    CarrierDetect=true;
    else
       
    CarrierDetect=false
    I havent tested setting out to RTS ( But I've tested the input lines as shown above) , but it wouldnt suprise me much if the following line now in forms1.cs will set RTS "active" ;

    PHP Code:
       m_CommPort.SetRts(); 
    As all readers probably know ,an active RS232 line is -12 Volts. A non-active line is +12 Volts.
    This means that if you want to test CTS input active or not ,it wont do to connect it to ground. It must be connected to -12 Volt.

    But most likely ,if you havent set RTS "non-active" , RTS will be -12 Volts. Therefore ,you can connect CTS to RTS ,in order to test CTS.
    Last edited by kwik; December 2nd, 2005 at 01:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured