【问题标题】:C# Issues with MCP2210.dllMCP2210.dll 的 C# 问题
【发布时间】:2018-12-18 15:36:51
【问题描述】:

我目前正在 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;
    }
}

}

【问题讨论】:

    标签: c# .net usb hid microchip


    【解决方案1】:

    方法的第三个参数(“M_Mcp2210_OpenByIndex”)是根据 dll doc(可在 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), null)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多