Results 1 to 2 of 2

Thread: read data from serial port and display it

  1. #1
    Join Date
    Jan 2013
    Posts
    44
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default read data from serial port and display it

    hi everyone
    i have write a simple code to read and display data that received from serial port with qextserialport ,i have compiler without any error but nothing was execute,plz solve it hep me
    this's my code
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <qextserialport.h>
    4. #include "stdlib.h"
    5. #include "stdio.h"
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. QextSerialPort * port = new QextSerialPort("COM2");
    13. port->setBaudRate(BAUD115200);
    14. port->setDataBits(DATA_8);
    15. port->setParity(PAR_NONE);
    16. port->setStopBits(STOP_1);
    17. port->setFlowControl(FLOW_OFF);
    18. port->open(QextSerialPort::ReadWrite);
    19. //while(1)
    20. //{
    21. char buff[1024];
    22. int bytesToRead = port->bytesAvailable();
    23. int result = (int)port->read(buff,bytesToRead);
    24. buff[result];
    25. char byte1[3];
    26. char byte2[3];
    27. byte1[0] = buff[0];
    28. byte1[1] = buff[1];
    29. byte1[2] = buff[2];
    30. byte2[0] = buff[3];
    31. byte2[1] = buff[4];
    32. byte2[2] = buff[5];
    33. long int B1=byte1[0]<<16|byte1[1]<<8|byte1[2];
    34. long int B2=byte2[0]<<16|byte2[1]<<8|byte2[2];
    35. long int C1;
    36. long int C2;
    37. char * next;
    38. C1 = strtol(byte1,&next,16);
    39. C2 = strtol(byte2,&next,16);
    40. ui->lcdNumber->display(B1);
    41. ui->lcdNumber_2->display(B2);
    42. ui->lcdNumber_3->display(C1);
    43. ui->lcdNumber_4->display(C2);
    44. //}
    45. }
    46.  
    47. MainWindow::~MainWindow()
    48. {
    49. delete ui;
    50.  
    51. }
    To copy to clipboard, switch view to plain text mode 

    when i run it without send any data ,lcdnumber display is -6378 and lcdnumber2 dispaly is 4864 and lcdnumber 3,4 is 0,
    but when i send data 0x10 0x20 0x30 0x40 0x50 0x60 ,it do have any changed in lcdnumber ...
    please help me

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: read data from serial port and display it

    The various Qt serial port libraries all operate asynchronously and require the Qt event loop to be running to receive data. Inside the constructor the event loop is not processing, so you receive nothing, and consequently nothing useful comes out of your remaining code. If you uncommented the while() loop your program will hang. The receive code should be in a slot connected to one of the QextSerialPort signals not in the constructor.

    Your remaining code is curious and asking to crash too, but one problem at a time.

Similar Threads

  1. Replies: 4
    Last Post: 2nd June 2012, 08:04
  2. read data from serial port
    By amitpatel22 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 20th July 2011, 18:11
  3. Replies: 4
    Last Post: 10th July 2010, 18:34
  4. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12
  5. First attempt to display serial port data on GUI
    By ShaChris23 in forum Newbie
    Replies: 12
    Last Post: 4th May 2007, 10:14

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.