以前看了很多文档,知道Windows CE的串口如何操作,但是由于从来没有想过要用串口,也没有实际的作过。昨天有人需要用我们的开发板,而他们主要就是要用串口,我就写了一个程序,用来测试串口是否工作。记录下来,作为以后串口通信程序的起点。 HANDLE hSer; hSer = CreateFile (TEXT ("COM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(hSer == INVALID_HANDLE_VALUE) { MessageBox(L"CreateFailed",L"Failed"); return; }
int RC; DWORD cByte_send,cByte_written; char ch[20]; cByte_send = sizeof(ch); sprintf(ch,"test COM1 good!"); RC=WriteFile(hSer,&ch,cByte_send,&cByte_written,NULL); WCHAR ret[10]; wsprintf(ret,L"%d",cByte_written); if(RC) { MessageBox(L"Send Ok!"); MessageBox(ret); CloseHandle(hSer); return; } wsprintf(ret,L"%d",GetLastError()); MessageBox(ret); CloseHandle(hSer); 这部分程序实际上跟对WInCE下大部分流驱动程序的操作方法一样,首先用CreateFile打开该设备,然后用Writefile/ReadFile进行读写,最后用CloseHandle来关闭。该程序没有改变串口的配置信息,如果要修改,应该定义各DCB(Dirver control Block),然后使用GetCommState/SetCommState来进行读取和修改串口信息。实际上这个函数是对串口驱动IoControl的封装,如果你愿意,你也完全可以直接使用Iocontrol来进行^_^ 
|