Wince下面蓝牙串口的打开问题
我现在要开发一个小的测试程序,测试我们的蓝牙开发板和蓝牙打印机之间的通讯。我参考了PB4.2下面的蓝牙测试程序的例子,名称是printui.使用这个程序,可以在我们的蓝牙开发板上找到并和蓝牙打印机建立连接。然后我想通过蓝牙虚拟串口向打印机发送指令,但是这个虚拟串口确怎么也打不开。请高手帮忙看看。参考代码如下:
在printui程序中,通过如下代码建立和蓝牙打印机之间的连接:
g_pState->hDevice = RegisterDevice (L"COM", ndx, L"btd.dll", (DWORD)&pp);
if (! g_pState->hDevice)
{
wsprintf (szString, L"Failed to register COM port, Error = %d", GetLastError ());
MessageBox (NULL, L"Failed to register COM port",L"PrintUI Error", MB_OK | MB_TOPMOST);
}
if (! g_pState->hDevice)
return;
if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"Printers\\Ports", 0, KEY_ALL_ACCESS, &hk))
{
MessageBox (NULL, L"Bluetooth printer is not defined (no registry entry)!", L"PrintUI Error", MB_OK | MB_TOPMOST);
return;
}
使用RegisterDevice注册一个蓝牙串口,并赋予g_pState->hDevice。
我曾经试着直接使用这个handle向串口发送指令,但是失败,代码如下:
WCHAR SendStr[3];
SendStr[0] = 0x1B;
SendStr[1] = 0x40;
DWORD dwNumWrite;
if(!WriteFile(g_pState->hDevice, SendStr, 3, &dwNumWrite, NULL))
{
MessageBox (NULL, L"Printer isnot init!", L"OK", MB_OK | MB_TOPMOST);//在这里弹出失败信息
return 0;
}
else
{
MessageBox (NULL, L"Printer is init!", L"OK", MB_OK | MB_TOPMOST);
if(!WriteFile(g_pState->hDevice, L"Test Printer!\r\n", 19, &dwNumWrite, NULL))
{
MessageBox (NULL, L"Printer Test Error!", L"OK", MB_OK | MB_TOPMOST);
return 0;
}
else
MessageBox (NULL, L"Printer Test!", L"OK", MB_OK | MB_TOPMOST);
}
我觉得可能还需要进行打开串口的工作,而不能直接发送指令。
在PB的sample里面,还有蓝牙的虚拟串口程序,但是我使用它里面的代码,确提示串口打开失败。代码如下:
HANDLE h = RegisterDevice (L"COM", index, L"btd.dll", (DWORD)&pp);
wprintf (L"handle = 0x%08x\n", h);
if (h == NULL)
{
wprintf (L"GetLastError = %d\n", GetLastError ());
return 0;
}
WCHAR szComPort[30];
wsprintf (szComPort, L"COM%d:", index);
HANDLE hCommPort = CreateFile (szComPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hCommPort == INVALID_HANDLE_VALUE)
{
wprintf (L"Failed to open %s. Error = %d\n", szComPort, GetLastError ());//在这里弹出失败信息
DeregisterDevice (h);
return 0;
}
那位高手做过类似的程序,请帮忙解决。谢谢!