|
|
|
Microchip Custom USB with C#.NET
|
|
Date: 2007/06/15 16:45
|
By: Kevin L.
|
Status: Visitor
|
|
|
|
|
|
|
I found Mat's article here on using C# to interface a Microchip USB microcontroller, and in hopes of finding a good C# wrapper for the mpusbapi DLL, downloaded the PC side code. Quite frankly, I was horrified to find that it was almost exactly the same code as provided by Microchip in their sample C++ code, but marked as 'unsafe' to get rid of the errors generated by pointer use, and slightly modified to fit the hardware he used.
As such, I've cobbled together a more 'correct' wrapper class (but omitted the rest of the application, as it is irrelevant). Please bear in mind that this is only a first revision. Also note that, since the DLL interface is represented by unmanaged datatypes, most types can be modified as long as the underlying datatype/size is the same. So, the wrapper can be tweaked to your specific application if necessary.
The main advantage of this wrapper is that the datatypes used in the interface are common C# types, so use of the wrapper doesn't require the 'unsafe' branding. Also, this means no tricks are required to get your parameters in the correct type to use the functions.
| Code: |
using DWORD = UInt32;
using HANDLE = UInt32;
class MPUSB
{
public const HANDLE INVALID_HANDLE_VALUE = 0xFFFFFFFF;
public const DWORD SUCCESS = 1;
public const DWORD FAIL = 0;
public enum PipeDir : uint
{
Write = 0,
Read = 1
}
public const int maxDevs = 127;
[DllImport("mpusbapi.dll",EntryPoint="_MPUSBGetDLLVersion")]
public static extern DWORD MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBGetDeviceCount")]
public static extern DWORD GetDeviceCount([MarshalAs(UnmanagedType.LPStr)] string pVID_PID);
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBOpen", SetLastError = true)]
public static extern DWORD Open ( DWORD instance,
[MarshalAs(UnmanagedType.LPStr)] string pVID_PID,
[MarshalAs(UnmanagedType.LPStr)] string pEP,
[MarshalAs(UnmanagedType.U4)] PipeDir dwDir,
DWORD dwReserved );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBRead", SetLastError = true)]
public static extern DWORD Read ( HANDLE handle,
[MarshalAs(UnmanagedType.LPArray)] byte[] pData,
DWORD dwLen,
out DWORD pLength,
DWORD dwMilliseconds );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBWrite", SetLastError = true)]
public static extern DWORD Write ( HANDLE handle,
[MarshalAs(UnmanagedType.LPArray)] byte[] pData,
DWORD dwLen,
out DWORD pLength,
DWORD dwMilliseconds );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBReadInt")]
public static extern DWORD ReadInt( HANDLE handle,
[MarshalAs(UnmanagedType.LPArray)] byte[] pData,
DWORD dwLen,
out DWORD pLength,
DWORD dwMilliseconds );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBClose")]
public static extern bool Close ( HANDLE handle );
public static DWORD DLLVersion { get { return MPUSBGetDLLVersion(); } }
public static void GetDLLVersion(out ushort major, out ushort minor)
{
DWORD ver = DLLVersion;
major = (ushort)(DLLVersion >> 16);
minor = (ushort)(DLLVersion & 0x0000FFFF);
}
}
|
It's a fairly simple and straightforward wrapper, but comments and suggestions are always welcome.
|
|
|
|
|
|
Re:Microchip Custom USB with C#.NET
|
|
Date: 2007/10/05 09:02
|
By: Salam
|
Status: Visitor
|
|
|
|
|
|
|
Hello Kevin L.
Have you any main program to the class MPUSB.C# I compiled this class in C#, but I am new in programm with C#. I want to show for example DllVersion in console application or any facilites of the class MPUSB.C#.
Thanks Regard Salam
|
|
|
|
|
|
Re:Microchip Custom USB with C#.NET
|
|
Date: 2009/03/09 01:45
|
By: dlchambers
|
Status: Visitor
|
|
|
|
|
|
|
I got around the unsafe stuff by putting all the low-level C++ code in a COM dll. My C# app then references the COM "assembly" and builds a very nice, easy-to-use wrapper for it.
I originally started by doing a full port of my C++ code (here: http://www.dlwrr.com/electronics/picdem/) to C#, but the low-level stuff (I pulled the MPUSB API dll code into my app - MChip's dll is very thin and didn't want yet another DLL to manage) is onerous to do in safe/managed C#. Thus I adopted the COM approach, which in the end makes for VERY clean C#.
-Dave
|
|
|
|
|
|
Re:Microchip Custom USB with C#.NET
|
|
Date: 2010/07/28 18:18
|
By: -
|
Status: Visitor
|
|
|
|
|
|
|
Kevin L. wrote: I found Mat's article here on using C# to interface a Microchip USB microcontroller, and in hopes of finding a good C# wrapper for the mpusbapi DLL, downloaded the PC side code. Quite frankly, I was horrified to find that it was almost exactly the same code as provided by Microchip in their sample C++ code, but marked as 'unsafe' to get rid of the errors generated by pointer use, and slightly modified to fit the hardware he used.
As such, I've cobbled together a more 'correct' wrapper class (but omitted the rest of the application, as it is irrelevant). Please bear in mind that this is only a first revision. Also note that, since the DLL interface is represented by unmanaged datatypes, most types can be modified as long as the underlying datatype/size is the same. So, the wrapper can be tweaked to your specific application if necessary.
The main advantage of this wrapper is that the datatypes used in the interface are common C# types, so use of the wrapper doesn't require the 'unsafe' branding. Also, this means no tricks are required to get your parameters in the correct type to use the functions.
| Code: |
using DWORD = UInt32;
using HANDLE = UInt32;
class MPUSB
{
public const HANDLE INVALID_HANDLE_VALUE = 0xFFFFFFFF;
public const DWORD SUCCESS = 1;
public const DWORD FAIL = 0;
public enum PipeDir : uint
{
Write = 0,
Read = 1
}
public const int maxDevs = 127;
[DllImport("mpusbapi.dll",EntryPoint="_MPUSBGetDLLVersion")]
public static extern DWORD MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBGetDeviceCount")]
public static extern DWORD GetDeviceCount([MarshalAs(UnmanagedType.LPStr)] string pVID_PID);
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBOpen", SetLastError = true)]
public static extern DWORD Open ( DWORD instance,
[MarshalAs(UnmanagedType.LPStr)] string pVID_PID,
[MarshalAs(UnmanagedType.LPStr)] string pEP,
[MarshalAs(UnmanagedType.U4)] PipeDir dwDir,
DWORD dwReserved );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBRead", SetLastError = true)]
public static extern DWORD Read ( HANDLE handle,
[MarshalAs(UnmanagedType.LPArray)] byte[] pData,
DWORD dwLen,
out DWORD pLength,
DWORD dwMilliseconds );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBWrite", SetLastError = true)]
public static extern DWORD Write ( HANDLE handle,
[MarshalAs(UnmanagedType.LPArray)] byte[] pData,
DWORD dwLen,
out DWORD pLength,
DWORD dwMilliseconds );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBReadInt")]
public static extern DWORD ReadInt( HANDLE handle,
[MarshalAs(UnmanagedType.LPArray)] byte[] pData,
DWORD dwLen,
out DWORD pLength,
DWORD dwMilliseconds );
[DllImport("mpusbapi.dll", EntryPoint = "_MPUSBClose")]
public static extern bool Close ( HANDLE handle );
public static DWORD DLLVersion { get { return MPUSBGetDLLVersion(); } }
public static void GetDLLVersion(out ushort major, out ushort minor)
{
DWORD ver = DLLVersion;
major = (ushort)(DLLVersion >> 16);
minor = (ushort)(DLLVersion & 0x0000FFFF);
}
}
|
It's a fairly simple and straightforward wrapper, but comments and suggestions are always welcome.
|
|
|
|