简体   繁体   English

C ++中的串行通信(Visual Studio 2010,.NET 4.5)

[英]Serial Communication in C++ (Visual Studio 2010, .NET 4.5)

I've got a clarification question regarding the receiving of data from across a serial port from an Arduino. 我有一个关于从Arduino的串行端口接收数据的澄清问题。 Specifically, I'm using a Sparkfun Pro Micro 5v/16MHz and trying to write a simple GUI in C++ to send messages over the serial port and then receive one of a few responses that are specified in the code running on the Arduino board. 具体来说,我使用的是Sparkfun Pro Micro 5v / 16MHz,并试图用C ++编写一个简单的GUI以通过串行端口发送消息,然后接收在Arduino板上运行的代码中指定的一些响应之一。

I know the communication and code running on the Arduino works because I have tried using the Serial Monitoring tool in the Arduino IDE and am able to send and receive the appropriate responses. 我知道在Arduino上运行的通信和代码可以工作,因为我尝试使用Arduino IDE中的串行监视工具,并且能够发送和接收适当的响应。

When sending messages through the C++ GUI I've created I see the traffic LED blinking on message send leading me to believe that send via C++ GUI is working, but nothing regarding receiving messages works. 通过我创建的C ++ GUI发送消息时,我看到消息发送上的流量LED闪烁,使我相信通过C ++ GUI发送是有效的,但是关于接收消息却没有任何效果。

Some of my code: 我的一些代码:

private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e)
        {
            String^ testStr = serialPort1->ReadExisting();
            log_textBox->Text += testStr + "\n";
        }

It's a very simple event handler that is assigned to the DataReceived event of my SerialPort object that is part of my form. 这是一个非常简单的事件处理程序,它分配给我的SerialPort对象的DataReceived事件,该事件是表单的一部分。 I've set a breakpoint inside of it, and it never gets triggered. 我已经在其中设置了一个断点,并且它从未触发过。 I'm kind of at a loss, I've done a nonexistent amount of work with Arduino or hardware so I'm not entirely sure what my options are for debugging. 我有点无所适从,我没有使用Arduino或硬件完成过很多工作,因此我不确定要调试的选项是什么。

If anyone has any debugging suggestions or ideas what I'm doing wrong I would very much appreciate it. 如果有人有任何调试建议或想法,我在做什么错,我将不胜感激。 Additionally, if I haven't provided any information let me know what is needed and I'll edit it in! 此外,如果我没有提供任何信息,请告诉我需要什么,然后我将其编辑!

Thank You, -K 谢谢-K

Finally figured this out after finding an obscure comment talking about settings. 在找到有关设置的晦涩注释后,终于明白了这一点。

It turns out that I needed the DTREnabled property of the Serial Port to be set to True as opposed to false in order for the DataReceived handler to fire. 事实证明,为了触发DataReceived处理程序,需要将“串行端口”的DTREnabled属性设置为True(而不是false)。 This setting was never set in any of the MSDN documentation regarding Serial Port communication. 任何有关串行端口通信的MSDN文档中都从未设置此设置。 If you are reading this and are having a similar problem, make sure that DTREnabled, RTSEnabled are both set to true, and perhaps even set Handshaking to RequestToSend. 如果您正在阅读本文,并且遇到类似的问题,请确保将DTREnabled和RTSEnabled都设置为true,甚至将Handshaking设置为RequestToSend。 Perhaps one or all of these will be what ends up fixing it. 也许其中之一或全部将最终得到解决。

In my case, only DTREnabled needed to be true. 就我而言,仅DTREnabled需要为真。 I don't quite understand what these settings do, but there you have it. 我不太了解这些设置的作用,但是您已经掌握了这些设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM