{"id":17255322,"url":"https://github.com/crozone/ftdi.d2xx.csharp","last_synced_at":"2025-03-26T08:16:18.194Z","repository":{"id":88314183,"uuid":"599823184","full_name":"crozone/FTDI.D2XX.CSharp","owner":"crozone","description":"C# interop wrapper for the FTDI D2XX Direct Driver (ftd2xx.dll)","archived":false,"fork":false,"pushed_at":"2023-02-13T05:53:09.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T09:31:42.683Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crozone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-10T00:28:13.000Z","updated_at":"2023-02-10T00:30:13.000Z","dependencies_parsed_at":"2023-03-13T18:25:19.061Z","dependency_job_id":null,"html_url":"https://github.com/crozone/FTDI.D2XX.CSharp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crozone%2FFTDI.D2XX.CSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crozone%2FFTDI.D2XX.CSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crozone%2FFTDI.D2XX.CSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crozone%2FFTDI.D2XX.CSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crozone","download_url":"https://codeload.github.com/crozone/FTDI.D2XX.CSharp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245615010,"owners_count":20644376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T07:11:18.462Z","updated_at":"2025-03-26T08:16:18.169Z","avatar_url":"https://github.com/crozone.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FTDI.D2XX.Wrapper\n\nC# interop wrapper for the FTDI D2XX Direct Driver (ftd2xx.dll)\n\n## Usage\n\nThe `FtdiD2xxDevice` class provides an object-oriented managed wrapper for the native FTDI Direct Driver interface.\nThe class abstracts the native device handle and implements `IDisposable` and destructor in order to make using the API safe.\n\n```csharp\nusing FTDI.D2XX.Wrapper;\n\n// Optionally get a list of all devices\n// FT_DEVICE_LIST_INFO_NODE[] devices = FtdiD2xxDevice.GetDevices();\n\n// Create the device\nusing (FtdiD2xxDevice device = new FtdiD2xxDevice())\n{\n    // Open the device\n    device.OpenByIndex(0);\n    //device.OpenBySerial(serial);\n    //device.OpenByDescription(description);\n    //device.OpenByLocation(location);\n\n    // Use the device\n    // ...\n\n    // Close the device.\n    // This is not strictly required, since disposing the device will also close it.\n    device.Close();\n}\n```\n\nThe `FtFunctions` static class implements the actual `DllImports` that interop with the native ftd2xx library.\n\n## Project Status\n\nMost functions listed in the [FD2XX Programmer's Guide](https://www.google.com/url?sa=t\u0026rct=j\u0026q=\u0026esrc=s\u0026source=web\u0026cd=\u0026cad=rja\u0026uact=8\u0026ved=2ahUKEwiP1ZyC54n9AhXSR2wGHa3-DgcQFnoECBUQAQ\u0026url=https%3A%2F%2Fwww.ftdichip.com%2FSupport%2FDocuments%2FProgramGuides%2FD2XX_Programmer%2527s_Guide(FT_000071).pdf) have been implemented.\n\nHowever, some are lower priority and still TODOs.\n\n## GPIO and Relay boards\n\nMany USB to GPIO/Relay boards (eg the [SainSmart USB to Relay boards](https://www.umart.com.au/product/sainsmart-usb-eight-channel-relay-board-for-automation-12-v-60454)) are implemented using an FTDI FT245R class chip. The relays are wired to each of the 8 output pins on the IC via a high current driver.\n\nYou may notice that simply opening the Virtual COM Port (VCP) and trying to write to it does nothing.\n\nThis is because by default, the FTDI FT245R devices start in \"regular interface mode\", which requires the \"RD\" pin to be strobed in order to read each successive byte from the receive FIFO. This is undesirable since the relay board does not operate in this way, and simply opening and writing to the VCP will not work. The device is stuck in \"regular interface mode\" and cannot be changed out of this mode from the VCP interface alone.\n\nInstead, the device needs to be put into a special bitbang bit mode, which causes all of the 8 output pins to immediately latch to the value of the last byte received without any activity on the RD pin.\nThese special modes can only be set by calling into the ftd2xx.dll, which in turn talks to the FTDI Direct Mode driver to send a SetBitMode request to the device. After this, the device will behave as needed for the relay board.\n\nOn Windows, the Direct Driver is installed in parallel with the Virtual COM Port driver (VCP) by the CDM driver package.\nWindows 10 usually finds this driver on Windows Update without any additional setup.\n\nOn Linux/MacOS, only one driver can be installed at a time. The VCP driver will need to be uninstalled and then the Direct Driver installed in its place.\n\n### Relay/GPIO board bitbang example\n\n```csharp\n// Create the device\nusing (FtdiD2xxDevice device = new FtdiD2xxDevice())\n{\n    // Open the first device by zero based index.\n    // This will throw an exception if the device is not found or could not be opened.\n    device.OpenByIndex(0);\n    \n    // It's also possible to open the device by serial string, description string, or location long\n\n    //device.OpenBySerial(serial);\n    //device.OpenByDescription(description);\n    //device.OpenByLocation(location);\n\n    // Reset the device so that it's in a known state\n    device.ResetDevice();\n\n    // Purge the RX and TX buffers\n    device.Purge(FT_PURGE.FT_PURGE_RX | FT_PURGE.FT_PURGE_TX);\n\n    // Set the baud rate\n    device.SetBaudRate(9600);\n\n    // Put the device into async bitbang mode.\n    // This mode uses the chip output pins as a parallel interface.\n    // Commonly, USB to GPIO/Relay breakout boards are implemented using a FT245R (or similar) chip that is wired in this way.\n    device.SetBitMode(0xFF, FT_BITMODE.FT_BITMODE_ASYNC_BITBANG);\n\n    // Write to device\n    byte[] sendBuffer = new byte[1];\n    for (int loops = 0; loops \u003c 8; loops++)\n    {\n        byte currentFlags = 0x00;\n        for (int i = 0; i \u003c 2; i++)\n        {\n            for (int j = 0; j \u003c 8; j++)\n            {\n                currentFlags ^= (byte)(1 \u003c\u003c j);\n                sendBuffer[0] = currentFlags;\n                int bytesWritten = device.Write(sendBuffer, sendBuffer.Length);\n\n                Thread.Sleep(50);\n            }\n        }\n    }\n\n    // Close the device.\n    // This is not strictly required, since disposing the device will also close it.\n    device.Close();\n}\n```\n\n## Native Library\n\nThis project interops with the native FTDI D2XX shared library. Typically, this is ftd2xx.dll, however there are many platform specific versions.\n\nSee [FTDI D2XX Direct Driver Downlaods](https://ftdichip.com/drivers/d2xx-drivers/).\n\nThis project interacts with the native library through `DllImport`. Currently the library name is hardcoded to \"ftd2xx.dll\".\n\n**The library does not include a version of ftd2xx.dll itself**, nor does it attempt to bind to a different library name depending on the OS or process architecture.\n\nIncluding the correct \"ftd2xx.dll\" in the build output is left as an excercise for the library consumer. It should be copied into the build directory during the application build.\n\n### TODO\n\nIt would be nice to improve the DLL loading experience. Source generators could accomplish this.\n\nA dedicated static class could be generated for every OS/architecture combination, and mapped to a different library name. A proxy class would then decide which concrete implementation to call at runtime, based on the runtime OS platform and process architecture.\n\nThis is a low-priority TODO due to complexity.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrozone%2Fftdi.d2xx.csharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrozone%2Fftdi.d2xx.csharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrozone%2Fftdi.d2xx.csharp/lists"}