我几周前开始使用c#进行编程(主要来自java),目前我正在努力使用libMPSSE.dll库。
以下是我导入函数的方法:
[DllImportAttribute("libMPSSE.dll", EntryPoint = "I2C_GetNumChannels", CallingConvention = CallingConvention.Cdecl)]
public static extern uint I2C_GetNumChannels(ref uint NumChannels);
[DllImportAttribute("libMPSSE.dll", EntryPoint = "I2C_OpenChannel", CallingConvention = CallingConvention.Cdecl)]
public static extern uint I2C_OpenChannel(uint index, ref IntPtr handler);
[DllImportAttribute("libMPSSE.dll", EntryPoint = "I2C_GetChannelInfo", CallingConvention = CallingConvention.Cdecl)]
private static extern uint I2C_GetChannelInfo(uint index, ref FT_DEVICE_LIST_INFO_NODE chanInfo);
这就是.dll中的C ++头文件:
FTDI_API FT_STATUS I2C_GetNumChannels(uint32 *numChannels);
FTDI_API FT_STATUS I2C_GetChannelInfo(uint32 index, FT_DEVICE_LIST_INFO_NODE *chanInfo);
FTDI_API FT_STATUS I2C_OpenChannel(uint32 index, FT_HANDLE *handle);
前两个运行正常,但如果我调用第三个,我得到FatalExecutionEngineError:
for (uint i = ch; i >= 0; i--) { //ch contains number of chans available
FT_DEVICE_LIST_INFO_NODE nodeInfo = new FT_DEVICE_LIST_INFO_NODE();
I2C_GetChannelInfo(i, ref nodeInfo); // crash here
Console.WriteLine(" > info: "+nodeInfo.Description);
}
}
我想崩溃是由FT_DEVICE_LIST_INFO_NODE参数引起的,在我的代码中看起来像这样:
public class FT_DEVICE_LIST_INFO_NODE
{
public UInt32 Flags;
public FTDI.FT_DEVICE Type;
public UInt32 ID;
public UInt32 LocId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string SerialNumber;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string Description;
public IntPtr ftHandle;
}
根据.dll文档,在c ++代码中看起来像这样:
typedef struct _ft_device_list_info_node {
DWORD Flags;
DWORD Type;
DWORD ID;
DWORD LocId;
char SerialNumber[16];
char Description[64];
FT_HANDLE ftHandle;
} FT_DEVICE_LIST_INFO_NODE;
我现在有点迷失,只是尝试和错误而不知道如何解决这个问题,当然没有帮助这是我第一次使用非托管.dll:D
可以在此处找到lib http://www.ftdichip.com/Support/SoftwareExamples/MPSSE/LibMPSSE-I2C.htm 这里有文档 http://www.ftdichip.com/Support/Documents/AppNotes/AN_177_User_Guide_For_LibMPSSE-I2C.pdf
提前致谢
/编辑:我忘记了FT_DEVICE枚举
public enum FT_DEVICE
{
/// <summary>
/// FT232B or FT245B device
/// </summary>
FT_DEVICE_BM = 0,
/// <summary>
/// FT8U232AM or FT8U245AM device
/// </summary>
FT_DEVICE_AM,
/// <summary>
/// FT8U100AX device
/// </summary>
FT_DEVICE_100AX,
/// <summary>
/// Unknown device
/// </summary>
FT_DEVICE_UNKNOWN,
/// <summary>
/// FT2232 device
/// </summary>
FT_DEVICE_2232,
/// <summary>
/// FT232R or FT245R device
/// </summary>
FT_DEVICE_232R,
/// <summary>
/// FT2232H device
/// </summary>
FT_DEVICE_2232H,
/// <summary>
/// FT4232H device
/// </summary>
FT_DEVICE_4232H,
/// <summary>
/// FT232H device
/// </summary>
FT_DEVICE_232H,
/// <summary>
/// FT232X device
/// </summary>
FT_DEVICE_X_SERIES
};
答案 0 :(得分:0)
谢谢你,我现在跑了。 我用FT_DEVICE_LIST_INFO_NODE创建了一个结构,并添加了LayoutKind.Sequential和CharSet.Ansi属性。我还将FTDI.FT_DEVICE更改为一个简单的uint用于测试目的,它似乎现在运行。
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct FT_DEVICE_LIST_INFO_NODE
{
public uint Flags;
public uint Type;
public uint ID;
public uint LocId;
/// char[16]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 16)]
public string SerialNumber;
/// char[64]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)]
public string Description;
}