- CVI 정보나눔(www.cvi.kr)
- 스터디
- 질문답변
질문,답변
CVI에서
USB device의 connection과 disconnection을 자동으로 알 수 없을까요?
원하는건... USB 장치가 연결이 되면 특정 function(예를들면 어떤 디바이스 연결되었다는 메시지 출력)을 수행하려고 합니다.
연결이 끊어진 경우는 반대의 function 수행.
CVI에서 어떻게 하면 될까요?
구굴링 하다보니, winapi를 이용하는 예제가 있긴한데..
그것들은 윈도우 msg 처리하는 무한루프가 돌아야 해서... 적당치 않은것 같아요.
DLL 만들기에도 function 내에 cvi function이 또 들어가야 하니.. 복잡해지고..
고수님들 조언 부탁합니다.
참고로 winapi 소스는 아래와 같아요. visual c환경입니다.
전처리기 정의는 WIN32;_DEBUG;_CONSOLE;_MBCS;UNICODE;_UNICODE 입니다.
아래코드의 무한루프 없애는 방법을 알려주셔도 좋고..
아니면 아래코드 무시하고 다른 방법을 제시해 주셔도 감사합니다.
#include <windows.h>
#include <dbt.h>
#include <winuser.h>
//#include <strsafe.h>
//#pragma comment(lib, "user32.lib")
#include <process.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define USE_CDROM_GUID_ONLY
HWND hwnd;
// this doesn't work for CD/DVD media change events
//#define USE_MESSAGE_ONLY_WINDOW
//-----------------------------------------------------------------------------
int OnDeviceChange( UINT nEventType, DWORD dwData )
{
switch ( nEventType ) // <-- Never gets here.
{
case DBT_DEVICEARRIVAL:
printf("connected \r\n");
break;
case DBT_DEVICEREMOVECOMPLETE:
printf("disconnected \r\n");
break;
default:
break;
}
return TRUE;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
if (msg == WM_DEVICECHANGE)
{
cout << "WM_DEVICECHANGE, wParam = 0x"
<< setw(4) << hex << setfill('0') << int(wParam) << dec
<< endl;
OnDeviceChange( wParam, lParam );
}//if
else
cout << "Got msg " << msg << ", " << int(wParam)
<< ", " << int(lParam) << endl;
return 1;
}//WinProc
//-----------------------------------------------------------------------------
int main()
{
const TCHAR *className = L"DevNotifyTest";
WNDCLASS wincl = {0};
HANDLE hThread;
DWORD dwThreadID;
wincl.hInstance = GetModuleHandle(0);
wincl.lpszClassName = className;
wincl.lpfnWndProc = WinProc;
if (!RegisterClass(&wincl))
{
DWORD le = GetLastError();
cout << "RegisterClassA() failed, le = " << le << endl;
return 1;
}//if
HWND parent = 0;
#ifdef USE_MESSAGE_ONLY_WINDOW
parent = HWND_MESSAGE;
#endif
hwnd = CreateWindowEx(WS_EX_TOPMOST, className, className, 0, 0, 0, 0, 0, parent, 0, 0, 0);
if (!hwnd)
{
DWORD le = GetLastError();
cout << "CreateWindowExA() failed, le = " << le << endl;
return 1;
}//if
GUID cdromDevIntGuid =
{0x53F56308, 0xB6BF, 0x11D0,
{0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B}};
DEV_BROADCAST_DEVICEINTERFACE notifyFilter = {0};
notifyFilter.dbcc_size = sizeof(notifyFilter);
notifyFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notifyFilter.dbcc_classguid = cdromDevIntGuid;
HDEVNOTIFY hDevNotify =
RegisterDeviceNotification(hwnd, ¬ifyFilter,
#ifndef USE_CDROM_GUID_ONLY
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES |
#endif
DEVICE_NOTIFY_WINDOW_HANDLE);
if (!hDevNotify)
{
DWORD le = GetLastError();
cout << "RegisterDeviceNotificationA() failed, le = " << le << endl;
return 1;
}//if
MSG msg;
for (;;) // ctrl-c to exit ;)
{
BOOL bRet = GetMessage(&msg, hwnd, 0, 0);
if ((bRet == 0) || (bRet == -1))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}//while
return 0;
}//main
제가 GUID 관련하여 이용했던 함수는 UuidFromString() 이라는 함수입니다.
gustaf님의 목적과 얼마나 부합할지 모르겠지만, 대략 아래와 같이 사용하였습니다.
------------------------------------------------------
#include <windows.h>
#include <oleauto.h>
CLSID Guid_aaa;
......
UuidFromString("5009B528-A40B-4158-A5C3-20633C473278", &Guid_aaa);
......
------------------------------------------------------
GUID 가 나와서 적어본 것인데, 별로 관련없는 내용이었다면 사과드립니다.
꼭 원하시는 답을 찾기를 바랍니다.
감사합니다. 이것도 찾고 있던 function이였어요.
그 주변에서 보다보면 특정 device filtering하는 것도 가능하지 않을까요 ^^;
자답에 이은 또 질문입니다. ^^;
아래 처럼 했더니 됩니다.
CVI에서 컴파일 한겁니다. "usb_test3.h" 에는 winapi의 dbt.h를 copy 했습니다. 단순히 "DEVICEARRIVAL"를 정의 하기위해서만 했습니다.
또 질문은....이렇게 하니.. 내컴퓨터의 모든 장치관리자 상의 디바이스 변경을 검색합니다. ...
사실 저는 USB 디바이스 중 3가지만 검색하면 됩니다.. winapi에서는 GUID 등을 이용해서 filter같은걸 등록 하던데.. cvi에서도 할 수 있을까요?
------------------- 아래 -------------
#include <windows.h>
#include <ansi_c.h>
#include <userint.h>
#include "toolbox.h"
#include "MyUIR.h"
#include "usb_test3.h" // 기존 dbt.h
int CVICALLBACK MyCallback_WinProc (int panelHandle, int message, unsigned int* wParam, unsigned int* lParam, void* callbackData)
{
switch ( *wParam ) // <-- Never gets here.
{
case DBT_DEVICEARRIVAL:
printf("connected \r\n");
break;
case DBT_DEVICEREMOVECOMPLETE:
printf("disconnected \r\n");
break;
default:
break;
}
return TRUE;
}
int panelHandle;
int postHandle;
void main (void)
{
if ((panelHandle = LoadPanel (0, "MyUIR.uir", PANEL)) < 0)
return;
/* To intercept the WM_CLOSE message with MyCallback */
InstallWinMsgCallback (panelHandle, WM_DEVICECHANGE, MyCallback_WinProc, VAL_MODE_INTERCEPT, NULL, &postHandle);
DisplayPanel(panelHandle);
RunUserInterface(); /* will run until user presses the quit button */
/* Uninstall the event handler and free associated memory */
RemoveWinMsgCallback (panelHandle, WM_CLOSE);
}