Integrating C++ Code with a GUI - c++

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);

Related

Listening to and calling wxPython event from another process

I'm currently trying to develop a proof of concept for a possible idea but am unsure if this is even possible.
We have application A, written in wxPython, which will not receive any code updates. It's too late in its life and will impact too many people. In a specific case which we wish to automatic - a GUI window pops up asking user to select an option from the list.
We want to develop script or "application B" which listens for this GUI to pop up and automatically makes the choice. It can be a script that runs in the background at all times.
Now.. from my understanding I don't think it's possible without some interprocess communication going on. Since we cannot change anything in application A the problem seems unsolvable. Any ideas? Am I missing something about how I could use wxPython?

Windows 10 detect keyboard layout change

I'm trying to implement a service that would monitor language/layout changes. I switch between English and Russian languages. So far I've found this question and tried to implement and install both sinks suggested there.
However, there are problems. ITfActiveLanguageProfileNotifySink::OnActivated is not triggered at all; ITfLanguageProfileNotifySink::OnLanguageChange is not triggered, too; ITfLanguageProfileNotifySink::OnLanguageChanged is triggered only when the main window of my program is in foreground, but it doesn't contain any information on the language. Is there any way to monitor input language change event globally?
I found another way to detect such changes: to use SetWindowsHookEx with WH_SHELL. One of the available event is HSHELL_LANGUAGE which is exactly what I need and the test project seems to work just fine.
There's an article by Alexander Shestakov that describes a program very similar to what I'm trying to achieve and it also has a link to github project that I use as an example.

Replacing std::cout output with windows form

I made a program in c++ (visual studio 2010) that looks for serial com ports and compares their friendly names with defined text. When there is a match that port is opened/connected and serial communication starts.
The program notifies the user when a com port is found, whether the connection was successful or not and if the data send was successful or not and other useful information. The program uses cout to notify the user.
I want to replace console output window with windows form but cant find much resources online on how to do this. To illustrate, I want this:
To become this:
I included form1.h and other files and tried replacing cout with below line but code is not compiling:
Form1::textBox1->Text = L" Text I want to display";
Can anyone explain how to use textBox1, or a tutorial for this?
"I included form1.h" - You can't just grab random files and hope it works. That is not how C++ works, or computers in general.
How then do you do something like this? The Standard Library provides std::cout and Visual Studio by default includes the Standard Library, so using it is fairly easy. But for graphics, you will need another library. I recommend Qt, if only because there are good tutorials for beginners.
So I finally did achieve the functionality I described above in my question and thought I should post my findings here.
To convert my code from console output to Windows Form I basically had to migrate from c++ to C++/cli.
holowczak.com has a great tutorial on how to get started with windows form (c++/cli) in visual studio.
Next if there is any busy looping(like infinite while-loop) in your c++ code then you would need to run that busy loop on a separate thread or the program can hang. Dr.Dobb's tutorial on how to create and mange threads in c++/cli, can help a lot.
Finally, if you need to access resources (such as textbox and other controls) of windows form from another thread then a thread-safe call has to be made. Microsoft's "HowTo:Make thread-safe call to windows Form controls" explains how the invoke method can be used for updating textbox from another thread.

Automatically filling a form and running a program

A friend sent me 3 programs, which are written in C or C++. Every program has a form, which is filled and then a button is pressed to perform some calculation. The programs were written in LabWindows/CVI which I've never heard of. He asked me, if it was possible to automatically fill the forms and run the calculation to get the output. I said that one way would be, to modify the program to a console one and run it with parameters (if the number of fields is low).
Does there exist a tool, which could be used to automate the execution of such programs or, if possible, does the LabWindows/CVI have anything which could help me, besides modifying the source?
I thank you in advance for you help.
you could use some automated test programs such as http://www.sikuli.org (this is written in java), it is also nice because you can add any java code you want to improve the test and modify it if you really need something extraordinary.
another way would be creating your own "automated test program" using c++ (since you posted this with c++ tag) to create a mouse and keyboard hook that will move the mouse and type characters on your forms.
the following links might help if you want to create your hooks C++ mouse click on certain spot in window, Global keyboard hook with WH_KEYBOARD_LL and keybd_event (windows)

Linux c++ console get key state

I want to create command line game in Linux but I don't know to get the key state. I heard of getch() but that stops the program.
I spent a little while reading around. Apparently, this is a hard thing to do without the help of a library. Many people recommended the library ncurses. If you want to try to do it by yourself, you need to learn about switching terminal modes and crazy stuff like that. This thread was very informative: Non-blocking keyboard read - C/C++
In this article, the author implements a cKeyboard class, which directly listens for events from /dev/event0. The class is then used as follows:
#include "keyboard.h"
cKeyboard kb;
...
if (kb.getKeyState(KEY_UP)) {
// do something
}
It works perfectly for me, but I had to change event0 to event4.