Re: Reading Serial Port
On Jan 31, 1:24 pm, clinisbut <clinis...@gmail.com> wrote:
On Jan 31, 11:50 am, clinisbut <clinis...@gmail.com> wrote:
I'm desperate...
Maybe this will help: I've made a simple java serial reader and I got
the same problem...
But we have another app that reads the entire bytes coming from serial=
port (we are sure of this) and there is no problem with this device!!!=
!
Well, I've successed to read correctly the serial port through a new
java application I've writed to test. The differences between this one
that works and the last that does not works is in the driver used. The
one good uses the RXTX library (http://www.rxtx.org/) and the bad one
the official from Sun. I know this is not a java group, just say this
to demonstrate that the device is working ok. There is something that
I'm missing!
Could it be, that the firsts 255 bytes I read are from a internal
buffer and I get that impression that i'm lossing data where the fact
is I start to read from the real serial port at the middle of a
frame=BF?
I've noted that the firsts bytes I'm receiving are always bytes from
frames that get cutted in old "sessions" of my app.
I mean:
*Open my app and start receiving data.
*After sometime, I stop the read process at the middle of a frame so I
get only some bytes of it ( 3 for instance ).
*I close the app.
*Open again and start to receive data.
*the firsts bytes I got are the rest of the last frame received the
last time I opened the app.
I'm using PurgeComm to clear the serial, but seems that has no effect.
Well... finally I think I solved. I've added to the open port routine
this:
fSucces = PurgeComm( hComm, PURGE_RXABORT|PURGE_RXCLEAR );
if( !fSucces )
{
TRACE("Fail purging port.\n");
return FALSE;
}
And in the function that sets the shutdown event:
void MyThread::Stop()
{
SetEvent( ShutdownEvent );
BOOL ok = PurgeComm( hComm, PURGE_RXABORT|PURGE_RXCLEAR );
if( ok==0 )
{
TRACE("No purge:%d",GetLastError() );
}
else
{
DWORD bytesRead;
unsigned char buffer[124];
//trying to read bytes left.
do
{
ReadFile( hComm, &buffer, 124, &bytesRead, &ovReader );
}while( bytesRead>0 );
}
}
Maybe I've added something else, but sincerely I don't remember. I
touched so things...
The one still disturbs me are the first bytes I receive when open the
port, I suspect that comes from lasts sessions... but if I'm purguing
them why are still there?