/// <summary> /// Открыть соединение с устройством. /// </summary> public void Open() { _device.OpenByIndex(DeviceIndex); IsDeviceOpen = _device.IsOpen; _device.GetDescription(out _description); _device.SetTimeouts(100, 100); #if DEBUG Console.WriteLine($"Device[{DeviceIndex}] Open: {_device.IsOpen}"); #endif }
// returns true on success static bool openSerialPort(TcpClient conn, string snToConnect) { NetworkStream ns = conn.GetStream(); ns.WriteTimeout = 10; ns.ReadTimeout = 10; byte[] msg; FTDI clientDevice = new FTDI(); FTDI.FT_STATUS ftdiRet = clientDevice.OpenBySerialNumber(snToConnect); ftdiRet |= clientDevice.SetBaudRate(1250000); ftdiRet |= clientDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_2, FTDI.FT_PARITY.FT_PARITY_ODD); ftdiRet |= clientDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0); ftdiRet |= clientDevice.SetTimeouts(8000, 100); ftdiRet |= clientDevice.InTransferSize(65536); ftdiRet |= clientDevice.SetDTR(true); if (ftdiRet != FTDI.FT_STATUS.FT_OK) { //throw new IOException("Failed to open FTDI device"); Console.WriteLine("ERR: Failed to open FTDI device\n"); msg = ASCIIEncoding.ASCII.GetBytes("ERR: Failed to open FTDI device\n"); ns.Write(msg, 0, msg.Length); ns.Close(); conn.Close(); return(false); } string desc = "X", sn = "X"; clientDevice.GetDescription(out desc); clientDevice.GetSerialNumber(out sn); Console.WriteLine("OK: Opened FTDI device {0}-{1}", desc, sn); msg = ASCIIEncoding.ASCII.GetBytes("OK: Opened:" + snToConnect + "\n"); ns.Write(msg, 0, msg.Length); CancellationTokenSource ct = new CancellationTokenSource(); Thread deviceThread = new Thread((ParameterizedThreadStart)doServer); deviceThreads[snToConnect] = new Tuple <Thread, CancellationTokenSource>(deviceThread, ct); Tuple <FTDI, TcpClient, CancellationToken> threadArg = new Tuple <FTDI, TcpClient, CancellationToken>(clientDevice, conn, ct.Token); deviceThread.IsBackground = true; deviceThread.Start(threadArg); return(true); }
static void Main(string[] args) { FTDI ftdi = new FTDI(); MAX7219 Display; uint devcount = 0; ftdi.GetNumberOfDevices(ref devcount); if (devcount > 0) { byte[] TransferBuffer = new byte[88]; uint NumBytesToTransfer; uint NumBytesTransfered = 0; Display = new MAX7219(TransferBuffer, WriteOut); //FTDI AN_108 3.8 Clock Divisor uint dwClockDivisor = 29; //Value of clock divisor, SCL Frequency = 60/((1+29)*2) (MHz) = 1Mhz FTDI.FT_DEVICE_INFO_NODE[] devices = new FTDI.FT_DEVICE_INFO_NODE[devcount]; string Buffer; FTDI.FT_STATUS s = ftdi.GetDeviceList(devices); for (uint ix = 0; ix < devcount; ix++) { ftdi.OpenBySerialNumber(devices[ix].SerialNumber); ftdi.GetCOMPort(out Buffer); Console.WriteLine(Buffer); ftdi.GetDescription(out Buffer); Console.WriteLine(Buffer); //FTDI Set Mode MPSSE s = ftdi.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET); s |= ftdi.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_MPSSE); if (s != FTDI.FT_STATUS.FT_OK) { Console.WriteLine("Fehler SetBitMode"); ftdi.Close(); return; } //FTDI Sync MPSSE (Check) FTDI_AN114 Page 10 if (Sync_MPSSE(ref ftdi, TransferBuffer) != FTDI.FT_STATUS.FT_OK) { Console.WriteLine("Fehler Sync MPSSE"); ftdi.Close(); return; } //Init FTDI SPI See FTDI AN_108 NumBytesToTransfer = 0; TransferBuffer[NumBytesToTransfer++] = 0x8a; // Disable Clock divide /5 TransferBuffer[NumBytesToTransfer++] = 0x97; // Disable adaptiv clockink TransferBuffer[NumBytesToTransfer++] = 0x8d; // Disables 3 phase data clocking // I think is not nesacery //TransferBuffer[NumBytesToTransfer++] = 0x80; //TransferBuffer[NumBytesToTransfer++] = 0x08; //TransferBuffer[NumBytesToTransfer++] = 0x0b; TransferBuffer[NumBytesToTransfer++] = 0x86; //3.8 Clock Divisor TransferBuffer[NumBytesToTransfer++] = (byte)(dwClockDivisor & 0xff); TransferBuffer[NumBytesToTransfer++] = (byte)(dwClockDivisor >> 8); s = ftdi.Write(TransferBuffer, NumBytesToTransfer, ref NumBytesTransfered); NumBytesToTransfer = 0; Thread.Sleep(20); TransferBuffer[NumBytesToTransfer++] = 0x85; // Disable loopback s |= ftdi.Write(TransferBuffer, NumBytesToTransfer, ref NumBytesTransfered); NumBytesToTransfer = 0; if (s != FTDI.FT_STATUS.FT_OK) { Console.WriteLine("SPI Init Fehler"); ftdi.Close(); return; } Console.WriteLine("SPI Init OK"); Console.WriteLine("Press ESC to Exit"); //Init 7219 s = ftdi.Write(TransferBuffer, Display.Init(8), ref NumBytesTransfered); UInt32 count = 0; while (true) { //Data s |= ftdi.Write(TransferBuffer, Display.WriteDec(count, (byte)(1 << ((byte)(count++ % 8)))), ref NumBytesTransfered); Console.WriteLine("SPI {0} Bytes Write", NumBytesTransfered); Thread.Sleep(100); if (Console.KeyAvailable && (Console.ReadKey(true)).Key == ConsoleKey.Escape) { break; } } s |= ftdi.Write(TransferBuffer, Display.Clr(), ref NumBytesTransfered); if (s != FTDI.FT_STATUS.FT_OK) { Console.WriteLine("SPI Fehler Write Data"); ftdi.Close(); return; } ftdi.Close(); } } else { Console.WriteLine("Kein FTDI gefunden :-("); } }
/// <summary> /// This method detects and configures Sainsmart 8ch USB outputs automatically (possibly FT245RBitbangController in general?). /// </summary> /// <param name="Cabinet">The cabinet object to which the automatically detected IOutputController objects are added if necessary.</param> public void AutoConfig(Cabinet Cabinet) { //Log.Write("FT245RBitbangControllerAutoConfigurator.AutoConfig started"); FTDI dummyFTDI = new FTDI(); uint amountDevices = 0; string callResult = ""; List <DeviceInfo> devicelist = new List <DeviceInfo>(); //fetch amount of devices, then kill instance callResult = dummyFTDI.GetNumberOfDevices(ref amountDevices).ToString(); //Log.Write("FT245RBitbangControllerAutoConfigurator.AutoConfig: amount of devices detected=" + amountDevices + ", callresult=" + callResult); dummyFTDI.Close(); dummyFTDI = null; //connect to each device using ftdi directly, snag the index and serial, then close and gc //do this seperate from adding instances in case multiple instances of ftdi would cause issues, as doing this too aggressive will cause locked exe / dll on exit for (uint i = 0; i < amountDevices; i++) { using (FTDI connectFTDI = new FTDI()) { string deviceSerial = ""; string deviceDesc = ""; connectFTDI.OpenByIndex(i); connectFTDI.GetSerialNumber(out deviceSerial); connectFTDI.GetDescription(out deviceDesc); devicelist.Add(new DeviceInfo(deviceSerial, deviceDesc)); //Log.Write("i=" + i + ", serial device=" + deviceSerial); connectFTDI.Close(); } } //next add instances of the controller to output, and all controller outputs for (int deviceIndex = 0; deviceIndex < devicelist.Count; deviceIndex++) { FT245RBitbangController FTDevice = new FT245RBitbangController(); FTDevice.Name = "FT245RBitbangController {0}".Build(deviceIndex); FTDevice.SerialNumber = devicelist[deviceIndex].serial; FTDevice.Description = devicelist[deviceIndex].desc; FTDevice.Id = deviceIndex; Log.Write("FT245RBitbangControllerAutoConfigurator.AutoConfig.. Detected FT245RBitbangController" + "[" + deviceIndex + "], name=" + FTDevice.Name + ", description: " + FTDevice.Description + ", serial #" + FTDevice.SerialNumber); if (!Cabinet.OutputControllers.Contains(FTDevice.Name)) { Cabinet.OutputControllers.Add(FTDevice); Log.Write("Detected and added FT245RBitbangController Id {0} with name {1}".Build(deviceIndex, FTDevice.Name)); //+40 used to define start of directoutputconfig[40...].xml if (!Cabinet.Toys.Any(T => T is LedWizEquivalent && ((LedWizEquivalent)T).LedWizNumber == deviceIndex - 0 + 40)) { LedWizEquivalent LWE = new LedWizEquivalent(); LWE.LedWizNumber = deviceIndex - 0 + 40; LWE.Name = "{0} Equivalent 1".Build(FTDevice.Name); for (int outputIndex = 1; outputIndex <= 8; outputIndex++) { LedWizEquivalentOutput LWEO = new LedWizEquivalentOutput() { OutputName = "{0}\\{0}.{1:00}".Build(FTDevice.Name, outputIndex), LedWizEquivalentOutputNumber = outputIndex }; LWE.Outputs.Add(LWEO); } if (!Cabinet.Toys.Contains(LWE.Name)) { Cabinet.Toys.Add(LWE); Log.Write("Added LedwizEquivalent Nr. {0} with name {1} for PacUIO with Id {2}".Build(LWE.LedWizNumber, LWE.Name, deviceIndex)); } } } } }