PDA

View Full Version : COM port reading with Qextserialport is creating problem:



pupqt
27th April 2011, 07:03
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");
}

//////////////////////////////////////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';
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);
}

/////////////////////////////////////////////////////




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 :)

squidge
27th April 2011, 07:43
What you are reading is what you sent, so what is the problem?

pupqt
27th April 2011, 08:54
Thanks for reply.

I know what am reading, but the method followed to read, was that correct??? because it cud able to read only one byte, rest all showing junk....

piece of code which i have posted can be referred and correct me where I went wrong......

ChrisW67
27th April 2011, 09:27
Your code looks how many bytes are available (could be 1, could be 5000), reads that number of bytes, assumes that is some sort of valid string, and appends it to the text edit. Since the data you are sending is not printable characters this will produce rubbish in the text edit. The odd characters you show seem consistent with treating the data you say was sent as ASCII characters, which was squidge's point.

If you want to display binary data in a readable form you will have to convert it somehow. Hexadecimal is often chosen for this purpose.

If you need all 6 bytes to be present before you do something with the packet then you need to allow for all 6 bytes to arrive one at at time, buffer them, and do something when you have a full set. You would need to do this to check your checksum.