[英]Serial Port not receiving any data
I'm trying to write a simple c# console application to read/write from a serial port that communicates to an Arduino that I have hooked up.我正在尝试编写一个简单的 c# 控制台应用程序来从串行端口读取/写入,该串行端口与我连接的 Arduino 通信。 The problem that I'm running into is that I can write to the Arduino no problem, but I am unable to receive any data back.
我遇到的问题是我可以写 Arduino 没问题,但我无法接收任何数据。 My serial port's
SerialDataReceivedEventHandler
isn't ever being fired either, but I'm guessing those two issues are related.我的串行端口的
SerialDataReceivedEventHandler
也从未被触发,但我猜这两个问题是相关的。
I know it isn't my Arduino that is causing the problem because when using the Arduino IDE I am able to receive data without any problems.我知道导致问题的不是我的 Arduino,因为在使用 Arduino IDE 时,我能够毫无问题地接收数据。 Here is what I've got code wise for now:
以下是我目前掌握的代码:
SerialPort sPort = new SerialPort();
sPort.PortName = SerialPort.GetPortNames()[0];
sPort.BaudRate = 9600;
sPort.Parity = Parity.None;
sPort.DataBits = 8;
sPort.StopBits = StopBits.One;
sPort.RtsEnable = false;
sPort.Handshake = Handshake.None;
sPort.DataReceived += new SerialDataReceivedEventHandler(sPort_dataReceived);
sPort.ErrorReceived += new SerialErrorReceivedEventHandler(sPort_ErrorReceived);
sPort.Open();
Console.Read();
sPort.Write("tt_calib");
while (true)
{
if (sPort.ReadExisting() != string.Empty)
Console.WriteLine(sPort.ReadExisting());
}
I am aware that I don't close the port in this code, that is not the issue as I am able to rerun and open it every time.我知道我没有关闭此代码中的端口,这不是问题,因为我每次都可以重新运行并打开它。 This code is also not in its final form, I'm attempting to get the read event working so that I can react to various messages differently.
这段代码也不是最终形式,我试图让读取事件工作,以便我可以对各种消息做出不同的反应。 I've read what seems like every question but no solution I've found seems to do the trick.
我已经阅读了似乎每个问题的内容,但我发现没有解决方案似乎可以解决问题。
This is a C# .NET 4.5 console application running on Windows 8.1这是在 Windows 8.1 上运行的 C# .NET 4.5 控制台应用程序
It ended up being a stupid simple issue. 最终这是一个愚蠢的简单问题。 Instead of
sPort.RtsEnable = false;
代替
sPort.RtsEnable = false;
it should be true
. 应该是
true
。 All events now trigger and I am able to read. 现在所有事件都会触发,并且我能够阅读。
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
I had the exact same issue and finally realized that my hardware flow control setting is bios configurable and my setting was DISABLED ! 我遇到了完全相同的问题,最后意识到我的硬件流控制设置是可配置的bios,而我的设置已禁用 ! doh
卫生署
With C# in VS2019 available cross-platform, I ran into the same issue on my VM (Parallels+Windows10) and my host machine (MacOS).随着 VS2019 中的 C# 跨平台可用,我在我的 VM(Parallels+Windows10)和我的主机(MacOS)上遇到了同样的问题。 Setting DTR to true did the trick
将 DTR 设置为 true 就成功了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.