public virtual List <FTDI.FT_DEVICE_INFO_NODE> GetDevicesList() { var dList = new FTDI.FT_DEVICE_INFO_NODE[10]; var devicesList = new List <FTDI.FT_DEVICE_INFO_NODE>(); var result = ftdi.GetDeviceList(dList); if (result != FTDI.FT_STATUS.FT_OK) { return(devicesList); } devicesList.AddRange(dList.Where(device => device != null).AsEnumerable()); return(devicesList); }
protected FTDI OpenChannel(string channelName, uint baudRate) { var res = new FTDI(); FTDI.FT_STATUS status = FTDI.FT_STATUS.FT_OTHER_ERROR; FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[255]; status = res.GetDeviceList(devicelist); // ON LINUX RUN APP WITH SUDO OR CONFIGURE ACCESS TO USB FTDI DEVICES Console.WriteLine($"getdevicelist status is {status}"); devicelist = devicelist.Where(x => x != null).ToArray(); if (!devicelist.Any()) { throw new Exception("No FTDI devices found."); } foreach (var device in devicelist) { Console.WriteLine($"Description is '{device.Description}'"); Console.WriteLine($"SerialNumber is '{device.SerialNumber}'"); Console.WriteLine($"ID is '{device.ID}'"); Console.WriteLine($"LocId is '{device.LocId}'"); Console.WriteLine($"Type is '{device.Type}'"); Console.WriteLine($"------"); } status = res.OpenBySerialNumber(channelName); Debug.Assert(status == FTDI.FT_STATUS.FT_OK); res.ResetDevice(); Debug.Assert(status == FTDI.FT_STATUS.FT_OK); // for (int i = 0; i < 60; i++) // { // status = res.OpenBySerialNumber(channelName); // if ( // status != FTD2XX_NET.FTDI.FT_STATUS.FT_DEVICE_NOT_FOUND && // status != FTD2XX_NET.FTDI.FT_STATUS.FT_DEVICE_NOT_OPENED // ) // break; // Thread.Sleep(1000); // res = new FTDI(); // FTDI.FT_DEVICE_INFO_NODE[] list = new FTDI.FT_DEVICE_INFO_NODE[200]; // status = res.GetDeviceList(list); // } Debug.Assert(status == FTDI.FT_STATUS.FT_OK); status = res.SetBaudRate(baudRate); Debug.Assert(status == FTDI.FT_STATUS.FT_OK); status = res.SetLatency(0); Debug.Assert(status == FTDI.FT_STATUS.FT_OK); // enable async bitbang mode for all 8 pins status = res.SetBitMode(0b11111111, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG); Debug.Assert(status == FTDI.FT_STATUS.FT_OK); status = res.SetTimeouts(1, 1); Debug.Assert(status == FTDI.FT_STATUS.FT_OK); return(res); }
public void Open() { ProtocolVersion = 0; MaxReadPacketSize = DefaultMaxReadPacketSize; MaxWritePacketSize = DefaultMaxWritePacketSize; SerialPort sPort = null; string portName = PortName; // Is port specified? if (string.IsNullOrEmpty(portName) || portName.ToLower() == "auto") { portName = null; // Need to autodetect port // Is it running on Windows? try { // First of all lets check bus reported device descriptions var allComPorts = Win32DeviceMgmt.GetAllCOMPorts(); foreach (var port in allComPorts) { if (!DeviceNames.Contains(port.bus_description)) { continue; } // Seems like it's dumper port but it can be already busy try { sPort = OpenPort(port.name, (int)Timeout); // It's not busy portName = port.name; Console.WriteLine($"Autodetected virtual serial port: {portName}"); break; } catch { continue; } } } catch { } if (portName == null) { // Port still not detected, using Windows FTDI driver to determine serial number try { FTDI myFtdiDevice = new(); uint ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // FTDI serial number autodetect ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException("Failed to get number of devices (error " + ftStatus.ToString() + ")"); } // If no devices available, return if (ftdiDeviceCount == 0) { throw new IOException("Failed to get number of devices (error " + ftStatus.ToString() + ")"); } // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList); portName = null; if (ftStatus == FTDI.FT_STATUS.FT_OK) { var dumpers = ftdiDeviceList.Where(d => DeviceNames.Contains(d.Description)); portName = dumpers.First().SerialNumber; Console.WriteLine($"Autodetected USB device serial number: {portName}"); } if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException("Failed to get FTDI devices (error " + ftStatus.ToString() + ")"); } } catch { } } if (portName == null) { // Port still not detected, let's try Linux methods try { var devices = GetLinuxUsbDevices(); var dumpers = devices.Where(d => { var productFile = Path.Combine(d, "product"); return(File.Exists(productFile) && DeviceNames.Contains(File.ReadAllText(productFile).Trim())); }); if (dumpers.Any()) { portName = LinuxDeviceToPort(dumpers.First()); if (string.IsNullOrEmpty(portName)) { throw new IOException($"Can't detect device path"); } Console.WriteLine($"Autodetected USB device path: {portName}"); } } catch { } } if (portName == null) { throw new IOException($"{DeviceNames.First()} not found, try to specify port name manually"); } } if (sPort != null) { // success, already opened serialPort = sPort; return; } if (portName.ToUpper().StartsWith("COM") || File.Exists(portName)) { // Serial port name/path to open serialPort = OpenPort(portName, (int)Timeout); return; } try { // Is it VCP serial number? var ttyPath = LinuxDeviceSerialToPort(portName); if (!string.IsNullOrEmpty(ttyPath)) { serialPort = OpenPort(ttyPath, (int)Timeout); return; } } catch { } try { // Is is FTDI serial number? // Using Windows FTDI driver FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class FTDI myFtdiDevice = new(); // Open first device in our list by serial number ftStatus = myFtdiDevice.OpenBySerialNumber(portName); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException($"Failed to open device (error {ftStatus})"); } // Set data characteristics - Data bits, Stop bits, Parity ftStatus = myFtdiDevice.SetTimeouts(Timeout, Timeout); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException($"Failed to set timeouts (error {ftStatus})"); } ftStatus = myFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException($"Failed to set data characteristics (error {ftStatus})"); } // Set flow control ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x11, 0x13); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException($"Failed to set flow control (error {ftStatus})"); } // Set up device data parameters ftStatus = myFtdiDevice.SetBaudRate(PortBaudRate); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException($"Failed to set Baud rate (error {ftStatus})"); } // Set latency ftStatus = myFtdiDevice.SetLatency(0); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new IOException($"Failed to set latency (error {ftStatus})"); } d2xxPort = myFtdiDevice; return; } catch { } throw new IOException($"Can't open {serialPort}"); }