poilkjewq
Posts: 14
Joined: Fri Jun 15, 2018 9:50 am

How to creat GUI in C++

Thu Jul 12, 2018 10:30 am

Hi everyone! The following Client C++ code is used for the raspberry pi communicate with serial device and then sending back the response to client(Windows OS PC) by using TCP server. The code is using command prompt to get the user input. I would like to create user interface to get the user input. However, I don't have any experience on creating GUI.

In my project, my connection of the device and raspberry pi like this:
[Serial Device]===[usb to serial db9 adaptor]===[raspberry pi]===[Ethernet]===[Computer(Windows command prompt)]

Anyone can tell me what software can do it easily, such as Qt or visual studio? Also, anyone can provide me some tutorial about GUI?

Thank You very much!!! :D :D

Code: Select all

#include<stdio.h>
#include<winsock2.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<errno.h>
using namespace std;

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc, char *argv[])
{
	WSADATA wsa;
	SOCKET s;
	struct sockaddr_in server;
	char *message, server_reply[2000];
	int recv_size;

	char trans[20];
	string yn;
	int x;
	int y;
	string a;
	string b;
	string c;

	printf("\nInitialising Winsock...");
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
		printf("Failed. Error Code : %d", WSAGetLastError());
		return 1;
	}

	printf("Initialised.\n");

	//Create a socket
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
	{
		printf("Could not create socket : %d", WSAGetLastError());
	}

	printf("Socket created.\n");


	server.sin_addr.s_addr = inet_addr("10.1.1.43");
	server.sin_family = AF_INET;
	server.sin_port = htons(5000);

	//Connect to remote server
	if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
	{
		puts("connect error");
		return 1;
	}

	puts("Connected");


	//Request user input then convert input to command and send it to server.
	cout << "Enter the command: ";
	cin >> x;

	if (x == 1) { strcpy(trans, "FW\r\n"); } //firmware version

	else if (x == 7)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "CMDECHO\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			if (a == "off") strcpy(trans, "CMDECHO OFF\r\n");
			else if (a == "on") strcpy(trans, "CMDECHO ON\r\n");
		}
	}

	else if (x == 8)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "BAUDRATE\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			string command = "BAUDRATE ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
	}


	else if (x == 10)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "LTCONST\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			string command = "LTCONST ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
	}

	else if (x == 11)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "UPWRLIM\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			cout << "Any parameter else?" << endl;
			cin >> yn;
			if (yn == "no")
			{
				string command = "UPWRLIM ";
				string end = "\r\n";
				a = command + a + end;
				strcpy(trans, a.c_str());
			}
			else if (yn == "yes")
			{
				cout << "Enter parameter: ";
				cin >> b;
				string command = "UPWRLIM ";
				string end = "\r\n";
				string space = " ";
				a = command + a + space + b + end;
				strcpy(trans, a.c_str());
			}
		}
	}

	else if (x == 12)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "ATTEN\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			string command = "ATTEN ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
	}

	else if (x == 13)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "INPWR\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			string command = "INPWR ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
	}

	else if (x == 14)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "OUTPWR\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			string command = "OUTPWR ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
	}

	else if (x == 15) { strcpy(trans, "TEMP\r\n"); }

	else if (x == 16)
	{
		cout << "Any parameter?" << endl;
		cin >> yn;
		if (yn == "no") { strcpy(trans, "ALRMST\r\n"); }
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> a;
			cout << "Any parameter else?" << endl;
			cin >> yn;
			if (yn == "no")
			{
				string command = "ALRMST ";
				string end = "\r\n";
				a = command + a + end;
				strcpy(trans, a.c_str());
			}
			else if (yn == "yes")
			{
				cout << "Enter parameter: ";
				cin >> b;
				string command = "ALRMST ";
				string end = "\r\n";
				string space = " ";
				a = command + a + space + b + end;
				strcpy(trans, a.c_str());
			}
		}
	}

	else if (x == 17)
	{
		cout << "Enter parameter: ";
		cin >> a;
		cout << "Any parameter else?" << endl;
		cin >> yn;
		if (yn == "no")
		{
			string command = "ALRMTH ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> b;
			cout << "Any parameter else?" << endl;
			cin >> yn;
			if (yn == "no")
			{
				string command = "ALRMTH ";
				string end = "\r\n";
				string space = " ";
				a = command + a + space + b + end;
				strcpy(trans, a.c_str());
			}
			else if (yn == "yes")
			{
				cout << "Enter parameter: ";
				cin >> c;
				string command = "ALRMTH ";
				string end = "\r\n";
				string space = " ";
				a = command + a + space + b + space + c + end;
				strcpy(trans, a.c_str());
			}
		}
	}

	else if (x == 18)
	{
		cout << "Enter parameter: ";
		cin >> a;
		cout << "Any parameter else?" << endl;
		cin >> yn;
		if (yn == "no")
		{
			string command = "ALRMHY ";
			string end = "\r\n";
			a = command + a + end;
			strcpy(trans, a.c_str());
		}
		else if (yn == "yes")
		{
			cout << "Enter parameter: ";
			cin >> b;
			cout << "Any parameter else?" << endl;
			cin >> yn;
			if (yn == "no")
			{
				string command = "ALRMHY ";
				string end = "\r\n";
				string space = " ";
				a = command + a + space + b + end;
				strcpy(trans, a.c_str());
			}
			else if (yn == "yes")
			{
				cout << "Enter parameter: ";
				cin >> c;
				string command = "ALRMHY ";
				string end = "\r\n";
				string space = " ";
				a = command + a + space + b + space + c + end;
				strcpy(trans, a.c_str());
			}
		}
	}

	else if (x == 19) { strcpy(trans, "LALRM\r\n"); }

	else if (x == 20) { strcpy(trans, "RESET\r\n"); }

	else if (x == 21) { strcpy(trans, "RESTART\r\n"); }

	else if (x == 22) { strcpy(trans, "DEFAULT\r\n"); }

	else if (x == 23) { strcpy(trans, "UPDATEFW\r\n"); }

	else { cout << "Invalid input." << endl; }


	//trans some data
	message = trans;
	if (send(s, message, strlen(message), 0) < 0)
	{
		puts("trans failed");
		return 1;
	}
	puts("Data trans\n");

	//Receive a reply from the server
	if ((recv_size = recv(s, server_reply, 2000, 0)) == SOCKET_ERROR)
	{
		puts("recv failed");
	}

	puts("Reply received\n");

	//Add a NULL terminating character to make it a proper string before printing
	server_reply[recv_size] = '\0';
	puts(server_reply);

	return 0;
}

ghans
Posts: 7752
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: How to creat GUI in C++

Fri Jul 13, 2018 6:20 am

QtCreator seems like a good choice.
Please note that WinSock does not work on Linux. Qt itself might have networking libraries so that you can use the same code on Windows and Linux.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Heater
Posts: 9342
Joined: Tue Jul 17, 2012 3:02 pm

Re: How to creat GUI in C++

Fri Jul 13, 2018 9:36 am

Yes, use the Qt GUI libraries and optionally use the qtcreator IDE. qtcreator has a drag and drop GUI designer to help.

Qt has networking built in. There is a serial port library for Qt as well, not sure if it is in the actual Qt libs now a days.

With that in place your C++ code should build and run on Linux, Windows and Mac. Which has the advantage that you do development in the comfort of a PC and then when the code looks good build, run and test it on the Pi.

poilkjewq
Posts: 14
Joined: Fri Jun 15, 2018 9:50 am

Re: How to creat GUI in C++

Fri Jul 13, 2018 1:49 pm

Thank you very much! :D :D

I will try to use Qtcreator to implement it.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 3 guests