Results 1 to 3 of 3

Thread: Recieving real-time data from serial port and plotting in a graph

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Recieving real-time data from serial port and plotting in a graph

    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:

    Qt Code:
    1. char bloco[MAXMSG];
    2. FILE *txt;
    3. char Linha[100], at[100];
    4. char *result;
    5. int n, arq0, arq1, arq2, arq3;
    6.  
    7. int Abre_Porta_Serial(int baudflag)
    8. {
    9. struct termios mytty, kbdios;
    10. // char *portname0="/dev/ttyS0";
    11. char *portname0="/dev/ttyUSB0";
    12.  
    13. printf("Start\n");
    14. printf("Configurando COM\n");
    15.  
    16. if((arq0=open(portname0, O_RDWR)) < 0)
    17. {
    18. perror(portname0);
    19. return 1;
    20. }
    21. tcgetattr(arq0, &kbdios);
    22. kbdios.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
    23. kbdios.c_oflag &= ~OPOST;
    24. kbdios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    25. kbdios.c_cflag &= ~(CSIZE|PARENB);
    26. kbdios.c_cflag |= CS8 + CSTOPB;
    27. kbdios.c_cflag &= ~CSTOPB;
    28. /* Set Baud Rate to 115200 baud */
    29. cfsetispeed(&kbdios, baudflag);
    30. cfsetospeed(&kbdios, baudflag);
    31. if(tcsetattr(arq0, TCSANOW, &kbdios)==-1)
    32. {
    33. perror(NULL);
    34. close(arq0);
    35. return -1;
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. int Le_Porta_Serial(char tipo_resposta[])
    2. {
    3. char buf[MAXMSG], bloco[MAXMSG], *look, ok[]="OK", erro[]="ERROR";
    4. int Resp, n=0, m=0, i=0, timeout=0;
    5.  
    6. strcpy(buf,"");
    7. strcpy(bloco,"");
    8. timeout=0;
    9. while(1)
    10. {
    11. n=read(arq0, buf,100);
    12. strncat(bloco, buf, n);
    13. m=n+m;
    14. bloco[m+1]='0';
    15.  
    16. look = strstr(bloco, tipo_resposta);
    17. if (look)
    18. {
    19. printf("Leitura Porta Serial:\n%s\n", bloco);
    20. strcpy(bloco,"");
    21. return 1;
    22. }
    23. look = strstr(bloco, erro);
    24. if (look)
    25. {
    26. printf("Leitura Porta Serial:\n%s\n", bloco);
    27. strcpy(bloco,"");
    28. return -1;
    29. }
    30. strcpy(buf,"");
    31. timeout++;
    32. }
    33. }
    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.

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Recieving real-time data from serial port and plotting in a graph

    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

  3. #3
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Recieving real-time data from serial port and plotting in a graph

    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/...tarball/master

Similar Threads

  1. Real time data plotting using Qwt
    By nagabathula in forum Qwt
    Replies: 8
    Last Post: 7th July 2011, 16:47
  2. real time graph plot
    By robotics in forum Qt Programming
    Replies: 5
    Last Post: 24th May 2011, 05:04
  3. real time plotting
    By agostain in forum Qwt
    Replies: 0
    Last Post: 10th August 2009, 10:47
  4. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 10:19
  5. real time plotting
    By gyre in forum Qwt
    Replies: 4
    Last Post: 11th December 2007, 16:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.