I write code for USB Bulk Transfer using "tiusbdll.dll" in C#. I take refrence wuth USB_Bulk Transfer with TI. Some time code work well but some time Write & Read code not work....Application hang when i call write/read function...
Once i plug out & again plug in USB. its application work again...this happen randomly.
DLL Import
--------------------------------------------------------------------------------------------
[DllImport("tiusbdll.dll", EntryPoint = "InitializeDevice", ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
public static extern IntPtr InitializeDevice(ushort usVID, ushort usPID, ref System.Guid lpGUID, ref bool pbDriverInstalled);
//[DllImport("tiusbdll.dll", EntryPoint = "ReadUSBPacket", ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
[DllImport("tiusbdll.dll")]
public static extern UInt32 ReadUSBPacket(IntPtr hHandle, [In] byte[] pcBuffer, UInt32 ulSize, ref UInt32 pulRead, UInt32 ulTimeoutMs, IntPtr hBreak);
//[DllImport("tiusbdll.dll", EntryPoint = "WriteUSBPacket", ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurity]
[DllImport("tiusbdll.dll")]
public static extern bool WriteUSBPacket(IntPtr hHandle, [Out] byte[] pcBuffer, UInt32 ulSize, ref UInt32 pulWritten);
--------------------------------------------------------------------------------------------
Code of USB Intilization
-------------------------------------------------------------------------------------------
private void btnInitializeUSB_Click(object sender, EventArgs e)
{
ushort BULK_VID = 0x1cbe;
ushort BULK_PID = 0x0003;
bool bDriverInstalled = false;
// Create a GUID, match to what is in INF file
System.Guid GUID_DEVINTERFACE_LUMINARY_BULK;
GUID_DEVINTERFACE_LUMINARY_BULK = new System.Guid("{6E45736A-2B1B-4078-B772-B3AF2B6FDE1C}");
// Init the USB device
hUSB = InitializeDevice(BULK_VID, BULK_PID, ref GUID_DEVINTERFACE_LUMINARY_BULK, ref bDriverInstalled);
}
-------------------------------------------------------------------------------------------
Code for Read/Write
private void btnSendData_Click(object sender, EventArgs e)
{
UInt32 dwError;
byte[] szBuffer = new byte[256];
byte[] szBufferRead = new byte[256];
const UInt32 INFINITE = 0xFFFFFFFF;
UInt32 ulRead = 1;
UInt32 pulWritten = 0;
IntPtr hBreak = IntPtr.Zero;
// Load up array with useless "t"
int x;
for (x = 0; x < 3; x++)
{
szBuffer[x] = 3; // Convert.ToByte("t");
}
string strSendMessage = txtSendMessage.Text;
szBuffer = Encoding.ASCII.GetBytes(txtSendMessage.Text);
// do this transaction 10k times for the fun of it...
int y;
//for (y = 0; y < 10000; y++)
//{
bool ret = WriteUSBPacket(hUSB, szBuffer, Convert.ToUInt16(strSendMessage.Length), ref pulWritten);
dwError = ReadUSBPacket(hUSB, szBufferRead, 256, ref ulRead, INFINITE, hBreak);
string strRec = ASCIIEncoding.ASCII.GetString(szBufferRead);
string strSend = ASCIIEncoding.ASCII.GetString(szBuffer);
}