Hallo zusammen
ich habe folgendes Problem:
Die DLL gibt Werte über Referenzen zurück. Das funktioniert bei RxDataLen noch, aber bei RxStCode0 und RxStCode1 nicht.
Für die Ansteuerung eines Kartenlesers bin ich auf eine dll angewiesen über die dieser angesteuert wird.
Laut Dokumentation sollte ein Aufruf so aussehen
int APIENTRY RS232_ExeCommand(HANDLE ComHandle,BYTE TxCmCode,BYTE TxPmCode, int _
TxDataLen,BYTE TxData[],BYTE *RxReplyType,BYTE *RxStCode1,BYTE *RxStCode0, _
int *RxDataLen,BYTE RxData[]) daraus hab ich dann erstmal folgenden Import für VB.Net gemacht
<DllImport("CRT_288_K001.dll", EntryPoint:="USB_ExeCommand", _
CallingConvention:=CallingConvention.Winapi, SetLastError:=True)> _
Public Shared Function USB_ExeCommand(ByVal ComHandle As IntPtr, ByVal TxCmCode _
As Byte, ByVal TxPmCode As Byte, ByVal TxDataLen As Integer, ByVal TxData As _
Byte(), ByRef RxReplyType As RxReplyType, ByRef RxStCode1 As Byte, ByRef _
RxStCode0 As Byte, ByRef RxDataLen As Integer, ByVal RxData As Byte()) As _
APIENTRY
End Function Das funktioniert aber so nicht und wirft eine Exception
Grund ist wohl die Übergabe von RxDataLen als Referenz
Dann hab ich das mal umgebaut (ByVal RxDataLen As IntPtr)
<DllImport("CRT_288_K001.dll", EntryPoint:="USB_ExeCommand", _
CallingConvention:=CallingConvention.Winapi, SetLastError:=True)> _
Public Shared Function USB_ExeCommand(ByVal ComHandle As IntPtr, ByVal _
TxCmCode As Byte, ByVal TxPmCode As Byte, ByVal TxDataLen As Integer, _
<MarshalAs(UnmanagedType.LPArray)> ByVal TxData As Byte(), ByRef _
RxReplyType As RxReplyType, ByRef RxStCode1 As IntPtr, ByRef RxStCode0 _
As IntPtr, ByVal RxDataLen As IntPtr, ByVal RxData As Byte()) As _
APIENTRY
End Function aufgerufen wird das Ganze dann so (Ausschnitt aus einer Methode, myReaderHandle ist ne Klassenvariable)
Dim TxCmCode As Byte = &H30 'Initialize command RESET
Dim TxPmCode As Byte = lock
Dim CmData As Byte() = New Byte() {}
Dim ReType As RxReplyType
Dim RxStCode1 As Byte = 0
Dim RxStCode0 As Byte = 0
Dim RxDataLen As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Integer)))
Dim RxData As Byte() = New Byte(256) {}
Dim rc As APIENTRY
rc = USB_ExeCommand(myReaderHandle, TxCmCode, TxPmCode, CmData, ReType, _
RxStCode1, RxStCode0, RxDataLen, RxData)
add2Log(Modul.CardReader, Loglevel.DEBUG, "[CRT] USB_ExeCommand( rc=" & _
rc.ToString _
& " RxReplyType = " & KonstantenName(RxReplyType) _
& " Status1 :" & RxStCode1.ToString _
& " Status0 :" & RxStCode0.ToString _
& " Datalen: " & Marshal.ReadInt32(RxDataLen).ToString _
& " Data : " & ByteArrayToASCII(RxData) & " )") Da bekomme ich nun die korrekte Länge der Daten und die Daten komme auch brav in der RxData Variable an.
Nun zu meinem Problem.
Der Kartenleser gibt über die beiden Statusbytes RxStCode0,RxStCode1 seinen Status zurück, z.B. ob eine Karte eingelegt ist.
Diese Daten kommen aktuell nicht sauber zurück, steht also quasi Blödsinn drin, drolligerweise immer der Gleiche.
Hab dann auch versucht über Marshalling die Bytes zu übergeben
Dim RxStCode1 As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Byte)))
Dim RxStCode0 As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Byte))) und den Import dann entsprechend ByRef, by ByVal bekomme ich immer dieselben Daten zurück, die aber nicht richtig sind.
<DllImport("CRT_288_K001.dll", EntryPoint:="USB_ExeCommand", _
CallingConvention:=CallingConvention.Winapi, SetLastError:=True)> _
Public Shared Function USB_ExeCommand(ByVal ComHandle As IntPtr, ByVal TxCmCode _
As Byte, ByVal TxPmCode As Byte, ByVal TxDataLen As Integer, <MarshalAs( _
UnmanagedType.LPArray)> ByVal TxData As Byte(), ByRef RxReplyType As _
RxReplyType, ByRef RxStCode1 As IntPtr, ByRef RxStCode0 As IntPtr, ByVal _
RxDataLen As IntPtr, ByVal RxData As Byte()) As APIENTRY
End Function Nun bekomme ich in den bytes wechselnde Daten, da die Funktion in einer Endlosschleife aufgerufen wird, also quasi gepollt wird.
Das ist so auch nicht richtig.
Irgendwie blick ich es grad nicht richtig.
Hat einer ne Idee was ich falsch mache ?
Danke und Viele Grüße
TwentySixer
|