我正在使用 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/