Bluetooth プリンタを WinCE ハンドヘルドで動作させようとしています。デバイスは ですでに信頼済みとして設定されておりControl Panel\Bluetooth Device Property
、PIN 認証済みです。
呼び出しはエラーなしでデバイス ハンドルを返しますが、OPEN_EXISTING を使用しRegisterDevice
てポートを開こうとすると、システム エラー コード 55が返されます。CreateFileW
INVALID_HANDLE_VALUE
ERROR_DEV_NOT_EXIST
私は WinCE API が初めてで、C++ も初めてです。問題がどこにあるのかわかりません。
設定の何が問題になっていますか? この bt-vcom を介してプリンターと通信する前に、追加の手順はありますか?
私のデバイスは WM5 HTC Diamond Phone です。そのCommunication Manager
> Bluetooth
>COM Port
セクションに、作成したポートが表示されません (ただし、それらは実際に作成されたようです: サードパーティ アプリにリストされTerminalCE
、これらのポートを使用してシステム GUI で仮想 COM ポートを作成しようとすると、占有されていると報告されます)。 . ここでシステム GUIに別のポートを作成するとOutgoing Port
、Python は新しいポートを介してプログラム的にプリンターと通信できます。結局のところ、私は同じ API を使用しているのではないのですか...なぜ私のものは機能しないのですか?
私のコードは、https ://msdn.microsoft.com/en-us/library/ms881004.aspx から次のように変更されています。
# -*- coding: utf-8 -*-
from time import sleep
import ctypes
from ctypes import POINTER, Structure
from ctypes import c_ulonglong, c_int, pointer, c_ulong, c_wchar, c_char, c_ushort
from ctypes import windll, cdll
from ctypes import memset, addressof, sizeof, byref
from comtypes import GUID
from ceserial import Serial
import wintypex as w
# in wintypex
# ULONGLONG = c_ulonglong
# bt_addr = ULONGLONG
# BT_ADDR = POINTER(bt_addr)
# BT_ADDR_PTR = POINTER(BT_ADDR)
# uiportflags
RFCOMM_PORT_FLAGS_REMOTE_DCB = 0x00000001
RFCOMM_PORT_FLAGS_KEEP_DCD = 0x00000002
RFCOMM_PORT_FLAGS_AUTHENTICATE = 0x00000004
RFCOMM_PORT_FLAGS_ENCRYPT = 0x00000008
core = windll.coredll # windll.kernel32
RegisterDevice = core.RegisterDevice # HANDLE RegisterDevice( LPCWSTR lpszType, DWORD dwIndex, LPCWSTR lpszLib, DWORD dwInfo );
RegisterDevice.restype = w.HANDLE
RegisterDevice.argtypes = [ w.LPCWSTR, w.DWORD, w.LPCWSTR, w.DWORD ]
DeregisterDevice = core.DeregisterDevice
GetLastError = core.GetLastError
SetLastError = core.SetLastError
# For BT_COM support.
class PORTEMUPortParams(Structure):
_fields_=[
( 'channel', w.INT),
( 'flocal', w.INT ),
( 'device', w.BT_ADDR),
( 'imtu', w.INT ),
( 'iminmtu', w.INT ),
( 'imaxmtu', w.INT ),
( 'isendquota', w.INT ),
( 'irecvquota', w.INT ),
( 'uuidService', GUID ),
( 'uiportflags', w.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( addressof(self), 0, sizeof(self) ) # memset (&pp, 0, sizeof(pp));
if not flocal:
bta = c_ulonglong( int(device_str, 16) )
print(bta)
bta_p = w.BT_ADDR( bta )
self.deivce = bta_p
self.flocal = w.INT( flocal )
# https://stackoverflow.com/questions/27302060/how-to-check-if-an-paired-bluetooth-device-is-a-printer-or-a-scanner-android
# " Note: most common UUID (scanners, printers, Mice)
# have the generic UUID 0001101-0000-1000-8000-00805F9B34FB "
if uuidService:
self.uuidService = uuidService
else:
self.uuidService = GUID("{00001101-0000-1000-8000-00805F9B34FB}")
if uiportflags:
self.uiportflags = uiportflags
if channel:
self.channel = channel & 0xff # pp.channel = channel & 0xff;
print('class defined.')
print( "try uuidService" )
pp = PORTEMUPortParams('dc1d30428b19') # PORTEMUPortParams pp;
# pp.uiportflags = RFCOMM_PORT_FLAGS_AUTHENTICATE
# pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB
pp.uiportflags = RFCOMM_PORT_FLAGS_REMOTE_DCB | RFCOMM_PORT_FLAGS_AUTHENTICATE
#HANDLE h = RegisterDevice ("COM", index, "btd.dll", (DWORD)&pp );
for i in range (1,10):
index = i
SetLastError( w.DWORD(0) )
h = RegisterDevice(u"COM", index, u"btd.dll", w.DWORD( addressof(pp) ) )
if h :
try:
print( 'handle=', h )
print( "COM%s" % index )
s = Serial( port="COM%s:" % index, open_existing=True )
s.open()
s.write(u'HELLO\r\n')
s.flushOutput()
# s.write(u'HI\r\n')
# s.flushOutput()
s.close()
except:
pass
# sleep(2)
DeregisterDevice( h )
break
else:
print('failed', GetLastError())
sleep(1)