Pregunta sobre puerto serie en C++
Wenas!
Estoy realizando una conexion entre un PocketPc y un sistema externo via bluetooth. El Pocket lee del puerto serie, el problema que tengo es que cuando el sistema externo se apaga, mi programa se cuelga. El codigo basico del programa es este:
HANDLE SPort;
DCB dcb;
COMMTIMEOUTS lpTo;
int main(int argc, char* argv[])
{
SPort= CreateFileW (L"COM8:",GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
printf("Opening Portn");
//printf ("%dn",SPort);
if (SPort == INVALID_HANDLE_VALUE)
{
printf("Error at creating filen");
return 0;
}
if(! GetCommState (SPort,&dcb))
{
printf("Error at configuringn");
return 0;
}
else
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
if (!SetCommState(SPort,&dcb))
{
printf("Error at entering new configurationn");
return 0;
}
GetCommTimeouts(SPort,&lpTo);
lpTo.ReadIntervalTimeout = 0;
lpTo.ReadTotalTimeoutMultiplier = 10;
lpTo.ReadTotalTimeoutConstant = 10;
lpTo.WriteTotalTimeoutMultiplier = 10;
lpTo.WriteTotalTimeoutConstant = 100;
SetCommTimeouts(SPort,&lpTo);
SetupComm(SPort,2048,2048);
BYTE Out;
DWORD nbytes;
unsigned long EventStatus;
int sReadSP ;
DWORD SPFlags;
DWORD WCE;
int aux1,aux2,result,aux;
SPFlags = EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING |EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY ;
SetCommMask (SPort,SPFlags);
printf("Waiting for eventn");
while (SPort != INVALID_HANDLE_VALUE)
{
WCE = WaitCommEvent (SPort,&EventStatus,0);
SetCommMask (SPort,SPFlags);
if ( EventStatus == EV_RXCHAR)
{
printf("Dentro del if del EV_CHARn");
do
{
sReadSP=ReadFile (SPort,&Out,1,&nbytes,0);
if (sReadSP==0)
{
printf ("Error reading the port.n");
return 0;
}
if (nbytes == 1)
{
/*interpreto codigo*/
}
}
while (nbytes == 1);
}
}
CloseHandle(SPort);
return 0;
}
No se si existe alguna funcion que permita conocer si la conexion se ha roto o algo asi.
Si alguien me puede echar una mano...
Merci
Estoy realizando una conexion entre un PocketPc y un sistema externo via bluetooth. El Pocket lee del puerto serie, el problema que tengo es que cuando el sistema externo se apaga, mi programa se cuelga. El codigo basico del programa es este:
HANDLE SPort;
DCB dcb;
COMMTIMEOUTS lpTo;
int main(int argc, char* argv[])
{
SPort= CreateFileW (L"COM8:",GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
printf("Opening Portn");
//printf ("%dn",SPort);
if (SPort == INVALID_HANDLE_VALUE)
{
printf("Error at creating filen");
return 0;
}
if(! GetCommState (SPort,&dcb))
{
printf("Error at configuringn");
return 0;
}
else
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
if (!SetCommState(SPort,&dcb))
{
printf("Error at entering new configurationn");
return 0;
}
GetCommTimeouts(SPort,&lpTo);
lpTo.ReadIntervalTimeout = 0;
lpTo.ReadTotalTimeoutMultiplier = 10;
lpTo.ReadTotalTimeoutConstant = 10;
lpTo.WriteTotalTimeoutMultiplier = 10;
lpTo.WriteTotalTimeoutConstant = 100;
SetCommTimeouts(SPort,&lpTo);
SetupComm(SPort,2048,2048);
BYTE Out;
DWORD nbytes;
unsigned long EventStatus;
int sReadSP ;
DWORD SPFlags;
DWORD WCE;
int aux1,aux2,result,aux;
SPFlags = EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_RING |EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY ;
SetCommMask (SPort,SPFlags);
printf("Waiting for eventn");
while (SPort != INVALID_HANDLE_VALUE)
{
WCE = WaitCommEvent (SPort,&EventStatus,0);
SetCommMask (SPort,SPFlags);
if ( EventStatus == EV_RXCHAR)
{
printf("Dentro del if del EV_CHARn");
do
{
sReadSP=ReadFile (SPort,&Out,1,&nbytes,0);
if (sReadSP==0)
{
printf ("Error reading the port.n");
return 0;
}
if (nbytes == 1)
{
/*interpreto codigo*/
}
}
while (nbytes == 1);
}
}
CloseHandle(SPort);
return 0;
}
No se si existe alguna funcion que permita conocer si la conexion se ha roto o algo asi.
Si alguien me puede echar una mano...
Merci
La función WaitCommEvent permite controlar todos los eventos que se producen en el puerto, incluso el de cierre de la conexión.
Mira la ayuda de esta fución o que te comento ahora es de memoria ya que hace mucho tiempo cree una dll y ahora es lo que utilizo. Si no recuerdo mal la función SetCommMask, te permite configurar los eventos que desear recibir del puerto y la función WaitCommEvent te permite capturar estos eventos.
Espero que mi ayuda te sea de utilidad.
Mira la ayuda de esta fución o que te comento ahora es de memoria ya que hace mucho tiempo cree una dll y ahora es lo que utilizo. Si no recuerdo mal la función SetCommMask, te permite configurar los eventos que desear recibir del puerto y la función WaitCommEvent te permite capturar estos eventos.
Espero que mi ayuda te sea de utilidad.
