I’m controlling a Firgelli LAC board using direct calls to MPBUSAPI.DLL. I can open, write to, and close the port with no problems. I can open the port for read, but, when I perform the read I get a system error number 5 (ACCESS_DENIED). I can’t see any reason why this shouldn’t work. I have investigated the MBS plug-in for this purpose (and I use it on the mac version of the program without difficulty) but it isn’t workable on Windows 8 for some reason.
All net.wisdom is appreciated.
The code is shown below.
Declare Function GetLastError Lib "Kernel32.DLL" () As Integer
Declare Function _MPUSBOpen Lib "MPUSBAPI.DLL" (instance as Uint32, PidVis As CString, pEp As CString, dwDir As Double, Reserved As Uint32) As Ptr
Declare Function _MPUSBWrite Lib "MPUSBAPI.DLL" (handle as Ptr, data as Cstring, dwlen as Uint32, ByRef bytesWritten As UInt32, timeout as Uint32) As Uint32
Declare Function _MPUSBClose Lib "MPUSBAPI.DLL" (handle as Ptr) as Uint32
Declare Function _MPUSBRead Lib "MPUSBAPI.DLL" (handle as Ptr, data as Cstring, dwlen as Uint32, ByRef bytesRead As Uint32, timeout as Uint32) As Uint32
Declare Function _MPUSBGetDeviceCount Lib "MPUSBAPI.DLL" (data As Cstring) As Uint32
Dim readHandle As ptr
Dim writeHandle As ptr
Dim mem as MemoryBlock
mem = New MemoryBlock(64)
Dim dat as MemoryBlock
// Set up the command to send.
dat = New MemoryBlock(4)
dat.byte(0)=&h10 ' command
dat.byte(1)=&h00 'data
dat.byte(2)=&h00 'data
dat.byte(3) = 3 'number of bytes written
Log "Number of devices = " + Str(_MPUSBGetDeviceCount(cs)) // Just a check that we are communicating with the DLL
writeHandle = _MPUSBOpen (0, cs, "\\mchp_ep1", MP_WRITE, 0)
If GetLastError <> 0 Then
Log "Error opening for write: " + Str(GetLastError)
End If
Dim bytesWritten As Uint32
if _MPUSBWrite(writeHandle, dat, 3, bytesWritten, 1000) <> MP_SUCCESS Then
log "Error writing to pipe: " + Str(GetLastError)
End If
if _MPUSBClose(writeHandle) <> MP_SUCCESS Then
log "Error closing output pipe: " + Str(GetLastError)
End if
readHandle = _MPUSBOpen (0, cs, "\\mchp_ep1", MP_READ, 0)
If GetLastError <> 0 Then
Log "Error opening for read: " + Str(GetLastError)
End If
Dim bytesRead As Uint32
If _MPUSBRead(readHandle, mem, 64, bytesRead, 5000) <> MP_SUCCESS Then
Log "Error during read: " + Str(GetLastError) // Always returns a 5
End If
Log "Read: " + Str(bytesRead) + " bytes " + Hex(mem.byte(0)) + " " + Hex(mem.byte(1)) + " " + Hex(mem.byte(2))
Log "Value: " + Str(Val(Mem))
if _MPUSBClose(readHandle) <> MP_SUCCESS Then
log "Error closing input pipe: " + Str(GetLastError)