简体   繁体   中英

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. 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. I am reading data off the PC serial port using C++ and the whole thing works wonderfully. 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

That's not acceptable, however, and I need to design a GUI for my project.

Question 1: Is there anyway I can integrate my existing code with a 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.

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. I used the serialport utility from the Toolbox. I haven't been able to read data off the COM port that way but I can write data to it just fine.

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.

I'm not exactly into these things, but using ReadLine() just once could be a possible source of the problem. 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.

But perhaps there's also another command, something like ReadAllLines() , which reads all the input at once and returns it to you?

My immediate suspicion is the ReadLine() command - are you sure your MC is writing the endline characters?

Use ReadFile instead and see if you can read single bytes. 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).

Everything you need was described very well by Allen Denver here: http://msdn.microsoft.com/en-us/library/ff802693.aspx

As for which GUI, if whatever Windows Forms project you're using works, then stick with it. I personally used MFC for a similar final year project, but that was because I was already familiar with it.

For next time though, learn C#. C# and Java are far, far easier and quicker for creating GUIs.

As for your question 1, I recommend you to use the MFC library, it is quite easy. 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

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. In this case, you could program a MFC project easily which serves your purpose.

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 . And in that button clicked function, implement a code like following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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