2

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.

  • Does it work normally? If not, you might have a fake chip that has been bricked by the FTDI driver. – leppie Feb 04 '15 at 09:11
  • yeah the device has been used for over 8 years, the software was due it annual update and when I changed to .net 4.0 it just stopped working – captin jack sparrow Feb 04 '15 at 09:16
  • So it works correctly with HyperTerm? If so, have you tried other USB ports? Have you set the debugger to mixed mode and look at the native stack? – leppie Feb 04 '15 at 09:19
  • it works on HyperTerminal (if thats what you mean) and mixed mode do you mean any cpu setting? if so yes it does and the call satck top item is `[Managed to Native transition]` and the one before that is `[FTDI.Write(with the parameters)]` – captin jack sparrow Feb 04 '15 at 09:34
  • In Project Properties, select Debug. Check 'Enable native debugging'. When it 'hangs', break the debugger. In the callstack, you should probably enable viewing all code. Might be other options under Tools, Options, Debug you need to play with. This will show you what it is waiting for. – leppie Feb 04 '15 at 09:42
  • It could be *timeouts* or some other default settings, which value has changed in `.net 4.0`. Try to check it (or simple set all values explicitly, even if they are equal to *default*). – Sinatr Feb 04 '15 at 10:08
  • ntdll.dll!NtWriteFile() Unknown Is where it is sitting – captin jack sparrow Feb 04 '15 at 10:24
  • i'm now setting them and it is still sitting in the same place – captin jack sparrow Feb 04 '15 at 10:25

1 Answers1

1

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

ZoomVirus
  • 615
  • 7
  • 22