PDA

View Full Version : Recieving real-time data from serial port and plotting in a graph



Momergil
22nd June 2011, 20:33
Hello!

As I indicated in previous threads (like: http://www.qtcentre.org/threads/42503-How-do-I-plot-two-or-more-graphs-in-the-same-window-software), I'm building a software aimed to capture ECG data and plot it in real-time graphic.

About the software the problem is over, since a friend of mine, who understands quite a bit of Qt, already have it. But still I will have to program the function that will read the data coming from a serial gate and than plot it.

In the almost-done software he will give me, there is already a location for where I will pass the data to the software in order to be ploted, so that's no problem [now]. But I still don't know how to read data from a serial port in C++ in a Qt environment. So, how do I do it?

By now, I already have a software in C which open a serial connection and read its data. The two function that do so are:


char bloco[MAXMSG];
FILE *txt;
char Linha[100], at[100];
char *result;
int n, arq0, arq1, arq2, arq3;

int Abre_Porta_Serial(int baudflag)
{
struct termios mytty, kbdios;
// char *portname0="/dev/ttyS0";
char *portname0="/dev/ttyUSB0";

printf("Start\n");
printf("Configurando COM\n");

if((arq0=open(portname0, O_RDWR)) < 0)
{
perror(portname0);
return 1;
}
tcgetattr(arq0, &kbdios);
kbdios.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IX ON);
kbdios.c_oflag &= ~OPOST;
kbdios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
kbdios.c_cflag &= ~(CSIZE|PARENB);
kbdios.c_cflag |= CS8 + CSTOPB;
kbdios.c_cflag &= ~CSTOPB;
/* Set Baud Rate to 115200 baud */
cfsetispeed(&kbdios, baudflag);
cfsetospeed(&kbdios, baudflag);
if(tcsetattr(arq0, TCSANOW, &kbdios)==-1)
{
perror(NULL);
close(arq0);
return -1;
}
}

and


int Le_Porta_Serial(char tipo_resposta[])
{
char buf[MAXMSG], bloco[MAXMSG], *look, ok[]="OK", erro[]="ERROR";
int Resp, n=0, m=0, i=0, timeout=0;

strcpy(buf,"");
strcpy(bloco,"");
timeout=0;
while(1)
{
n=read(arq0, buf,100);
strncat(bloco, buf, n);
m=n+m;
bloco[m+1]='0';

look = strstr(bloco, tipo_resposta);
if (look)
{
printf("Leitura Porta Serial:\n%s\n", bloco);
strcpy(bloco,"");
return 1;
}
look = strstr(bloco, erro);
if (look)
{
printf("Leitura Porta Serial:\n%s\n", bloco);
strcpy(bloco,"");
return -1;
}
strcpy(buf,"");
timeout++;
}
}

I imagine that it would look something like this in C++ (since C++ is quite similar to C), but I don't know how would it be in a Qt environment. Could somebody help, please?



Obs. 1: Notice that I'm using a serial-usb conversor instead of direct serial, but this doesn't interfer in the programming (just change the port of connection in the beginning).
Obs. 2: I'm using Linux Ubuntu 10.04.


Thank you for the help.

schnitzel
22nd June 2011, 22:21
take a look at qextserialport: http://code.google.com/p/qextserialport/

the source comes with examples, that should get you well on your way. In order to download the latest code, you have to go to the source repository and use mercurial.

good luck

kuzulis
24th June 2011, 06:31
But better use QSerialDevice.
https://gitorious.org/qserialdevice

This project is developing dynamically and provides more than QextSerialPort.
Also available as ready-console and GUI examples (see /examples, /test).

Direct download link:
https://gitorious.org/qserialdevice/qserialdevice/archive-tarball/master