Uso di VBScript per gestire i dispositivi (documentazione di POS per .NET v1.14 SDK)
Usando l'API WMI documentata in questa sezione, è possibile gestire i dispositivi usando codice gestito o scripting. POSDM.EXE è un'interfaccia della riga di comando per questa API. Questo esempio di VBScript esegue le operazioni seguenti:
Usa il metodo WMI ExecQuery per recuperare un elenco di oggetti posDevice installati. Con questo elenco di oggetti assistenza, lo script visualizza il tipo, il nome, il percorso corrispondente e il relativo stato abilitato o disabilitato. Questo è analogo all'esecuzione del comando seguente:
PosDM.exe LISTDEVICES
Tenta quindi di assegnare il percorso COM1 all'oggetto assistenza installato, Microsoft Msr Simulator usando il metodo AddDevice. Equivale all'esecuzione di:
PosDM.exe ADDDEVICE COM1 /SONAME:Microsoft Msr Simulator
Se il metodo AddDevice ha esito negativo, lo script rileva l'errore e presuppone che COM1 sia già stato aggiunto al dispositivo e quindi tenti di eliminarlo chiamando DeleteDevice. Equivale all'esecuzione di:
PosDM.exe DELETEDEVICE COM1
Se il metodo AddDevice in precedenza ha avuto esito negativo, lo script tenta di chiamare di nuovo AddDevice. Il programma viene chiuso se il metodo ha esito negativo.
Infine, l'esempio tenta di aggiungere il nome logico MSRSim a questo oggetto assistenza chiamando AddName. Equivale all'esecuzione di:
PosDM.exe ADDNAME MSRSim /SONAME:"Microsoft Msr Simulator"
È possibile visualizzare i risultati di questo esempio eseguendo:
PosDM.exe LISTDEVICES
And
PosDM.exe LISTNAMES
Per eseguire l'esempio
L'oggetto assistenza Microsoft Msr Simulator è stato installato con l'SDK. Assicurarsi che sia installato nel computer che verrà usato per eseguire l'esempio.
Copiare questo script in un file PosDMSample.vbs
Eseguire lo script con la riga di comando seguente:
CScript //U PosDMSample.vbs
Esempio
'Get a handle to the POS namespace service into 'objServices'.
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objServices = objLocator.ConnectServer(, "/root/MicrosoftPointOfService")
'List the POS devices.
EnumeratePosDevice
'Add a name: MSRSim for Msr Simulator by retrieving the SO and invoking AddDevice() then AddName()
WScript.Echo "Add Device on COM1 and add name 'MSRSim' for MsrSimulator ..."
Set objSO = objServices.Get("ServiceObject.Type='Msr',Name='Microsoft Msr Simulator'")
On Error Resume Next
objSO.AddDevice "COM1"
if Err.number <> 0 Then
WScript.Echo "AddDevice failed - it already is in use."
WScript.Echo "Try to delete the device..."
On Error Resume Next
objSO.DeleteDevice "COM1"
if Err.number <> 0 Then
WScript.Echo "DeleteDevice failed"
WScript.Quit 1
end if
WScript.Echo "DeleteDevice succeeded! Attempting AddDevice again..."
On Error Resume Next
objSO.AddDevice "COM1"
if Err.number <> 0 Then
WScript.Echo "AddDevice failed a second time - exiting"
WScript.Quit 2
end if
end if
Set objDevice = objServices.Get("PosDevice.SoName='Microsoft Msr Simulator',Type='Msr',Path='COM1'")
objDevice.AddName "MSRSim"
Set objDevice = GetDevice("Msr", "MSRSim")
WScript.Echo "Added 'MSRSim' to: " & objDevice.Type & vbTab & objDevice.SoName & vbTab & objDevice.Path
'Enumerate the sClass by name
Sub EnumeratePosDevice( )
sClass = "PosDevice"
WScript.Echo "Enumerating " & sClass & "..." & vbCrLf
Set collection = objServices.ExecQuery("SELECT * From " & sClass)
For Each obj In collection
Enabled = "DISABLED"
if obj.Enabled = true Then
Enabled = "ENABLED"
end If
WScript.Echo obj.Type & Space(15-len(obj.type)) & obj.SoName & Space(35-len(obj.SoName)) & Enabled & vbTab & obj.Path
Next
WScript.Echo vbCrLf
End Sub
'Return a PosDevice matching DeviceType and Name.
Function GetDevice( DeviceType, Name )
Set Logical = GetLogicalDevice( DeviceType, Name )
objectPath = "PosDevice.SoName='" & Logical.SoName & "',Type='" & DeviceType & "',Path='" & Logical.Path & "'"
Set GetDevice = objServices.Get(objectPath)
End Function
'Return a LogicalDevice matching DeviceType and Name.
Function GetLogicalDevice( DeviceType, Name )
Query = "SELECT * From LogicalDevice WHERE Type = '" & DeviceType & "' AND Name='" & Name & "'"
Set collection = objServices.ExecQuery( Query )
For Each obj In collection
Set GetLogicalDevice = obj
exit For
Next
End Function
Se il percorso COM1 non è stato assegnato a un dispositivo, l'esempio genera un output simile a questo codice.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Enumerating PosDevice...
Msr Microsoft Msr Simulator ENABLED
Msr Microsoft Msr Simulator ENABLED COM1
Keylock Microsoft Keylock Simulator ENABLED
Scanner Microsoft Scanner Simulator ENABLED
CashDrawer Microsoft CashDrawer Simulator ENABLED
CheckScanner Microsoft CheckScanner Simulator ENABLED
LineDisplay Microsoft LineDisplay Simulator ENABLED
PinPad Microsoft PinPad Simulator ENABLED
PosPrinter Microsoft PosPrinter Simulator ENABLED
PosKeyboard Microsoft PosKeyboard Simulator ENABLED
Add Device on COM1 and add name 'MSRSim' for MsrSimulator ...
AddDevice failed - it already be in use.
Try to delete the device...
DeleteDevice succeeded! Attempting AddDevice again...
Added 'MSRSim' to: Msr Microsoft Msr Simulator
Se il percorso COM1 è già in uso e non si verifica alcun altro errore, lo script genera un output simile a questo codice.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Enumerating PosDevice...
Msr Microsoft Msr Simulator ENABLED
Msr Microsoft Msr Simulator ENABLED COM1
Keylock Microsoft Keylock Simulator ENABLED
Scanner Microsoft Scanner Simulator ENABLED
CashDrawer Microsoft CashDrawer Simulator ENABLED
CheckScanner Microsoft CheckScanner Simulator ENABLED
LineDisplay Microsoft LineDisplay Simulator ENABLED
PinPad Microsoft PinPad Simulator ENABLED
PosPrinter Microsoft PosPrinter Simulator ENABLED
PosKeyboard Microsoft PosKeyboard Simulator ENABLED
Add Device on COM1 and add name 'MSRSim' for MsrSimulator ...
AddDevice failed - it already be in use.
Try to delete the device...
DeleteDevice succeeded! Attempting AddDevice again...
Added 'MSRSim' to: Msr Microsoft Msr Simulator
Vedi anche
Risorse aggiuntive
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per