I’m just starting a project that involves talking to a board with an FT245R USB interface. I’ve got an XP VM set up with VS 2010 (some target machines will be running XP), the FTD2XX dll, the C# interface code published by FTDI, and some working VB6 code to get started with.
All seemed straightforward-ish. The first thing I had to do was rewrite the C# interface code to take out the message boxes it popped up when there were errors, and change it so that it throws exceptions instead of returning statuses that have to be checked for every call. It uses a somewhat bizarre and long winded way of referencing the functions in the dll, getting function pointers and marshalling them to delegates as they are used (rather than just using extern declarations), but it works so I left it.
Next thing was to create a Windows Forms app, and open the USB device using its description. Worked straight away. Excellent. Put some buttons on the form and run it again. Didn’t work – failed to open the device. That happened repeatedly, so I tried running the VB program. That worked, then running the app again worked. Then it didn’t.
Then followed many hours of trying to work out what made it work and what didn’t. I found that if I copied the app to the host machine (W7 64 bit) and ran it, it worked every time. If I ran the exe of the app on the XP machine, it worked almost every time (the VB app fails very occasionally too). Once I ran the exe and it worked, the app running in the IDE would usually work a couple of times before failing. I put in retries, sleeps, took out the VS debug hosting, all sorts of things without any consistent success.
One way I everntually found to make it work every time was to put a breakpoint on the FT_OpenEx and click the Step Over button. That worked every time. Oh well, I thought, I’ll leave the breakpoint there and when I run it in the IDE I’ll just press F10 when it gets there. Doesn’t work. Fails just as often as not having the breakpoint. But if I press the Step Over toolbar button in the IDE it works every time…
Not only that, but if I put in a message box when the first attempt to open it failed, sometimes the retry would work and sometimes it wouldn’t. Turns out it works every time when I dismiss the message box using the mouse, and not when I use the keyboard.
Eventually, I discovered that if I start running the app by clicking the Run button on the toolbar, rather than pressing F5 as I habitually do, it works every bloody time. Took me a whole day to find that!
There’s obviously some weird interaction between the VS IDE, the mouse, the keyboard and the FTDI driver, but I’ve no idea what it could be.