Results 1 to 6 of 6

Thread: Fail To Write byte to device via QSerialPort

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Fail To Write byte to device via QSerialPort

    My system is Windows 8.1 and use qt5.4. I use the "terminal" example to check whether I can send an effective commands to the devices. To device A the write command is in string(readable characters) type and the communication is fine. However, it seems the write command is rejected and never get readready signal from device B which could only recognize the command in bytes. Are there any mistakes in my code? If not, are there any other possible ways I can try and learn? Handle in Qserialport might be the one, but I do not know how to use it properly. After the port is opened, let

    hCOMM=serial->handle(); //declare HANDLE hCOMM; in header file

    however it's still fail to have read command

    WriteFile(hCOMM,startI,5,&ByteRead,NULL);
    ReadFile(hCOMM, InBuff, 50, &ByteRead, NULL);
    if(ByteRead){
    qDebug()<<"PASS"<<endl;
    }
    else{

    qDebug()<<"FIAL"<<endl; //qDebug() shows "FAIL"
    }




    Another question, since the baudrate for device B is 5000000 baud/s, how could I set this large number in qt code effectively?

    PS The devices are functional, verified with BCB on Windows XP.

    ================================================== ================

    The code for detecting the device A with command in string is
    Qt Code:
    1. void MainWindow::writeData(const QByteArray &data)
    2. {
    3. serial->write("*IDN?\n"); //Get the product name and firmware version
    4. qDebug()<<(serial->bytesToWrite() )<<"- Can Read? "<<(serial->canReadLine())
    5. <<"|| num "<<(serial->readBufferSize() )<<"bytesAvailable?"<<(serial->bytesAvailable())
    6. <<"|| Sequential?"<<(serial->isSequential())
    7. <<"END?"<<(serial->atEnd() );
    8. }
    9.  
    10. void MainWindow::readData()
    11. { qDebug()<<(serial->bytesToWrite() )<<"- Can Read? "<<(serial->canReadLine())
    12. <<"|| num "<<(serial->readBufferSize() )<<"bytesAvailable?"<<(serial->bytesAvailable())
    13. <<"|| Sequential?"<<(serial->isSequential())
    14. <<"END?"<<(serial->atEnd() );//<<endl;
    15. QByteArray data = serial->readAll();
    16.  
    17. console->putData(data);
    18. qDebug()<<"END2?"<<(serial->atEnd() )<<endl;
    19. }
    To copy to clipboard, switch view to plain text mode 

    The result in debug

    6 - Can Read? false || num 0 bytesAvailable? 0 || Sequential? true END? true
    0 - Can Read? false || num 0 bytesAvailable? 1 || Sequential? true END? false
    END2? true

    0 - Can Read? false || num 0 bytesAvailable? 5 || Sequential? true END? false
    END2? true

    The code for detect the device B with command in byte:
    Qt Code:
    1. void MainWindow::writeDataI(const QByteArray &data)
    2. {
    3. const char startI[5]={0xA2,0xD0,0x00,0x00,0x55}; //Get the default setting.
    4. serial->write(startI,5);
    5. qDebug()<<(serial->bytesToWrite() )<<"- Can Read? "<<(serial->canReadLine())
    6. <<"|| num "<<(serial->readBufferSize() )<<"bytesAvailable?"<<(serial->bytesAvailable())
    7. <<"|| Sequential?"<<(serial->isSequential())
    8. <<"END?"<<(serial->atEnd() );
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    The result in debug (never get into the readData)

    5 - Can Read? false || num 0 bytesAvailable? 0 || Sequential? true END? true
    Last edited by leisha; 15th April 2015 at 17:22. Reason: updated contents

Similar Threads

  1. Replies: 1
    Last Post: 1st April 2014, 08:48
  2. How to write the BYTE* data to a QFile?
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 23rd November 2011, 04:33
  3. Replies: 2
    Last Post: 2nd November 2010, 05:15
  4. Replies: 3
    Last Post: 19th April 2010, 14:16
  5. Replies: 1
    Last Post: 16th June 2009, 09:09

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.