How to connect to a serial port in Linux using c++?

In Linux all devices have a file in /dev directory, so the communication with these devices is very simple, just need to open necessary file, and make read and write operations upon them.

In this article is shown how to connect to serial port in Linux, using c++.

First of all we include all needed libraries, and declare all necessary variables:

#include <stdio.h>

#include <string.h>

#include <fcntl.h>

#include <errno.h>

#include <termios.h>

#include <unistd.h>

int fd1;

int fd2;

char *buff,*buffer,*bufptr;

int wr,rd,nbytes,tries;

int main()

{

return 0;

}

Next step, we connect to device through associated file and check the connection:

fd1=open(“/dev/ttyS0”, O_RDWR | O_NOCTTY | O_NDELAY);

if (fd1 == -1 )

{

perror(“open_port: Unable to open /dev/ttyS0 – “);

}

else

{

fcntl(fd1, F_SETFL,0);

printf(“Port 1 has been sucessfully opened and %d is the file description\n”,fd1);

}

Where, “/dev/ttyS0” is associated with COM1 port.

With following code we send to device some bits:

wr=write(fd1,”ATZ\r”,4);

And for reading response from device:

rd=read(fd1,buff,10);

printf(“Bytes sent are %d \n”,rd);

At the end, close the connection:

close(fd1);

Enjoy!

Advertisements

How to connect to a MySQL DB using c++ Linux?

C++ is a very good programming language with high-level and low-level capabilities.

Linux is a Unix-like computer operating system. It promotes community of free and open source software. Almost using those instruments it is possible to make a complex and sophisticate enterprise system.

MySQL is a multithreaded, multi-user SQL database management system. It is a popular database system, which has more than 10 million installations.

In this article is shown how to connect to a MySQL database using C++ under a Linux operating system.

First of all we include all needed libraries:

#include <sys/time.h>

#include <stdio.h>

#include <mysql.h>

Now we declare main function:

int main(char **args)

{

// code

return 0;

};

Next, we declare all necessary variables:

MYSQL_RES *result;

MYSQL_ROW row;

MYSQL *connection, mysql;

int state;

Of course if we want to use MySQL database, we must connect to it, using the next code:

mysql_init(&mysql);

connection = mysql_real_connect(&mysql,host,usr,pswd,database,0,0,0);

Where

host – is a host name, the location of the MySQL database server, for example “localhost” or “remotehost.com”

usr – is the username

pswd – is the password

database – is the name of the MySQL database from the Host.

If something goes wrong, for example password is not right, we must know it, and display the error message:

if (connection == NULL)

{

printf(mysql_error(&mysql));

return 1;

}

Now we make a simple query like “SELECT * FROM mytable” and check if it has no errors, where “mytable” is the name of wished table:

state = mysql_query(connection, “SELECT * FROM mytable”);

if (state !=0)

{

printf(mysql_error(connection));

return 1;

}

After the successful execution of the query, we must store the results somewhere:

result = mysql_store_result(connection);

Using mysql_num_rows function, we can get number of rows from result:

printf(“Rows:%d\n”,mysql_num_rows(result));

Using while statement and mysql_fetch_row functions, it possible to process each row in the result set:

while ( ( row=mysql_fetch_row(result)) != NULL )

{

printf(” %s, %s\n”, (row[0] ? row[0] : “NULL”), (row[1] ? row[1] : “NULL” ));

}

At the end, we must free the memory:

mysql_free_result(result);

mysql_close(connection);

IMPORTANT!!! How to make this code under Linux?

g++ test.cpp -I/usr/include/mysql /usr/lib/mysql/libmysqlclient.so

Enjoy!