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.
}
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.
}
To copy to clipboard, switch view to plain text mode
PS: excuse for my english...
Bookmarks