C# SerialPort DSR/DTR and CTS/RTS handshaking

Paul Knopf picture Paul Knopf · Jun 26, 2014 · Viewed 14.6k times · Source

I am trying to communicate with a device using C#/.NET's SerialPort class.

The documentation for interacting with the device is described here.


RS232 documentation


I am using a NULL modem cable with the TX/RX and all the handshake pins swapped (verified).

I would expect the following C# code to work, but I am not getting any response back (low to high) from the camera I am trying to interact with. I am sure the problem is with the code though. This camera works with other "PCs". Why would I never get DsrHolding (null modem cable, so DTR high from camera) to become true within my code?

static void Main(string[] args)
{
    var serialPort = new SerialPort("COM5");

    // start the DSR/DTR handshake
    // we are using a null modem cable, so DTR/DSR is switched
    serialPort.DtrEnable = true;
    while (!serialPort.DsrHolding)
    {
        // probally should timeout instead of infinite wait
        Thread.Sleep(10);
        Console.WriteLine("Waiting for the DTR line to go high.");
    }


    // start the RTS/CTS handshake
    // we are using a null modem cable, so RTS/CTS is switched
    serialPort.RtsEnable = true;
    while (!serialPort.CtsHolding)
    {
        // probally should timeout instead of infinite wait
        Thread.Sleep(10);
        Console.WriteLine("Waiting for the RTS line to go high.");
    }

    // read/write
    //serialPort.Write("Some command");
    //var response = serialPort.ReadChar();
    //while (response != stopBit)
    //    response = serialPort.ReadChar();

    // close the connection because we have written/read our data

    // start the DSR/DTR handshake
    // we are using a null modem cable, so DTR/DSR is switched
    serialPort.DtrEnable = false;
    while (serialPort.DsrHolding)
    {
        // probally should timeout instead of infinite wait
        Thread.Sleep(10);
        Console.WriteLine("Waiting for the DTR line to go low.");
    }


    // start the RTS/CTS handshake
    // we are using a null modem cable, so RTS/CTS is switched
    serialPort.RtsEnable = false;
    while (serialPort.CtsHolding)
    {
        // probally should timeout instead of infinite wait
        Thread.Sleep(10);
        Console.WriteLine("Waiting for the RTS line to go low.");
    }
}

Answer

egfconnor picture egfconnor · Jun 26, 2014

Have you tried using a straight through cable? The camera may already take care of what pins need to be crossed.