C#SerialPort DSR / DTR和CTS / RTS握手

时间:2014-06-26 02:47:34

标签: .net serial-port handshake

我正在尝试使用C#/。NET SerialPort类与设备进行通信。

此处介绍了与设备交互的文档。


RS232 documentation


我正在使用带有TX / RX的NULL调制解调器电缆,并且交换了所有握手引脚(已验证)。

我希望以下C#代码可以正常工作,但我没有从我试图与之交互的相机中获得任何回复(从低到高)。我确信问题在于代码。这款相机适用于其他电脑"。为什么我的代码中永远不会得到DsrHolding(零调制解调器电缆,因此相机中的DTR高)?

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.");
    }
}

2 个答案:

答案 0 :(得分:2)

您是否尝试使用直通电缆?相机可能已经处理了需要穿过的引脚。

答案 1 :(得分:0)

您的代码中缺少serialPort.Open();