Results 1 to 6 of 6

Thread: Fail To Write byte to device via QSerialPort

  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 18:22. Reason: updated contents

  2. #2
    Join Date
    Mar 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Fail To Write byte to device via QSerialPort

    My problem above should be narrowed down to 2 parts:
    (1) How could I convert unsigned char* or qint8 to qbytearray properly?
    (2) How could I actully set baudrate as 5000000 for a serialport?

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fail To Write byte to device via QSerialPort

    1. Convert char* to QByteArray : use suitable QByteArray constructor.
    2. Convert qint8 to QByteArray : read about functions qToBigEndian and qToLitleEndian.
    3. COM port speed : what serial port library are You using ?

  4. #4
    Join Date
    Mar 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Fail To Write byte to device via QSerialPort

    Thank you for the reply. Before I have posted the previous thread, I had tried your method
    " 1. Convert char* to QByteArray : use suitable QByteArray constructor."
    but nothing had improved. Then I thought maybe the problem was at the setting of baudrate.
    The high speed baudrate requirement is by MCU/ ARM via a UART from FTDI. Since I do not know how to set the such high speed in QT, I tried to use FTDI API yesterday. With correct setting, the communication is fine. By the way, could I ask a very simple question what qint32 really is? I want to set QSerialPort::setBaudRate as :serial->setBauRate(5000000) but it has compiler error. If I can make sure there is no problem in baudrate setting via QSerialPort, I probably can clarify whether my conversion from unsign char* to QByteArray is successful or not.

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Fail To Write byte to device via QSerialPort

    qint32 is a signed 32-bit integer value whose possible range of values is +/- 2,147,483,647. One bit is used for the sign and the remaining 31 bits are used for the value. You may commonly see this also referred to as 2^31.

    You may also use QByteArray::append to append your unsigned char values to the QByteArray.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Fail To Write byte to device via QSerialPort

    Quote Originally Posted by leisha View Post
    I want to set QSerialPort::setBaudRate as :serial->setBauRate(5000000) but it has compiler error.
    And you didn't feel to include the error because you don't want help on that issue?

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 1st April 2014, 09:48
  2. How to write the BYTE* data to a QFile?
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 23rd November 2011, 05:33
  3. Replies: 2
    Last Post: 2nd November 2010, 06:15
  4. Replies: 3
    Last Post: 19th April 2010, 15:16
  5. Replies: 1
    Last Post: 16th June 2009, 10: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.