<< 点击显示目录 >> 主页 PVI通信 > PVI帮助信息 > PVI base system > PVICOM interface > User messages > Message function |
必须输入SET_PVIFUNCTION(二进制0xfffffffc)作为 用户信息的信息编号 。消息句柄和消息参数可以由PVICOM应用程序自由使用。然而,请记住,为消息句柄(PVI函数的参数 "hResMsg")输入零值会抑制响应(见 "请求和响应"部分)。 对于不需要的消息句柄,可以输入常数PVI_HMSG_NIL。
为了读取用户消息,PVICOM应用程序需要调用 PviGetNextResponse 或PviXGetNextResponse函数。之后,程序与 用户信息作为窗口信息 的程序相同 。
在PVICOM应用程序中,应在中央程序循环中循环调用函数PviGetNextResponse。这样就可以通过用户消息来控制整个程序过程。如果对PVI响应或事件数据的评估是在一个单独的线程中进行的,那么函数PviGetNextRespons可以很容易地集成在线程程序循环中。所有的异步PVICOM函数(异步和同步函数)和函数PviGetNextResponse(或PviXGetNextResponse)可以从不同的线程调用。
Visual C++的例子
#include <PviCom.h>
typedef bool (*T_RESPONSE_FUNC) (WPARAM, LPARAM, T_RESPONSE_INFO*, DWORD);
#define PVIOBJ_CNT 40
static DWORD PviObjLinkID[PVIOBJ_CNT];
static bool bExitProg;
static HANDLE hRespEvent;
void ExitProg ()
{
bExitProg = TRUE;
SetEvent (hRespEvent);
}
...
static void ObjectEvent (WPARAM wParam, LPARAM lParam, T_RESPONSE_INFO* pInfo, DWORD DataLen)
{
INT ErrCode;
switch (pInfo->nType)
{
case POBJ_EVENT_DATA:
// confirm object event and read data:
ErrCode = PviReadResponse (wParam, pDataBf, DataLen);
...
case POBJ_EVENT_ERROR:
...
case POBJ_EVENT_DATAFORM:
...
}
}
static void CreateResp (WPARAM wParam, LPARAM lParam, T_RESPONSE_INFO* pInfo, DWORD DataLen)
{
DWORD LinkID
INT ErrCode;
// confirm create response:
ÉrrCode = PviCreateResponse (wParam, &LinkID)
if (ErrCode != 0)
{
printf ("Object creation failed (Error=%d)\n", ErrCode);
LinkID = 0;
}
PviObjLinkID[lParam] = LinkID;
}
static void GlobalEvent (WPARAM wParam, LPARAM lParam, T_RESPONSE_INFO* pInfo, DWORD DataLen)
{
// confirm global event:
PviReadResponse (wParam, NULL, 0);
switch (pInfo->nType)
{
case POBJ_EVENT_PVI_CONNECT:
printf ("PVI connection established\n");
break;
case POBJ_EVENT_PVI_DISCONN:
printf ("PVI connection failed (Error=%d)\n", pInfo->ErrCode);
break;
case POBJ_EVENT_PVI_ARRANGE:
// create all required PVI objects:
ErrCode = PviCreateRequest ("@Pvi/LNINA2", POBJ_LINE, "CD=LnIna2",
ObjectEvent, SET_PVIFUNCTION, 0, "EV=e", CreateResp, SET_PVIFUNCTION, 0);
...
ErrCode = PviCreateRequest ("@Pvi/LNINA2/COM2/CPU/PVar1", POBJ_PVAR, "CD=PVar1",
ObjectEvent, SET_PVIFUNCTION, 24, "EV=edf", CreateResp, SET_PVIFUNCTION, 24);
...
break;
}
}
int main ()
{
INT ErrCode;
WPARAM wParam;
LPARAM lParam;
LPVOID hMsg;
DWORD DataLen;
T_RESPONSE_INFO Info;
T_RESPONSE_FUNC RespFunc;
bExitProg = FALSE;
hRespEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
if (hRespEvent == NULL)
{
printf ("Can't create system event\n");
return (1);
}
ErrCode = PviInitialize (0, 0, "", NULL);
if (ErrCode != 0)
{
printf ("Initialize failed (Error=%d)\n", ErrCode);
return (1);
}
// define global events:
ErrCode = PviSetGlobEventMsg (POBJ_EVENT_PVI_ARRANGE, GlobalEvent, SET_PVIFUNCTION, 0);
...
// loop to control all response/event data:
while (! bExitProg)
{
// get response/event data identification:
ErrCode = PviGetNextResponse (&wParam, &lParam, &hMsg, hRespEvent);
if (ErrCode != 0)
{
printf ("NextResponse failed (Error=%d)\n", ErrCode);
break; // -> exit loop
}
if (wParam == 0)
// wait for response/event data:
WaitForSingleObject (hRespEvent, INFINITE);
else
{
// read response/event data information:
ErrCode = PviGetResponseInfo (wParam, NULL, &DataLen, &Info, sizeof (Info));
if (ErrCode != 0)
{
if (ErrCode == 12055)
continue; // -> no response (skip error 12055)
printf ("GetResponseInfo failed (Error=%d)\n", ErrCode);
break; // -> exit loop
}
// call response/event function:
RespFunc = (T_RESPONSE_FUNC) hMsg;
RespFunc (wParam, lParam, &Info, DataLen);
}
}
PviDeinitialize ();
CloseHandle (hRespEvent);
return (0);
}