c# - SerialPort.ReadLine() 不从 Arduino 发送的 USB COM 端口读取数据

标签 c# arduino serial-port

我正在使用 Arduino 将文本数据发送到 COM15(通过微型 USB)。在我的桌面上,我试图从我的 C# 应用程序中读取数据。然而,当我运行它时,控制台什么也没显示,程序卡在“string s = myPort.ReadLine()”行。

以下是我的C#程序:

static void Main(string[] args)
{
    var myPort = new SerialPort("COM15", 115200);
    myPort.Open();
    while (true)
    {
        var s = myPort.ReadLine(); // execution stucks here waiting forever!
        Console.WriteLine(s);
    }
}

以下是Arduino代码(发送数据到COM15):

int counter = 1;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(" Data Loop = " + String(counter));
counter++;
delay(500);
}

arduino 串口监视器确实显示了 COM15 接收到的数据。我还尝试了其他读取 COM 端口的软件,并验证了端口上的数据可用。

最佳答案

通过在 myPort.Open() 命令之前添加以下行,我设法解决了我的问题并成功地从 COM 读取:

myPort.DtrEnable = true;

你可能会问什么是Dtr标志。 Dtr 代表“数据终端就绪”,根据 Wikipedia :

Data Terminal Ready (DTR) is a control signal in RS-232 serial communications, transmitted from data terminal equipment (DTE), such as a computer, to data communications equipment (DCE), for example a modem, to indicate that the terminal is ready for communications and the modem may initiate a communications channel.

关于c# - SerialPort.ReadLine() 不从 Arduino 发送的 USB COM 端口读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39401946/

相关文章:

Eclipse Arduino make : *** error 1

multithreading - Arduino - 在后台使用串行打印

java - 如何向外部设备发送数据

python - 从 Arduino 串行发送信号到 Python 程序

c# - 我如何知道我使用的是什么 Windows 主题?

c# - 命令执行后调用事件

C++:在包含的头文件中使用#define 常量 (Arduino)

javascript - 带串口的数据流和 Promises Node.js

c# - 应用程序设置不会在 Xamarin.Forms 中删除

c# - 通过其中的元素查询特定数据的最佳方式是什么?