MCP2210.dll的C#问题

时间:2018-07-10 17:33:40

标签: c# .net usb hid microchip

我目前正在Visual Studio上的c#中使用Microchip的USB设备MCP2210开发一个项目。我正在使用.Net v4和Microchip发布的mcp2210_dll_dotnetv4_x86.dll。不幸的是,我很困惑为什么我在连接HID设备时遇到问题。我的问题来自通话

  

D1.Handle = MCP2210.M_Mcp2210_OpenByIndex(D1.VID,D1.PID,i-1,null)<

位于第84行。响应为-106,设备已连接。太好了,除了我需要将该函数调用与此函数连接以接收正确的句柄。如果我更改索引(当前为i-1),则会收到-101错误,该索引处没有设备。有没有人建议诊断或解决这个问题?会在

下发布项目代码
using mcp2210_dll_m;  
using System;  
using System.Text;
namespace HID_Command_Line
{  

public struct Device  
{
    public ushort PID { get; set; }  
    public ushort VID { get; set; }  
    public IntPtr Handle { get; set; }  

    public Device (ushort vid, ushort pid, IntPtr handle)
    {

        VID = vid;
        PID = pid;
        Handle = handle;
    }
}


class Program
{

    static void Main(string[] args)
    {
        byte[] Command = new byte[64];


        string temp;
        ConsoleKeyInfo code;





        temp = ReadCommand();

        do
        {
            Command = Converter.ToByteArray(temp);

            Converter.CheckByteArray(Command);
            code = Console.ReadKey();
            Console.Write("\r\n");
            if (code.Key == ConsoleKey.Y)
            {
                if (ConnectUSB() ==  true)
                {
                  // SetUpDeviceSettings();
                }
            }
            else if (code.Key == ConsoleKey.N)
            {
               temp = ReadCommand();
            }

        } while (code.Key != ConsoleKey.Escape);
        Console.WriteLine("Exited Correctly");
    }
    public static string ReadCommand()
    {
        string temp;
        Console.WriteLine("Please Enter the Command in Hex Format");
        //Console.WriteLine(" Type Cntrl + Space for Nul ");
        temp = Console.ReadLine();
        int length = temp.Length / 2;
        for (int i = length; i < 64; i++)
        {
            temp += "00";
        }
        return temp;
    }
    public static bool ConnectUSB()
    {
        bool confirmation = false;
        Device D1 = new Device(0x04D8, 0x00DE, (IntPtr)null);
        uint i = (uint) MCP2210.M_Mcp2210_GetConnectedDevCount(D1.VID, D1.PID);
        if (i == 1)
        {
            Console.WriteLine("yeah Baby, Press any Button to Connect to Single Devices in your Area");  //assuming that the index for 1 device is always 0
            Console.Read();
            D1.Handle = MCP2210.M_Mcp2210_OpenByIndex(D1.VID, D1.PID, i-1, null);
            Console.WriteLine("Handle is {0}", D1.Handle);
            int Error = MCP2210.M_Mcp2210_GetLastError();
            Console.WriteLine("Response is {0} \r\n", Error);
            switch (Error)
            {
                case -101:
                    {
                        Console.WriteLine("Error, No Such Index Exists");
                        break;
                    }
                case -103:
                    {
                        Console.WriteLine("Error, Device Not Found");
                        break;
                    }
                case -104:
                    {
                        Console.WriteLine("Error, Internal Buffer too small");
                        break;
                    }
                case -105:
                    {
                        Console.WriteLine("Error, Open Device Error");
                        break;
                    }
                case -106:
                    {
                        Console.WriteLine("Error, Device Already Connected");
                        confirmation = true;
                        break;
                    }
                case -20:
                    {
                        Console.WriteLine("Error, MALLOC Unsucessful");
                        break;
                    }
                case -10:
                    {
                        Console.WriteLine("Error, NULL Returned");
                        break;
                    }
                case -1:
                    {
                        Console.WriteLine("Error, Unknown Error");
                        break;
                    }
                case 0:
                    {
                        Console.WriteLine("Success");
                        confirmation = true;
                        break;
                    }

            }


        }
        else if (i > 1)
        {
            Console.WriteLine("To Many Devices Connected. Only one is to be run at a time \r\n");
            Console.Read();

        }
        else
        {
            Console.WriteLine("System does not register the device. Please connect the device");
        }




        return confirmation;
    }

  // public static SetUpDeviceSettings()
  //  {

  //      return;
   // }

}

static class Converter
{
    public static void CheckByteArray(byte[] bytes)
    {
        var convert = new StringBuilder("Is this command correct[] { ");
        foreach (var b in bytes)
        {
            convert.Append(b + ", ");
        }
        convert.Append("} \r\n [y]es, [n]o or Escape to Exit \r\n");
        Console.WriteLine(convert.ToString());
    }

    public static byte[] ToByteArray(String HexString)
    {
        int NumberChars = HexString.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
        }
        return bytes;
    }
}

}

1 个答案:

答案 0 :(得分:0)

该方法的第三个参数(“ M_Mcp2210_OpenByIndex”)是根据dll文档(在http://ww1.microchip.com/downloads/en/DeviceDoc/MCP2210_DLLv2.zip上)在您的计算机上连接的MCP数量

您可以使用MCP2210.M_Mcp2210_GetConnectedDevCount(MCP2210_VID,MCP2210_PID)获取此号码

尝试:MCP2210.M_Mcp2210_OpenByIndex(D1.VID,D1.PID,(uint)MCP2210.M_Mcp2210_GetConnectedDevCount(MCP2210_VID,MCP2210_PID),空)

OR

MCP2210.M_Mcp2210_OpenByIndex(D1.VID,D1.PID,(uint)0,null)