|
Win32环境下COM口通信
DCB dcb; //
COMMTIMEOUTS CommTimeouts1;
DWORD dwEvtMask;
OVERLAPPED hOverlapped;
HANDLE hCOM;
hCOM = CreateFile("COM1"
GENERIC_READ | GENERIC_WRITE,
0, //comm devices must be opened w/exclusive-access
NULL, //no security attrs
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, //not overlapped I/O
NULL //hTemplate must be NULL for comm devices
);
if (GlobalDataForm->hGPSCom == INVALID_HANDLE_VALUE)
{
sprintf(Str,"打开GPS_COM错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
if (!GetCommState(GlobalDataForm->hGPSCom, &dcb))
{
sprintf(Str,"获取GPS_COM状态错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
dcb.BaudRate = 9600; //波特率9600
dcb.ByteSize = 8; //8个数据位
dcb.Parity = NOPARITY; //没有校验
dcb.StopBits = ONESTOPBIT; //1个停止位
if (!SetCommState(GlobalDataForm->hGPSCom, &dcb))
{
sprintf(Str,"设置GPS_COM状态错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
if (!GetCommTimeouts(GlobalDataForm->hGPSCom,&CommTimeouts1))
{
sprintf(Str,"获取GPS_COM超时状态错误,错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
//CommTimeouts1.ReadIntervalTimeout=0xffffffff;
CommTimeouts1.ReadTotalTimeoutConstant=100;
if (!SetCommTimeouts(GlobalDataForm->hGPSCom,&CommTimeouts1))
{
sprintf(Str,"设置GPS_COM超时状态错误,错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
/*if (!SetCommMask(GlobalDataForm->hGPSCom,EV_DSR))
{
sprintf(Str,"设置GPS_COM屏蔽状态错误,错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
hOverlapped.hEvent=CreateEvent(NULL,
FALSE,
FALSE,
NULL
);
assert(hOverlapped.hEvent);
if (WaitCommEvent(GlobalDataForm->hGPSCom,&dwEvtMask,&hOverlapped))
//{
// sprintf(Str,"等待GPS_COM通信事件错误,错误代号%d",GetLastError());
// MessageBox(NULL,Str,"错误",MB_OK);
//}
//else
{
if (dwEvtMask & EV_DSR)
{
MessageBox(NULL,"GPS数据准备好","提示",MB_OK);
}
}
}*/
mrAnswer=IDOK;
GlobalDataForm->mGPSCom=true;
}
}
}
}
}
break;
/* DCB dcb;
COMMTIMEOUTS CommTimeouts1;
DWORD dwEvtMask;
OVERLAPPED hOverlapped;
GlobalDataForm->hGPSCom = CreateFile(GlobalDataForm->GPSComName,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attrs
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (GlobalDataForm->hGPSCom == INVALID_HANDLE_VALUE)
{
sprintf(Str,"打开GPS_COM错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
if (!GetCommState(GlobalDataForm->hGPSCom, &dcb))
{
sprintf(Str,"获取GPS_COM状态错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
dcb.BaudRate = 9600; //波特率9600
dcb.ByteSize = 8; //8个数据位
dcb.Parity = NOPARITY; //没有校验
dcb.StopBits = ONESTOPBIT; //1个停止位
if (!SetCommState(GlobalDataForm->hGPSCom, &dcb))
{
sprintf(Str,"设置GPS_COM状态错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
if (!GetCommTimeouts(GlobalDataForm->hGPSCom,&CommTimeouts1))
{
sprintf(Str,"获取超时状态错误,错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
//CommTimeouts1.ReadIntervalTimeout=0xffffffff;
CommTimeouts1.ReadTotalTimeoutConstant=100;
if (!SetCommTimeouts(GlobalDataForm->hGPSCom,&CommTimeouts1))
{
sprintf(Str,"设置超时状态错误,错误代号%d",GetLastError());
MessageBox(NULL,Str,"错误",MB_OK);
}
else
{
mrAnswer=IDOK;
GlobalDataForm->mGPSCom=true;
}
}
}
}
}
break;*/
}
case mOFF:{
GlobalDataForm->mGPSCom=false;
CloseHandle(GlobalDataForm->hGPSCom);
mrAnswer=IDOK;
break;
}
|
|