【问题标题】:C# SerialPort DSR/DTR and CTS/RTS handshakingC# SerialPort DSR/DTR 和 CTS/RTS 握手
【发布时间】:2014-08-16 18:45:26
【问题描述】:

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

此处描述了与设备交互的文档。



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

我希望以下 C# 代码能够正常工作,但我没有从我尝试与之交互的相机得到任何响应(从低到高)。我确信问题出在代码上。该相机可与其他“PC”配合使用。为什么我永远不会在我的代码中让DsrHolding(零调制解调器电缆,因此从相机的 DTR 高)变为 true?

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

【问题讨论】:

  • 您为什么使用零调制解调器电缆?相机文档是否说您应该这样做?
  • 这样相机可以使用TX发送数据,我可以使用RX接收。
  • 相机文档是否说您应该使用零调制解调器电缆?
  • 不具体,没有。
  • 您说,“此相机可与其他“PC”配合使用。您使用的电缆是?

标签: .net serial-port handshake


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-12-26
      • 2010-10-23
      • 2019-01-25
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多