PDA

View Full Version : why bytesAvailable() returns '0' when there are answer data from serial port monitor



to_guliang
3rd June 2008, 08:23
hello, everyone. i use qextserialport in my application to realize serial communication.
i can write data using writeData(const char * data, qint64 maxSize). But the function bytesAvailable () returns '0' while reading, so i can't get any data through readData(char * data, qint64 maxSize). But i can see the answer data from serial port monitor. WHY??
my code:

if(RS232->open(QIODevice::ReadWrite))
{
RS232->writeData(Data,13);
//usleep(500);

int i= RS232->QIODevice::bytesAvailable();
if(i > 0)
{
char *pStr=NULL;
RS232->readData(pStr,25);
}
RS232->close();
}
adding the "usleep(500);" has no use.

thanks very much for any advice!

wysota
3rd June 2008, 10:32
As far as I remember bytesAvailable() doesn't work with QExtSerialPort.

zeratul
3rd June 2008, 15:09
1.- Wait for X ms, depending of the data length. The serial port isn't so fast...
2.- Don't use the serial monitor, because it can take the data from the buffer, cleaning it, and no leaving data in it.

For example:

bool threadDatos::RecibirTrama(char *trama, int num_bytes, datos *Datos)
{
msleep(12); // No queda de otra...esta cosa no tiene timeout ¬¬
int i = PuertoSerial->bytesAvailable(); // Leer cuantos bytes hay
if(i == num_bytes) // Si es igual al número de bytes requeridos
{
Datos->pBuffer->acquire(); // Pedir recurso en el buffer
PuertoSerial->read(trama, num_bytes); // Leer el buffer de recepción
Datos->cBuffer->release(); // Liberar recurso de dato valido
mutex->unlock(); // Liberar puerto serial
return TRUE; // Regresar con TRUE
}
else if(i == num_bytes + 5) // Si hay mas, debido a un NCK de SETP
{
Datos->pBuffer->acquire(); // Pedir recurso en el buffer
PuertoSerial->read(trama, 5); // Leer el buffer de recepción
PuertoSerial->read(trama, num_bytes); // Leer el buffer de recepción
Datos->cBuffer->release(); // Liberar recurso de dato valido
mutex->unlock(); // Liberar puerto serial
return TRUE; // Regresar con TRUE
}
mutex->unlock(); // Liberar puerto serial
return FALSE; // Error: devolver FALSE.
}

PS: excuse for my english...:o

to_guliang
4th June 2008, 01:09
thanks, i will try as you say.

to_guliang
4th June 2008, 01:32
As far as I remember bytesAvailable() doesn't work with QExtSerialPort.

supposed that bytesAvailable() doesn't work ,i amend my code as:


RS232->writeData(Data,13); //write data
usleep(120000); //wait

int i= RS232->QIODevice::bytesAvailable();//if there are bytes availble to read

QString str;
str.setNum(i);
Com->setText(str);//show the bytes availble to read on the 'Com' Button

char *pStr=NULL;
RS232->read(pStr,13); //read data from serial port to pStr
Com1->setText(pStr); //show the data on 'Com1' Button
RS232->close();
i still can't get any data! why?

to zeratul: i don't use the serial monitor.

wysota
4th June 2008, 07:20
I just checked - bytesAvailable() works fine with the serial port class. If it doesn't work for you, there has to be some other reason for that. What did open() return and what did you pass to it?

to_guliang
4th June 2008, 08:15
I just checked - bytesAvailable() works fine with the serial port class. If it doesn't work for you, there has to be some other reason for that. What did open() return and what did you pass to it?
thanks for your reply:)
i have develop a application that could write and read program to the MELSEC FX-3U PLC.
open() return true. i write command that read program from PLC to the serial port.
i can observe the answer data from PLC through serial port, that means i have pass my data succeedly to the PLC and the PLC has answer me.
but the bytesAvailable() always returns '0' and i can't get any data by using readData(char * data, qint64 maxSize).
i don't know why:confused:

sorry, my expression is poor,Could you understand me? if don't, please tell me, i will try my best to express myself:)

wysota
4th June 2008, 09:30
How exactly do you "observe" the data passing through the port? If you use another application for that, it could be that it steals the data, as it was already suggested.

to_guliang
4th June 2008, 10:48
How exactly do you "observe" the data passing through the port? If you use another application for that, it could be that it steals the data, as it was already suggested.

i make sure i have write the data succeedly. the PLC can run with my program that i write to it. According to the protocol of the MELSEC-FX PLC, the PLC will answer me some information.
i can't get any data even if i close the serial port monitor as i have state it after i try as the suggestion.:confused:

to_guliang
4th June 2008, 12:29
the problem has been solved. QIODevice::bytesAvailable()
i can read data the PLC answer, and the bytesAvailable() returns the right value.
i should use "RS232->bytesAvailable();", not "RS232->QIODevice::bytesAvailable();":)