Results 1 to 2 of 2

Thread: write data using QextSerialPort is writing 0 bytes always

  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default write data using QextSerialPort is writing 0 bytes always

    Hi, I am trying to send 1/0 to my ARDUINO Board & trying to receive some data from board as a response , I am using QextSerialPort for this But I am not able to write any data to the board & not able to receive any data also.

    http://docs.qextserialport.googlecode.com/git/1.2/qextserialport.html

    qDebug() << "send.size() : " << send.size() << " data = " << send.data() <<" Written = " << port->write(send, send.size());
    This printing : send.size() : 1 data = 1 Written = 0 //Means I am writing 0 bytes every time

    Is there a problem in my code ??


    Qt Code:
    1. void MainWindow::ledOnOff(bool on)
    2. {
    3. if(port == 0)
    4. {
    5. port = new QextSerialPort("COM6", QextSerialPort::EventDriven); //QextSerialPort* port is class member
    6. port->setBaudRate(BAUD9600);
    7. port->setFlowControl(FLOW_OFF);
    8. port->setParity(PAR_NONE);
    9. port->setDataBits(DATA_8);
    10. port->setStopBits(STOP_2);
    11.  
    12. connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    13.  
    14. if(port->open(QIODevice::ReadWrite) == true)
    15. {
    16. qDebug() << "Port open success";
    17. }
    18. else
    19. {
    20. qDebug() << "Port open success";
    21. }
    22. }
    23. else
    24. {
    25. port->close();
    26. }
    27.  
    28. quint8 writeByte = 0;
    29.  
    30. if(on)
    31. {
    32. writeByte = 1;
    33. }
    34.  
    35. if(port->isOpen() || port->open(QIODevice::ReadWrite) == true)
    36. {
    37. QByteArray send;
    38. send.resize(writeByte );
    39. send = QByteArray::number(writeByte);
    40.  
    41. port->flush();
    42. qDebug() << "send.size() : " << send.size() << " data = " << send.data()
    43. <<" Writtend = " << port->write(send, send.size());
    44. }
    45. else
    46. {
    47. qDebug() << "device failed to open:" << port->errorString();
    48. }
    49. }
    50.  
    51.  
    52. void MainWindow::onReadyRead()
    53. {
    54. QByteArray bytes;
    55. quint8 a = port->bytesAvailable();
    56. bytes.resize(a);
    57. port->read(bytes.data(), bytes.size());
    58.  
    59. qDebug() << bytes.constData();
    60. }
    To copy to clipboard, switch view to plain text mode 


    Thanks In Advance.
    Thanks :-)

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: write data using QextSerialPort is writing 0 bytes always

    Moved to QSerialPort. Working now

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow), Serial(NULL)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->ledBut, SIGNAL(clicked(bool)), this, SLOT(ledOnOff(bool)));
    7. setup();
    8. }
    9.  
    10. MainWindow::~MainWindow()
    11. {
    12. delete ui;
    13. Serial->close();
    14. delete Serial;
    15. Serial = NULL;
    16. }
    17.  
    18. void MainWindow::ledOnOff(bool on)
    19. {
    20. int write = -1;
    21.  
    22. if(on)
    23. {
    24. write = 1;
    25. ui->ledBut->setText("OFF");
    26. }
    27. else
    28. {
    29. write = 0;
    30. ui->ledBut->setText("OFF");
    31. }
    32.  
    33. if(Serial->isOpen() || this->Serial->open(QIODevice::ReadWrite))
    34. {
    35. this->Serial->setBaudRate(QSerialPort::Baud9600);
    36. this->Serial->setParity(QSerialPort::NoParity);
    37. this->Serial->setStopBits(QSerialPort::OneStop);
    38. this->Serial->setFlowControl(QSerialPort::NoFlowControl);
    39. this->Serial->setDataBits(QSerialPort::Data8);
    40.  
    41. QByteArray dayArray;
    42. dayArray[0]= write;
    43. qDebug() << "Written Bytes : " << this->Serial->write(dayArray);
    44. this->Serial->waitForBytesWritten(-1);
    45. }
    46. else
    47. {
    48. qDebug() << "Failled open com port";
    49. }
    50. }
    51.  
    52. void MainWindow::onReadyRead()
    53. {
    54. QByteArray bytes;
    55. int a = Serial->bytesAvailable();
    56. bytes.resize(a);
    57. Serial->read(bytes.data(), bytes.size());
    58. qDebug() << "bytes read:" << bytes.size() << " Written data = " << bytes.data();
    59. }
    60.  
    61. void MainWindow::errorReport(QSerialPort::SerialPortError error)
    62. {
    63. if(error!=0)
    64. qDebug()<<"ERROR:"<<endl<<error;
    65. }
    66.  
    67. void MainWindow::setup()
    68. {
    69. if(!Serial)
    70. {
    71. this->Serial=new QSerialPort(this);
    72. this->Serial->setPortName("COM6");
    73. this->Serial->setBaudRate(QSerialPort::Baud9600);
    74. connect(this->Serial,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorReport(QSerialPort::SerialPortError)));
    75. connect(Serial, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    76. }
    77. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

Similar Threads

  1. Replies: 4
    Last Post: 23rd October 2012, 08:40
  2. WriteFile in QT... Too many bytes to write?
    By AlphaWolfXV in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2009, 01:22
  3. Reading and writing bytes in file
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 20:25
  4. Writing raw bytes as text?
    By DiamonDogX in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2009, 16:56
  5. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12

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.