首页 > 解决方案 > 如何使用 C# 可靠地读取串行端口的 CTS 和 DSR 状态

问题描述

回到过去(15 年前),我在 Delphi 上做了一个小程序来监控、记录和显示来自 PC 和 I2C 传感器之间的 I2C 通信的信号的时间图。现在我需要同样的东西,我有很大的问题来适应 C#。一般来说,我将 CTS 线从串行端口连接到 I2C 的 SCL 线,并将 DSR 连接到 SDA。然后我启动一个线程来读取 CTS 和 DSR 的状态(带有无限循环)并将它们保存到一个文件中。这是代码:

        void InitCommPort()
        {
            myCOMport = new SerialPort("COM2", 9600);
            myCOMport.Open();
        }

        while (!bExit)
        {
            CTS_Signal = myCOMport.CtsHolding;
            DSR_Signal = myCOMport.DsrHolding;
        }

The problem is that i can not get the signals[CTS and DSR] fast enough to reconstruct the communication protocol. Actually mostly i can get only one of the signals, and it is just part of the all transfer. I tried also "SerialPinChangedEventHandler", but I'm receiving only '0' as a result. Obviously I'm missing something or just the SerialPort-Class is made in this way. Is it possible to be done with C#? I do not want to install Windows XP for something simple like that. Any help will be appreciated. Thank You in advance

标签: c#serial-porti2c

解决方案


推荐阅读