WinCE 蓝牙虚拟 com 端口 RegisterDevice 总是导致错误 110 或 2404
WinCE bluetooth virtual com port RegisterDevice always result in Error 110 or 2404
我正在尝试让蓝牙打印机在 WinCE 手持设备上工作。
当我通过 execfile('.../bt_ce.py')
在 PythonCE 中 运行 它时,它没有提供任何反馈,就像我没有输入任何内容一样。没有句柄或索引被打印回来。再次尝试后,总是报错110
或2102
。
我是 C++ 和 WinCE API 的新手。我看不出问题出在哪里。
我的设备是 WinCE 5.0 条码扫描仪。我的代码如下改编自https://msdn.microsoft.com/en-us/library/ms881004.aspx
# -*- coding: utf-8 -*-
import ctypes
from ctypes import POINTER, Structure, c_ulonglong, c_int, pointer, c_ulong, c_wchar
from comtypes import GUID
from ctypes.wintypes import DWORD,WORD,BYTE,UINT, HANDLE, WCHAR, ULONG
INT = c_int
ULONGLONG = c_ulonglong
# typedef ULONGLONG bt_addr, *pbt_addr, BT_ADDR, *PBT_ADDR;
bt_addr = ULONGLONG
BT_ADDR = POINTER(bt_addr)
"""
execfile('\program files\ppygui_client\bluetooth_ce.py')
"""
RFCOMM_PORT_FLAGS_REMOTE_DCB = 0x00000001
RFCOMM_PORT_FLAGS_KEEP_DCD = 0x00000002
RFCOMM_PORT_FLAGS_AUTHENTICATE = 0x00000004
RFCOMM_PORT_FLAGS_ENCRYPT = 0x00000008
RegisterDevice = ctypes.windll.coredll.RegisterDevice
DeregisterDevice = ctypes.windll.coredll.DeregisterDevice
GetLastError = ctypes.windll.coredll.GetLastError
SetLastError = ctypes.windll.coredll.SetLastError
class PORTEMUPortParams(Structure):
_fields_=[
( 'channel' , INT),
( 'flocal', INT ),
( 'device', BT_ADDR),
( 'imtu', INT ),
( 'iminmtu', INT ),
( 'imaxmtu', INT ),
( 'isendquota', INT ),
( 'irecvquota', INT ),
( 'uuidService', GUID ),
( 'uiportflags', UINT)
]
def __init__( self, device_str=None,
flocal=False,
channel = None,
uuidService=None,
uiportflags=None ):
if device_str is None and not flocal:
raise Exception( 'device address missing in client mode.' )
# memset (&pp, 0, sizeof(pp));
ctypes.memset( ctypes.addressof(self), 0, ctypes.sizeof(self) )
self.flocal = INT( flocal )
if not flocal:
bta = c_ulonglong( long(device_str, 16) )
print(bta)
bta_p = BT_ADDR( bta )
self.deivce = bta_p
# I don't have the channel address
# pp.channel # channel & 0xff;
if uuidService:
self.uuidService = uuidService
else:
#
self.uuidService = GUID("{00001101-0000-1000-8000-00805F9B34FB}")
if channel:
self.channel = channel & 0xff
if uiportflags:
self.uiportflags = uiportflags
print( "try uuidService" )
pp = PORTEMUPortParams('dc1d30428b19')
pp.uiportflags = RFCOMM_PORT_FLAGS_AUTHENTICATE
# pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB
# pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB | RFCOMM_PORT_FLAGS_AUTHENTICATE
index = 6
SetLastError( DWORD(0) )
#HANDLE h = RegisterDevice ("COM", index, "btd.dll", (DWORD)&pp );
h = RegisterDevice(u"COM", index, u"btd.dll", DWORD( ctypes.addressof(pp) ) )
if h :
print( 'handle=', h )
print( "COM", index )
DeregisterDevice( h )
else:
print('failed', GetLastError())
端口号不正确,不知为何,我的设备上1、2、5、6端口一直被占用。更改端口可解决此问题。
Winsock 方式确实更容易工作。根据 .net InTheHand
蓝牙库的文档。在 WinCE 上设置 bt 虚拟 COM 端口有两种方法,一种是 RegisterDevice 调用,另一种是通过 registery controlled service 需要重启。前者不稳定,似乎对许多设备没有影响。我想我的都包括在内,真是浪费时间...
我正在尝试让蓝牙打印机在 WinCE 手持设备上工作。
当我通过 execfile('.../bt_ce.py')
在 PythonCE 中 运行 它时,它没有提供任何反馈,就像我没有输入任何内容一样。没有句柄或索引被打印回来。再次尝试后,总是报错110
或2102
。
我是 C++ 和 WinCE API 的新手。我看不出问题出在哪里。
我的设备是 WinCE 5.0 条码扫描仪。我的代码如下改编自https://msdn.microsoft.com/en-us/library/ms881004.aspx
# -*- coding: utf-8 -*-
import ctypes
from ctypes import POINTER, Structure, c_ulonglong, c_int, pointer, c_ulong, c_wchar
from comtypes import GUID
from ctypes.wintypes import DWORD,WORD,BYTE,UINT, HANDLE, WCHAR, ULONG
INT = c_int
ULONGLONG = c_ulonglong
# typedef ULONGLONG bt_addr, *pbt_addr, BT_ADDR, *PBT_ADDR;
bt_addr = ULONGLONG
BT_ADDR = POINTER(bt_addr)
"""
execfile('\program files\ppygui_client\bluetooth_ce.py')
"""
RFCOMM_PORT_FLAGS_REMOTE_DCB = 0x00000001
RFCOMM_PORT_FLAGS_KEEP_DCD = 0x00000002
RFCOMM_PORT_FLAGS_AUTHENTICATE = 0x00000004
RFCOMM_PORT_FLAGS_ENCRYPT = 0x00000008
RegisterDevice = ctypes.windll.coredll.RegisterDevice
DeregisterDevice = ctypes.windll.coredll.DeregisterDevice
GetLastError = ctypes.windll.coredll.GetLastError
SetLastError = ctypes.windll.coredll.SetLastError
class PORTEMUPortParams(Structure):
_fields_=[
( 'channel' , INT),
( 'flocal', INT ),
( 'device', BT_ADDR),
( 'imtu', INT ),
( 'iminmtu', INT ),
( 'imaxmtu', INT ),
( 'isendquota', INT ),
( 'irecvquota', INT ),
( 'uuidService', GUID ),
( 'uiportflags', UINT)
]
def __init__( self, device_str=None,
flocal=False,
channel = None,
uuidService=None,
uiportflags=None ):
if device_str is None and not flocal:
raise Exception( 'device address missing in client mode.' )
# memset (&pp, 0, sizeof(pp));
ctypes.memset( ctypes.addressof(self), 0, ctypes.sizeof(self) )
self.flocal = INT( flocal )
if not flocal:
bta = c_ulonglong( long(device_str, 16) )
print(bta)
bta_p = BT_ADDR( bta )
self.deivce = bta_p
# I don't have the channel address
# pp.channel # channel & 0xff;
if uuidService:
self.uuidService = uuidService
else:
#
self.uuidService = GUID("{00001101-0000-1000-8000-00805F9B34FB}")
if channel:
self.channel = channel & 0xff
if uiportflags:
self.uiportflags = uiportflags
print( "try uuidService" )
pp = PORTEMUPortParams('dc1d30428b19')
pp.uiportflags = RFCOMM_PORT_FLAGS_AUTHENTICATE
# pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB
# pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB | RFCOMM_PORT_FLAGS_AUTHENTICATE
index = 6
SetLastError( DWORD(0) )
#HANDLE h = RegisterDevice ("COM", index, "btd.dll", (DWORD)&pp );
h = RegisterDevice(u"COM", index, u"btd.dll", DWORD( ctypes.addressof(pp) ) )
if h :
print( 'handle=', h )
print( "COM", index )
DeregisterDevice( h )
else:
print('failed', GetLastError())
端口号不正确,不知为何,我的设备上1、2、5、6端口一直被占用。更改端口可解决此问题。
Winsock 方式确实更容易工作。根据 .net InTheHand
蓝牙库的文档。在 WinCE 上设置 bt 虚拟 COM 端口有两种方法,一种是 RegisterDevice 调用,另一种是通过 registery controlled service 需要重启。前者不稳定,似乎对许多设备没有影响。我想我的都包括在内,真是浪费时间...