📄 windows ce_com.txt
字号:
Windows CE下串行通信的实现,接受GPS数据例子
GPS数据接收程序代码如下:
. //初始化
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{ HWND hWnd;
HANDLE hThread;
unsigned long rc;
g_hSendEvent=CreateEvent(NULL,FALSE,FALSE,NULL);//创建事件
hThread=CreateThread(NULL,0,SendThread,hWnd,0,&rc);//创建写线程
if(hThread)
CloseHandle(hThread);
else
{ DestroyWindow(hWnd);
return 0;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//写线程
unsigned long SendThread(PVOID pArg)
{
HWND hWnd,hwndSText;
unsigned long cBytes,nGoCode;
TCHAR szText[TEXTSIZE];
hWnd=(HWND)pArg;
hwndSText=GetDlgItem(hWnd,IDE_SEND);//获得发送文本框的句柄
while(1)
{
nGoCode=WaitForSingleObject(g_hSendEvent,INFINITE);//等待事件有信号
if(nGoCode==WAIT_OBJECT_0)
{
GetWindowText(hwndSText,szText,dim(szText));//得到文本框内的文本
BYTE buffer[10]={0x2};
WriteFile(hComPort,buffer,10,&cBytes,0);//把文本发送到串口
SetWindowText(hwndSText,TEXT(""));//清空发送文本框
}
else
break;
}
return 0;
}
//读线程
unsigned long ReadThread(PVOID pArg)
{ BYTE szText[170],*pPtr;
pPtr=szText;
TCHAR str[220]={0};
TCHAR temp[10];
hWnd=(HWND)pArg;
while(hComPort!=INVALID_HANDLE_VALUE)
{ ReadFile(hComPort,szText,170,&cBytes,0);//从串口读回数据
……………
//数据处理
……………
}
}
//“打开串口”按钮处理消息函数
LPARAM DoMainCommandOpen(HWND hWnd,WORD idItem,HWND hwndCtl,WORD wNotifyCode)
{ DCB dcb;//定义DCB结构变量
HWND hBtn;
COMMTIMEOUTS cto;//定义结构COMMTIMEOUTS变量
HANDLE hLocal;
DWORD dwTSat;
hBtn=GetDlgItem(hWnd,IDB_OPEN);
if(hComPort==INVALID_HANDLE_VALUE)
{hComPort=CreateFile(TEXT("COM1:"),GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);//打开串口
if(hComPort!=INVALID_HANDLE_VALUE)
{ GetCommState(hComPort,&dcb);//获取当前串口配置
dcb.BaudRate =nSpeed;
dcb.fParity =FALSE;
dcb.fNull=FALSE;
dcb.StopBits=ONESTOPBIT;
dcb.Parity=NOPARITY;
dcb.ByteSize=8;
SetCommState(hComPort,&dcb);//设置串口配置
cto.ReadIntervalTimeout=0;
cto.ReadTotalTimeoutMultiplier=0;
cto.ReadTotalTimeoutConstant=0;
cto.WriteTotalTimeoutConstant=0;
cto.WriteTotalTimeoutMultiplier=0;
SetCommTimeouts(hComPort,&cto);//设置串口超时
if(!GetExitCodeThread(hReadThread,&dwTSat)||(dwTSat!=STILL_ACTIVE))
{ //创建读线程
hReadThread=CreateThread(NULL,0,ReadThread,hWnd,0,&dwTSat);
if(hReadThread)
CloseHandle(hReadThread);
}
SetWindowText(hBtn,TEXT("Close"));
}
}
else
{ CloseHandle(hComPort);
SetWindowText(hBtn,TEXT("Open"));
hComPort=INVALID_HANDLE_VALUE;
}
return 0;
}
//“发送”按钮处理消息函数
LPARAM DoMainCommandSend(HWND hWnd,WORD idItem,HWND hwndCtl,WORD wNotifyCode)
{
SetEvent(g_hSendEvent);//设置事件为有信号状态
SetFocus(GetDlgItem(hWnd,IDE_SEND));
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -