Online Development Notebook > Index > Programming Overview > General C# DLL Info

General C#⁄.NET Information using USBm.dll


This example uses C# to interface to the U4xx. The very minimum application would be to open the USB device and transmit a single command. This example shows the code that it takes to use USBm.dll to open the U4xx device and initialize the ports.

The Class

⁄⁄ needed to import the .dll
using System.Runtime.InteropServices;

    public class USBm
        {
        public static byte BitA0 = 0x00;
        public static byte BitA1 = 0x01;
        public static byte BitA2 = 0x02;
        public static byte BitA3 = 0x03;
        public static byte BitA4 = 0x04;
        public static byte BitA5 = 0x05;
        public static byte BitA6 = 0x06;
        public static byte BitA7 = 0x07;
        public static byte BitB0 = 0x08;
        public static byte BitB1 = 0x09;
        public static byte BitB2 = 0x0A;
        public static byte BitB3 = 0x0B;
        public static byte BitB4 = 0x0C;
        public static byte BitB5 = 0x0D;
        public static byte BitB6 = 0x0E;
        public static byte BitB7 = 0x0F;

⁄⁄  USBm.dll - C# pInvoke examples
⁄⁄  "Commands"
⁄⁄      [DllImport("USBm.dll", EntryPoint = "USBm_FindDevices", CharSet = CharSet.Auto)]
        [DllImport("USBm.dll")]
        public static extern bool USBm_FindDevices();
        [DllImport("USBm.dll")]
        public static extern int USBm_NumberOfDevices();
        [DllImport("USBm.dll")]
        public static extern bool USBm_DeviceValid(int Device);
        [DllImport("USBm.dll")]
        public static extern bool USBm_About(StringBuilder About);
        [DllImport("USBm.dll")]
        public static extern bool USBm_Version(StringBuilder Version);
        [DllImport("USBm.dll")]
        public static extern bool USBm_Copyright(StringBuilder Copyright);
        [DllImport("USBm.dll")]
        public static extern bool USBm_DeviceMfr(int Device, StringBuilder Mfr);
        [DllImport("USBm.dll")]
        public static extern bool USBm_DeviceProd(int Device, StringBuilder Prod);
        [DllImport("USBm.dll")]
        public static extern int USBm_DeviceFirmwareVer(int Device);
        [DllImport("USBm.dll")]
        public static extern bool USBm_DeviceSer(int Device, StringBuilder dSer);
        [DllImport("USBm.dll")]
        public static extern int USBm_DeviceDID(int Device);
        [DllImport("USBm.dll")]
        public static extern int USBm_DevicePID(int Device);
        [DllImport("USBm.dll")]
        public static extern int USBm_DeviceVID(int Device);
        [DllImport("USBm.dll")]
        public static extern bool USBm_DebugString(StringBuilder DBug);
        [DllImport("USBm.dll")]
        public static extern bool USBm_RecentError(StringBuilder rError);
        [DllImport("USBm.dll")]
        public static extern bool USBm_ClearRecentError();
        [DllImport("USBm.dll")]
        public static extern bool USBm_SetReadTimeout(uint TimeOut);
        [DllImport("USBm.dll")]
        public static extern bool USBm_ReadDevice(int Device, byte[] inBuf);
        [DllImport("USBm.dll")]
        public static extern bool USBm_WriteDevice(int Device, byte[] outBuf);
        [DllImport("USBm.dll")]
        public static extern bool USBm_CloseDevice(int Device);
        }

 

Example of function calling

⁄⁄ Test USBm device attached

    if ( !USBm.USBm_FindDevices() )
        { 
        MessageBox.Show( string.Format("No Device Present"), "USBm Devices", MessageBoxButtons.OK, MessageBoxIcon.Information );
        return;
        }  ⁄⁄ implied else

⁄⁄Walk the USBm.dll functions

    ⁄⁄ some containers
    StringBuilder sb = new StringBuilder( 200 );
    bool result = false;  ⁄⁄ return values

    ⁄⁄ .DLL FindDevices  returns the number of devices
    ⁄⁄  public static extern bool USBm_FindDevices();
    result = USBm.USBm_FindDevices();

    ⁄⁄ return the number of devices
    ⁄⁄ public static extern int USBm_NumberOfDevices();
    int TotalDevices = USBm.USBm_NumberOfDevices();
    int Device = TotalDevices -1;  ⁄⁄ only One device is ever attached so ...

    ⁄⁄ .DLL About info
    ⁄⁄ public static extern bool USBm_About(StringBuilder about );
    result = USBm.USBm_About( sb );

    ⁄⁄ .DLL Version info
    ⁄⁄ public static extern bool USBm_Version(StringBuilder Version);
    result = USBm.USBm_Version( sb );

    ⁄⁄ .DLL Copyright info
    ⁄⁄ public static extern bool USBm_Copyright(StringBuilder Copyright);
    result = USBm.USBm_Copyright( sb );

    ⁄⁄ Device Valid
    ⁄⁄public static extern bool USBm_DeviceValid(int Device);
    result = USBm.USBm_DeviceValid( Device );

    ⁄⁄ Device Manufacturer
    ⁄⁄public static extern bool USBm_DeviceMfr(int Device, StringBuilder Mfr);
    result = USBm.USBm_DeviceMfr( Device, sb );

    ⁄⁄ Device Product String
    ⁄⁄  public static extern bool USBm_DeviceProd(int Device, StringBuilder Prod);
    result = USBm.USBm_DeviceProd( Device, sb );

    ⁄⁄ Device Firmware Version
    ⁄⁄  public static extern int USBm_DeviceFirmwareVer(int Device);
    int FirmVer = USBm.USBm_DeviceFirmwareVer(Device);

    ⁄⁄ Device SerialNumber [ ]
    ⁄⁄  public static extern bool USBm_DeviceSer(int Device, StringBuilder dSer);
    result = USBm.USBm_DeviceSer(Device, sb);

    ⁄⁄ Device DiD
    ⁄⁄  public static extern int USBm_DeviceDID(int Device);
    int DID = USBm.USBm_DeviceDID(Device);

    ⁄⁄ Device PiD
    ⁄⁄  public static extern int USBm_DevicePID(int Device);
    int PID = USBm.USBm_DevicePID(Device);

    ⁄⁄ Device ViD
    ⁄⁄  public static extern int USBm_DeviceVID(int Device);
    int VID = USBm.USBm_DeviceVID(Device);

    ⁄⁄ Device Debug String
    ⁄⁄  public static extern int USBm_DebugString(int Device);
    result = USBm.USBm_DebugString(sb);

    ⁄⁄ Device Recent Error - always returns true
    ⁄⁄  public static extern bool USBm_RecentError(void);
    result = USBm.USBm_RecentError(sb);

    ⁄⁄ Device Clear Recent Error
    ⁄⁄  public static extern bool USBm_ClearRecentError(void);
    result = USBm.USBm_ClearRecentError();

    ⁄⁄ Device SetReadTimeout [ sixteen-bit millisecond value]
    ⁄⁄  public static extern bool USBm_SetReadTimeout(uint TimeOut);
    uint tOUT = 3000;
    result = USBm.USBm_SetReadTimeout(tOUT);

    ⁄⁄ Device WriteDevice [ 8 byte to write (device raw commands)]
    ⁄⁄  public static extern bool USBm_WriteDevice(int Device, ref int[] inBuf);
    byte[] OutBuf = { 0, 21, 3, 65, 8, 17, 60, 0 };
    result = USBm.USBm_WriteDevice(Device, OutBuf);

    ⁄⁄ Device ReadDevice [ ]
    ⁄⁄  public static extern bool USBm_ReadDevice(int Device, array);
    byte[] InBuf = { 0, 0, 0, 0, 0, 0, 0, 0 };
    result = USBm.USBm_ReadDevice(Device, InBuf);

    ⁄⁄ Device CloseDevice [ ]
    ⁄⁄  public static extern bool USBm_CloseDevice(int Device);
    result = USBm.USBm_CloseDevice(Device);


Hardware:  U401 USB Interface    U421 USB Interface   U451 USB Interface

Programming:  USBm DLL Programming    Download Files

Application Notes:  U4x1 Application Notes    Misc Applications and Information    FAQ

While every effort has been made to make sure that the information posted on this site is correct, the author can not be held liable for any damages whatsoever for losses as a result of the application of this information. Use this information at your own risk.

USBmicro can design your custom and semi-custom USB product. Email about USB design can be directed to " Robert " at usbmicro.com.

Copyright © USBmicro, L.L.C., 2002-2010

Previous  |  Next ]     [ Up  |  First  |  Last ]     (Article 22 of 126)

This page is created with TreePad