PDA

View Full Version : Fail To Write byte to device via QSerialPort



leisha
15th April 2015, 16:57
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


void MainWindow::writeData(const QByteArray &data)
{
serial->write("*IDN?\n"); //Get the product name and firmware version
qDebug()<<(serial->bytesToWrite() )<<"- Can Read? "<<(serial->canReadLine())
<<"|| num "<<(serial->readBufferSize() )<<"bytesAvailable?"<<(serial->bytesAvailable())
<<"|| Sequential?"<<(serial->isSequential())
<<"END?"<<(serial->atEnd() );
}

void MainWindow::readData()
{ qDebug()<<(serial->bytesToWrite() )<<"- Can Read? "<<(serial->canReadLine())
<<"|| num "<<(serial->readBufferSize() )<<"bytesAvailable?"<<(serial->bytesAvailable())
<<"|| Sequential?"<<(serial->isSequential())
<<"END?"<<(serial->atEnd() );//<<endl;
QByteArray data = serial->readAll();

console->putData(data);
qDebug()<<"END2?"<<(serial->atEnd() )<<endl;
}


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:


void MainWindow::writeDataI(const QByteArray &data)
{
const char startI[5]={0xA2,0xD0,0x00,0x00,0x55}; //Get the default setting.
serial->write(startI,5);
qDebug()<<(serial->bytesToWrite() )<<"- Can Read? "<<(serial->canReadLine())
<<"|| num "<<(serial->readBufferSize() )<<"bytesAvailable?"<<(serial->bytesAvailable())
<<"|| Sequential?"<<(serial->isSequential())
<<"END?"<<(serial->atEnd() );

}

The result in debug (never get into the readData)

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

leisha
16th April 2015, 05:42
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?

Lesiok
16th April 2015, 08:05
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 ?

leisha
17th April 2015, 04:58
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.

jefftee
17th April 2015, 05:39
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.

anda_skoa
17th April 2015, 08:09
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,
_