I am new to QT programming. Serial communication application is being developed. For this Qextserialport classes are successfully conformed with QT classes and is being utilised for developing application. writing to port and Reading from port are incorporated in same application. Am able to write all the bytes in a msgpckt to port but reading from that port is creating the problem. It can only read one byte when read function of qextserialport is called. First byte is read properly rest its showing junk values.
How to debug it??? Is the method am following is correct??? here is the piece of code
Writing to port (COM1) : here is the code
///////////////////////////////////
openflag = port ->isOpen();
if(openflag)
{
int i = port->write((const char *) &Hmi_statusMsg, sizeof(Hmi_statusMsg));
qDebug("Transmitted bytes =%d",i);
}
else
{
qDebug("COM1 port is not open");
}
openflag = port ->isOpen();
if(openflag)
{
int i = port->write((const char *) &Hmi_statusMsg, sizeof(Hmi_statusMsg));
qDebug("Transmitted bytes =%d",i);
}
else
{
qDebug("COM1 port is not open");
}
To copy to clipboard, switch view to plain text mode
//////////////////////////////////////End code ///
Hmi_statusMsg in above code is Msg packet, each feilds are of type unsigned char, having data : MsgHdr = 0x75
Data1 = 0x08
Data2 = 0x02
Data3 = 0x40
Data4 = 0x10
chksum = 0xcd
Reading from port and displaying it on text-edit box: here is the code
char buff [1024];
int numBytes;
numBytes = port->bytesAvailable();
if(numBytes > 0)
{
int i = port->read(buff, numBytes);
buff[i] = '\0';
received_msg->append(msg);
received_msg->ensureCursorVisible();
qDebug("bytes available: %d", numBytes);
qDebug("received: %d", i);
ui->textEdit->setText(msg);
}
else
{
qDebug("bytes not available: %d", numBytes);
}
char buff [1024];
int numBytes;
numBytes = port->bytesAvailable();
if(numBytes > 0)
{
int i = port->read(buff, numBytes);
buff[i] = '\0';
QString msg = (QString) buff;
received_msg->append(msg);
received_msg->ensureCursorVisible();
qDebug("bytes available: %d", numBytes);
qDebug("received: %d", i);
ui->textEdit->setText(msg);
}
else
{
qDebug("bytes not available: %d", numBytes);
}
To copy to clipboard, switch view to plain text mode
/////////////////////////////////////////////////////
Transmitter output::::
Status Msg header = 75
Status Msg Data1 = 8
Status Msg Data2 = 2
Status Msg Data3 = 40
Status Msg Data4 = 10
Status Msg chksum = cd
Transmitted bytes =6
Receiver Output::::
bytes available: 6
received: 6
RCVD Status Msg header = u
RCVD Status Msg Data1 =
RCVD Status Msg Data2 =
RCVD Status Msg Data3 = @
RCVD Status Msg Data4 =
RCVD Status Msg chksum = Ã
First byte is displayed in ASCII, but remaining all are junk values...
Expecting guidance,
Thanks in advance
Bookmarks