USB Read & Access Denied

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)

By using one of these windows tools from Microsoft, it might give you an insight what happens.

Process Explorer: https://technet.microsoft.com/en-us/sysinternals/bb896653

Process Monitor: https://technet.microsoft.com/en-us/sysinternals/bb896645

Pass your memoryblock as a Ptr instead of a CString.

http://www.xojo.com/blog/en/2014/12/oh-say-can-you-c.php

Tim,

I gave that a try, although it is the same result. The write works so I expected the read would be no different. I modified the write as well, it still works fine.

John,

I haven’t found anything logged or in the tools, but I do like them. Thanks for the trip.

There must be some access control thing I’m missing but I’m running out of ideas.

Do you have any virus checker running ? . Maybe that cause the issue

Sometimes this error can be caused by other programs accessing the serial port. I ran into the same problem with my last project and it was due to an external program. Do some thinking as to what may be accessing the port.

This is a great point as virus checkers access everything if the settings are configured to do so, thus they are using the serial port.

Problem solved.

The Declare statement

Declare Function _MPUSBOpen Lib “MPUSBAPI.DLL” (instance as Uint32, PidVis As CString, pEp As CString, dwDir As Double, Reserved As Uint32) As Ptr

Should be

Declare Function _MPUSBOpen Lib “MPUSBAPI.DLL” (instance as Uint32, PidVis As CString, pEp As CString, dwDir As Uint32, Reserved As Uint32) As Ptr

That small change made it work just fine. No one can explain why it worked for writes and not for reads, but it does.