简体   繁体   English

将C ++代码与GUI集成

[英]Integrating C++ Code with a GUI

I am trying to implement a security system as part of my Microcontrollers term project. 我正在尝试将安全系统作为微控制器学期项目的一部分。 I am almost done with the software end of things. 软件方面的工作我几乎完成了。 I just need to design a GUI that will serve as an indicator or an alarm device for my security system. 我只需要设计一个GUI,它将用作我的安全系统的指示器或警报设备。 The microcontroller makes use of the sensing circuitry to check if the system has been breached and it sends the alarm signal serially to a PC. 微控制器利用传感电路检查系统是否被破坏,并将警报信号串行发送到PC。 I am reading data off the PC serial port using C++ and the whole thing works wonderfully. 我正在使用C ++从PC串行端口读取数据,整个过程运行非常好。 However, I am doing it in a console based environment. 但是,我正在基于控制台的环境中进行操作。 That's all I have ever known. 这就是我所知道的。 I am studying to be an Electronic Engineer. 我正在学习成为一名电子工程师。 :P :P

That's not acceptable, however, and I need to design a GUI for my project. 但是,那是不可接受的,我需要为我的项目设计一个GUI。

Question 1: Is there anyway I can integrate my existing code with a GUI? 问题1:无论如何,我可以将现有代码与GUI集成吗? I have googled this already without much luck. 我已经用谷歌搜索了,没有多大运气。 All the answers seem to point in the direction of external libraries like Qt etc. This is kind of scary because I don't have a lot of time. 所有答案似乎都指向外部库(例如Qt等)的方向。这有点吓人,因为我没有很多时间。

Question 2: As I mention earlier, I am kind of intimidated by the thought of having to use external libraries etc. so I tried to make a GUI on Visual Studio using Visual C++ based Windows Form Applications. 问题2:正如我前面提到的,我对必须使用外部库等的想法感到有些害怕。因此,我尝试使用基于Visual C ++的Windows窗体应用程序在Visual Studio上创建GUI。 I used the serialport utility from the Toolbox. 我使用了工具箱中的serialport实用程序。 I haven't been able to read data off the COM port that way but I can write data to it just fine. 我还无法从COM端口读取数据,但可以向其中写入数据。

This is what the form looks like: 表单如下所示:

在此处输入图片说明

I am trying to display the data read from the serial port on a textbox in response to a button press. 我正在尝试在文本框中显示从串行端口读取的数据以响应按钮的按下。 Here's the code I am using for the button: 这是我用于按钮的代码:

private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) 
{}

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
this->serialPort1->PortName = "COM1";  
this->serialPort1->Open();
this->serialPort1->BaudRate = 9600;
try
{
this->textBox1->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^)
{
this->textBox1->Text="Timeout Exception";
}
this->serialPort1->Close(); 

Can you tell me what I am doing wrong? 你能告诉我我做错了什么吗? I am new to this. 我是新来的。 This is my preferred method, btw. 顺便说一句,这是我的首选方法。

Using Windows Forms is definitely the way to go if you don't want to use external libraries, that's how I'd have done it too. 如果您不想使用外部库,那么使用Windows Forms绝对是必经之路,这也是我要做的。

I'm not exactly into these things, but using ReadLine() just once could be a possible source of the problem. 我不完全了解这些事情,但是仅使用ReadLine()可能是问题的根源。 Maybe the data coming in over the serial port is sent over more than just one line. 也许通过串行端口传入的数据不仅仅通过一行来发送。 Perhaps you'll have to loop through the input, continuously using ReadLine() and collecting the lines in an array until all data has been received. 也许您将不得不遍历输入,连续使用ReadLine()并收集数组中的行,直到接收到所有数据为止。

But perhaps there's also another command, something like ReadAllLines() , which reads all the input at once and returns it to you? 但是也许还有另一个命令,例如ReadAllLines() ,它可以一次读取所有输入并将其返回给您?

My immediate suspicion is the ReadLine() command - are you sure your MC is writing the endline characters? 我直接怀疑的是ReadLine()命令-您确定您的MC正在写结束符吗?

Use ReadFile instead and see if you can read single bytes. 请改用ReadFile,看看是否可以读取单个字节。 If not, then maybe you need to adjust more options. 如果没有,那么您可能需要调整更多选项。 If you get to the timeout exception, then you will need to adjust the read timeout (SetCommTimeouts). 如果遇到超时异常,则需要调整读取超时(SetCommTimeouts)。

Everything you need was described very well by Allen Denver here: http://msdn.microsoft.com/en-us/library/ff802693.aspx 艾伦·丹佛(Allen Denver)在这里很好地描述了您所需的一切: http : //msdn.microsoft.com/zh-cn/library/ff802693.aspx

As for which GUI, if whatever Windows Forms project you're using works, then stick with it. 至于哪个GUI,如果您正在使用的任何Windows Forms项目都可以工作,请坚持使用。 I personally used MFC for a similar final year project, but that was because I was already familiar with it. 我个人使用MFC完成了类似的最后一年项目,但这是因为我已经熟悉了它。

For next time though, learn C#. 对于下一次,请学习C#。 C# and Java are far, far easier and quicker for creating GUIs. C#和Java在创建GUI方面远非如此,而且更加快捷。

As for your question 1, I recommend you to use the MFC library, it is quite easy. 关于问题1,我建议您使用MFC库,这很容易。 Here is a good example for you to start. 这是一个很好的示例,供您开始。 http://depts.washington.edu/cmmr/biga/chapter_tutorials/1.C++_MFC_D3DOGL/1.StepByStepGuide/index.html http://depts.washington.edu/cmmr/biga/chapter_tutorials/1.C++_MFC_D3DOGL/1.StepByStepGuide/index.html

And based on what I understand, You are already able to read and write the data in console based environment. 根据我的理解,您已经能够在基于控制台的环境中读写数据。 The GUI is just used to enter or display the data. GUI仅用于输入或显示数据。 In this case, you could program a MFC project easily which serves your purpose. 在这种情况下,您可以轻松地为您的目的编程MFC项目。

And for your 2nd question, if you tried to display the data read from the serial port on a edit box in response to a button press. 对于第二个问题,如果您尝试在响应按钮按下的情况下在编辑框中显示从串行端口读取的数据。 You should first add a variable to the edit box, for example edit_box_value . 您应该首先将一个变量添加到编辑框中,例如edit_box_value And in that button clicked function, implement a code like following: 然后在该按钮单击的函数中,实现如下代码:

edit_box_value.Format(_T("%s"), data_to_be_displayed);
UpdateData(FALSE); 

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

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