Hello!
As I indicated in previous threads (like: http://www.qtcentre.org/threads/4250...indow-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|IXON);
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;
}
}
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|IXON);
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;
}
}
To copy to clipboard, switch view to plain text mode
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++;
}
}
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++;
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks