Reputation: 21
i started programming in c# a couple of weeks ago (coming from mostly java) and i am currenty struggling using the libMPSSE.dll library.
Here is how i import the functions:
[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);
And this is what the C++ header file in the .dll looks like:
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);
The first two run fine, but if i call the third one i get the 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);
}
}
I guess the crash is cause by the FT_DEVICE_LIST_INFO_NODE parameter, which looks like this in my code:
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;
}
And according to the .dll documentation, looks like this in c++ code:
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;
I am kinda lost right now and just try and error without really knowing how to fix this problem, certainly doesnt help that is my first time using an unmanaged .dll :D
The lib can be found here http://www.ftdichip.com/Support/SoftwareExamples/MPSSE/LibMPSSE-I2C.htm with documentation available here http://www.ftdichip.com/Support/Documents/AppNotes/AN_177_User_Guide_For_LibMPSSE-I2C.pdf
Thanks in advance
/edit: i forgot the FT_DEVICE enum
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
};
Upvotes: 1
Views: 511
Reputation: 21
Thanky you everone, i got it running now. I made a struct out of FT_DEVICE_LIST_INFO_NODE and added LayoutKind.Sequential and CharSet.Ansi attribute. I also changed FTDI.FT_DEVICE to a simple uint for test purpose and it seems to run now.
[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;
}
Upvotes: 1