locked
Serial Port Control of RTS line RRS feed

  • Question

  • I'm opening a serial port connection in C++ (DB9) and need complete control of the RTS line.
    I'm opening the serial port like this:

    // START OF CODE SNIPPET
    // Open the Serial Port for communication.
    mPortHandle = CreateFile
    (
     "COM1",
     (GENERIC_READ | GENERIC_WRITE),
     0,
     0,
     OPEN_EXISTING,
     FILE_FLAG_OVERLAPPED,
     0
    );

    and then later:

    // Disable the RTS line.
    if(!BuildCommDCB(portStatus, &mComDCB))
    {
     UINT error = GetLastError();
     sprintf(msg, "SetPortStatus: BuildCommDCB failed: GetLastError = %d", error);
     LogMessageFormatted(msg);
     return false;
    }
    mComDCB.fAbortOnError = (DWORD)1;
    mComDCB.fTXContinueOnXoff = (DWORD)1;
    mComDCB.fRtsControl = RTS_CONTROL_DISABLE;
    if(!(SetCommState(mPortHandle, &mComDCB)))
    {
     UINT error = GetLastError();
     sprintf(msg, "SetPortStatus: SetCommState failed: GetLastError = %d", error);
     LogMessageFormatted(msg);
     return false;
    }
    // END OF CODE SNIPPET

    However, when I execute the CreateFile command, the RTS line goes high, until I execute the SetCommState command, at which time it goes low.
    My problem is I cannot have it go high when I issue the CreateFile command. I need it to remain low until I instruct it to go high via the DCB later on in my code (I need comlete control of the RTS line).

    Is there some way of preventing CreateFile from forcing the RTS line high (or is there a better way of doing this)?

    Thanks

    -Mike

    Tuesday, August 3, 2010 2:25 PM

Answers

  • Hi David,

    I tried settings using different FILE_FLAG options. Unfortunately, none produces a different result. The RTS line is raised regardless darnet.

    Scott,

    Good suggestion. However, I can connect a volt meter between SGND (pin 5) and RTS (pint 7) to monitor the voltage between the pins (pins range from apx. -6 volts (disabled) to + 6 volts (enabled)). Prior to issuing the CreateFile command, the voltage is -6 volts (i.e. RTS is disabled). Upon issuing the CreateFile command, the voltage goes to +6 volts (i.e. RTS is enabled). This tells me that CreateFile is indeed raising RTS autonomously.

    Thanks, though David and Scott for your suggestions tough.

    I'll keep researching. :-)

    Some interesting news. I did find out, however, that the SerialPort class in C# allows me to have complete control over the RTS pin as needed.

    So, I may need to redevelop my app in C# instead in order to gain the functionality I need.

    Thanks again.

    -Mike

    • Marked as answer by Jesse Jiang Tuesday, August 10, 2010 2:10 AM
    Tuesday, August 3, 2010 7:42 PM

All replies

  • I have no idea whether this will help, but does specifying one or both of FILE_FLAG_NO_BUFFERING, FILE_FLAG_WRITE_THROUGH together with FILE_FLAG_OVERLAPPED make a difference?


    Answering policy: see profile.
    Tuesday, August 3, 2010 3:27 PM
  • Thanks David,

    I actually thought about doing something like this but havn't yet. I'll try it and get back.

    -Mike

    Tuesday, August 3, 2010 4:10 PM
  • Try experimenting with another program that sets RTS low before your program runs.  My guess is that CreateFile does not automatically set it high, but sets it to a saved previous state.  If that works you could create a service that sets RTS low during system boot.

    But your requirement is a bit unrealistic, considering the lack of control as the system powers up.

     

     

    Tuesday, August 3, 2010 4:38 PM
  • Hi David,

    I tried settings using different FILE_FLAG options. Unfortunately, none produces a different result. The RTS line is raised regardless darnet.

    Scott,

    Good suggestion. However, I can connect a volt meter between SGND (pin 5) and RTS (pint 7) to monitor the voltage between the pins (pins range from apx. -6 volts (disabled) to + 6 volts (enabled)). Prior to issuing the CreateFile command, the voltage is -6 volts (i.e. RTS is disabled). Upon issuing the CreateFile command, the voltage goes to +6 volts (i.e. RTS is enabled). This tells me that CreateFile is indeed raising RTS autonomously.

    Thanks, though David and Scott for your suggestions tough.

    I'll keep researching. :-)

    Some interesting news. I did find out, however, that the SerialPort class in C# allows me to have complete control over the RTS pin as needed.

    So, I may need to redevelop my app in C# instead in order to gain the functionality I need.

    Thanks again.

    -Mike

    • Marked as answer by Jesse Jiang Tuesday, August 10, 2010 2:10 AM
    Tuesday, August 3, 2010 7:42 PM
  • Some interesting news. I did find out, however, that the SerialPort class in C# allows me to have complete control over the RTS pin as needed.

    So, I may need to redevelop my app in C# instead in order to gain the functionality I need.


    But if that's the case, then there really ought to be a way to do it in native C++, since C# is using the same underlying interfaces.  You could try asking how it was done in the C# general forum (please report back here if you get an useful answer!).
    Answering policy: see profile.
    Thursday, August 5, 2010 5:00 PM