截止2004年12月16日 |
本站源码总量(商业源码除外)RAR压缩为 4,206,733 KB。其中免费源码为 1,124,495 KB,会员源码为 3,082,238 KB!
C/C++ |
129,555 KB |
Delphi |
1,258,381 KB |
Java |
120,937 KB |
.Net |
36,886 KB |
PowerBuilder |
954,525 KB |
Visual Basic |
923,454 KB |
ASP |
259,795 KB |
JSP |
4,987 KB |
其他 |
94,723 KB |
|
|
工控系统串口通讯设计 |
|
2)、读线程的处理模式:
2-1)、端口读的接口函数
long ReadData …,long size, BYTE* data)
{
COMTIMEOUTS to;
GetCommTimeouts hCom,&to); /*取超时参数*/
ThreadParam tp_read; /*主线程定义的读线程传入参数*/
tp_read.bActive = TRUE; /*主线程当前活动*/
tp_read.hCom = hCom; /*已打开的端口句柄*/
tp_read.evTerm = CreateEvent NULL,TRUE,FALSE,NULL); /*创建异步事件*/
tp_read.cs = cs; /*已经创建的临界区*/
tp_read.data = data; /*缓冲区指针*/
tp_read.dwQueryBytes = size;
tp_read.dwResultBytes = 0;
/*计算读的侦听超时数*/
tp_read.dwTimeouts = to.ReadTotalTimeoutConstant + to.ReadTotalTimeoutMultiplier * size;
tp_read.nResultCode = 0; /*初始状态码*/
/*创建读线程*/
CreateThread NULL,0, LPTHREAD_START_ROUTINE)ReadProc, void*)&tp_read,0,&dw);
/*等待线程结束*/
WaitForSingleObject tp_read.evTerm,INFINITE);
CloseHandle tp_read.evTerm);
…
/*返回已经处理的字节数*/
return tp_read.dwResultBytes;
}
2-2)、端口读的线程回调函数
DWORD WINAPI ReadProc LPVOID lpParam)
{
ThreadParam* pRead = ThreadParam*)lpParam;
OVERLAPPED ov;
memset void*)&ov,0,sizeof OVERLAPPED));
ov.hEvent = CreateEvent NULL,TRUE,FALSE,NULL);
/*端口异步读*/
EnterCriticalSection & pRead->cs));
ReadFile hCom, void*)pRead->data,pRead->dwQueryBytes,& pRead->dwResultBytes),&ov);
LeaveCriticalSection & pRead->cs));
/*等待异步读结果*/
WaitForSingleObject ov.hEvent,pRead->dwTimeouts)
/*取重叠操作结果*/
GetOverlappedResult pRead->hCom,&ov,&pRead->dwResultBytes,FALSE);
CloseHandle ov.hEvent);
/*通知主线程读操作完成*/
SetEvent pRead->evRead);
ExitThread 0);
return 0;
}
|
3)、写线程的处理模式:
3-1)、端口写的接口函数
long WriteData …,long size, BYTE* data)
{
COMTIMEOUTS to;
GetCommTimeouts hCom,&to); /*取超时参数*/
ThreadParam tp_write; /*主线程定义的写线程传入参数*/
tp_write.bActive = TRUE; /*主线程当前活动*/
tp_write.hCom = hCom; /*已打开的端口句柄*/
tp_write.evTerm = CreateEvent NULL,TRUE,FALSE,NULL); /*创建异步事件*/
tp_write.cs = cs; /*已经创建的临界区*/
tp_write.data = data; /*缓冲区指针*/
tp_write.dwQueryBytes = size;
tp_write.dwResultBytes = 0;
/*计算写的侦听超时数*/
tp_write.dwTimeouts = to.WriteTotalTimeoutConstant + to.WriteTotalTimeoutMultiplier * size;
tp_write.nResultCode = 0; /*初始状态码*/
/*创建写线程*/
CreateThread NULL,0, LPTHREAD_START_ROUTINE)WriteProc, void*)&tp_write,0,&dw);
/*等待线程结束*/
WaitForSingleObject tp_write.evTerm,INFINITE);
CloseHandle tp_write.evTerm);
…
/*返回已经处理的字节数*/
return tp_write.dwResultBytes;
}
3-2)、端口写的线程回调函数
DWORD WINAPI WriteProc LPVOID lpParam)
{
ThreadParam* pWrite = ThreadParam*)lpParam;
OVERLAPPED ov;
memset void*)&ov,0,sizeof OVERLAPPED));
ov.hEvent = CreateEvent NULL,TRUE,FALSE,NULL);
/*端口异步写*/
EnterCriticalSection & pWrite->cs));
WriteFile hCom, void*)pWrite->data,pWrite->dwQueryBytes,& pWrite->dwResultBytes),&ov);
LeaveCriticalSection & pWrite->cs));
/*等待异步写结果*/
WaitForSingleObject ov.hEvent,pWrite->dwTimeouts)
/*取重叠操作结果*/
GetOverlappedResult pWrite->hCom,&ov,&pWrite->dwResultBytes,FALSE);
CloseHandle ov.hEvent);
/*通知主线程写操作完成*/
SetEvent pWrite->evWrite);
ExitThread 0);
return 0;
}
|
4、EasyComm串口控件简介:EasyComm控件是适用于WIN32环境下的串口异步侦听、读写控件,采用前述的思路设计。主要特点在于为提高后台侦听线程的处理效率,在侦听线程中捕获的事件都被异步发送到主线程的窗口消息循环中,然后由主线程的窗口消息处理例程将串口事件消息通知给客户进程,客户进程根据事件类型决定下一步的动作。
EasyComm的开发库包括:EasyComm.h EasyComm.lib EasyComm.dll
1)、EasyComm库初始化和终结:
BOOL InitEasyComm HINSTANCE hIns);
/*说明:初始化EasyComm运行库*/
/*hIns:调用进程的句柄*/
/*返回:零值失败,非零值成功*/
/*举例:可以在进程初始化时调用*/
BOOL CSomeApp::InitInstance )
{
…
if !InitEasyComm this->m_hInstance))
return FALSE;
…
}
void UnInitEasyComm HINSTANCE hIns);
/*说明:终止EasyComm运行库*/
/*hIns:调用进程的句柄*/
/*举例:可以在进程退出时调用*/
BOOL CSomeApp::ExitInstance )
{
…
UnInitEasyComm this->m_hInstance);
…
}
2)、EasyComm控件窗体的创建和销毁:
HWND CreateWindow LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HANDLE hInstance, LPVOID lpParam );
/*说明:采用WIN32函数创建EasyComm控件窗体*/
/*lpClassName: 窗体类名 “EasyCommCtrl”*/
/*lpWindowName: 窗体名*/
/*dwStyle: 窗体样式*/
/*x: X位置*/
/*y: Y位置*/
/* nWidth: 宽度*/
/* nHeight: 高度*/
/*hWndParent: 父窗体句柄*/
/*hMenu: 忽略*/
/*hInstance: 忽略*/
/*lpParam: 忽略*/
/*举例: 可以选择在客户窗体创建后创建EasyComm控件*/
int CCustomerWnd::OnCreate LPCREATESTRUCT lpCreateStruct)
{
if CWnd::OnCreate lpCreateStruct) == -1)
return -1;
m_hCom=CreateWindow “EasyCommCtrl”,NULL,WS_CHILD,0,0,20,20,m_hWnd,NULL,NULL,NULL);
if ::IsWindow m_hCom))
{
::SetWindowLong hCom,GWL_ID,IDC_COM);
return 0;
}else
return -1;
}
|
|
|
文章出处: |
|
发表时间:2004-11-20 12:14:47 |
|