Search code examples
c#.net-4.0.net-3.5ftdi

FTDI Write function Never ending


I have a FTDI write function that was working when the program was .net3.5, but I have been updating it so it now runs on .net 4.0 and now my FTDI write function doesn't return after entering.

private FT_HANDLE portHandle = 0;

public unsafe int Write(byte[] buffer, int offset, int count)
{
    if (portHandle == 0)
    {
        throw new IOException("Port Closed");
    }

    try
    {
        uint bytesWritten = 0;
        Status status = Status.FT_OTHER_ERROR;
        byte[] data = new byte[count];
        for (int i = 0; i < count; i++)
        {
            data[i] = buffer[i + offset];
        }
        fixed (byte* pBuffer = data)
        {
            status = FT_Write(portHandle, pBuffer, (uint)count, ref bytesWritten);// the line that never returns
        }
        if (status == Status.FT_OK)
        {
            return (int)bytesWritten;
        }
        throw new IOException("Failed To Write: " + status.ToString());
    }
    catch (DllNotFoundException ex)
    {
        throw new NotSupportedException("Driver not installed", ex);
    }
}

portHandle doesn't get caught in the equivalent to 0 if check. My questions is what could be causing this and how could I fix it.


Solution

  • In dot net3.5 to 4.0 they change timers and settings for such devices firstly set you timers to what you want,However do not use varaiables and getters and setter as they will not work due to the way windows uses them use constants and uints and make sure you give enough time for both read and write(read is usually longer than write) remember time is done in milli seconds This should work as i had the same problem when i made a switch

    So main Points set read and write timeouts

    Do Not Use getters and setters

    Make sure they are constand uints