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;
console->putData(data);
qDebug()<<"END2?"<<(serial->atEnd() )<<endl;
}
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;
}
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:
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() );
}
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() );
}
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
Bookmarks