private void AttemptConnect() { connected = false; UInt32 DeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class ftdi = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = ftdi.GetNumberOfDevices( ref DeviceCount ); // Check status if(ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0) { commStat = CommStatus.NoDevice; return; } commStat = CommStatus.NoElev8; // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount]; try { // Populate our device list ftStatus = ftdi.GetDeviceList( DeviceList ); bool FoundElev8 = false; for(int i = 0; i < DeviceCount && FoundElev8 == false; i++) { if(DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES) continue; for(int baud = 0; baud < 2; baud++) { ftStatus = ftdi.OpenBySerialNumber( DeviceList[i].SerialNumber ); if(ftStatus == FTDI.FT_STATUS.FT_OK) { string portName; ftdi.GetCOMPort( out portName ); if(portName == null || portName == "") { ftdi.Close(); continue; } if(baud == 0) { ftdi.SetBaudRate( 115200 ); // try this first } else { ftdi.SetBaudRate( 57600 ); // then try this (xbee) } ftdi.SetDataCharacteristics( 8, 1, 0 ); ftdi.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0 ); txBuffer[0] = (byte)'E'; txBuffer[1] = (byte)'l'; txBuffer[2] = (byte)'v'; txBuffer[3] = (byte)'8'; uint written = 0; for(int j = 0; j < 10 && FoundElev8 == false && !quit; j++) // Keep pinging until it replies, or we give up { ftdi.Write( txBuffer, 4, ref written ); System.Threading.Thread.Sleep( 50 ); uint bytesAvail = 0; ftdi.GetRxBytesAvailable( ref bytesAvail ); // How much data is available from the serial port? if(bytesAvail > 0) { int TestVal = 0; while(bytesAvail > 0 && !quit) { uint bytesRead = 0; ftdi.Read( rxBuffer, 1, ref bytesRead ); if(bytesRead == 1) { TestVal = (TestVal << 8) | rxBuffer[0]; if(TestVal == (int)(('E' << 0) | ('l' << 8) | ('v' << 16) | ('8' << 24)) ) { FoundElev8 = true; commStat = CommStatus.Connected; break; } } if(bytesRead == 0) break; } } } if(FoundElev8) { connected = true; if(ConnectionStarted != null) { ConnectionStarted(); } break; } else { ftdi.Close(); } } } } } catch(Exception) { return; } }
//------------------------------------------------------------------------------------------------- public FTDI.FT_STATUS InitializeFTDI() { // Create new instance of the FTDI device class SPI_Device= new FTDI(); uint ftdiDeviceCount = 0; int i; Initialize_SPI_Constants(); // Determine the number of FTDI devices connected to the machine ftStatus = SPI_Device.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus != FTDI.FT_STATUS.FT_OK) { return (ftStatus); } // If no devices available, return if (ftdiDeviceCount == 0) { ftStatus = FTDI.FT_STATUS.FT_DEVICE_NOT_FOUND; return (ftStatus); } // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list ftStatus = SPI_Device.GetDeviceList(ftdiDeviceList); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (i = 0; i < ftdiDeviceCount; i++) { //MessageBox.Show("Device Index: " + i.ToString()); //MessageBox.Show("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); //MessageBox.Show("Type: " + ftdiDeviceList[i].Type.ToString()); //MessageBox.Show("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); //MessageBox.Show("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); //MessageBox.Show("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); //MessageBox.Show("Description: " + ftdiDeviceList[i].Description.ToString()); //MessageBox.Show(""); } } // Open first device in our list by serial number ftStatus = SPI_Device.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber); if (ftStatus != FTDI.FT_STATUS.FT_OK) { return (ftStatus); } // Set latency timer ftStatus = SPI_Device.SetLatency(2); if (ftStatus != FTDI.FT_STATUS.FT_OK) { return (ftStatus); } // Reset the controller ftStatus = SPI_Device.SetBitMode(0, 0); if (ftStatus != FTDI.FT_STATUS.FT_OK) { return (ftStatus); } // Set synchronous bit bang mode ftStatus = SPI_Device.SetBitMode(FT232Routputs, 4); // Set device to mode 4 and sets outputs if (ftStatus != FTDI.FT_STATUS.FT_OK) { return (ftStatus); } // Set baud rate/bit clock settings ftStatus = SPI_Device.SetBaudRate(3000000); if (ftStatus != FTDI.FT_STATUS.FT_OK) { return (ftStatus); } presetShiftRegisterOutputs(); return (ftStatus); }
public FTDI.FT_DEVICE_INFO_NODE[] GetDevices(UInt32 numberOfDevices) { // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[numberOfDevices]; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Populate our device list ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (UInt32 i = 0; i < numberOfDevices; i++) { Console.WriteLine("Device Index: " + i.ToString()); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString()); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString()); Console.WriteLine(""); } } return ftdiDeviceList; }
/// <summary> /// Constructor /// </summary> /// <param name="dev_index"></param> /// <returns></returns> public FTDI.FT_STATUS Init(uint dev_index = 0) { // Reste and purge any data ResetPort(); _ftdi.Purge(FTDI.FT_PURGE.FT_PURGE_RX | FTDI.FT_PURGE.FT_PURGE_TX); // Check we have a device uint count = 0; _ftdi.GetNumberOfDevices(ref count); Debug.Assert(count > dev_index, string.Format("No FTDI device at channel {0}. FTDI device count was {1}", dev_index, count)); _ftdi.OpenByIndex(dev_index); // Verify is the right type FTDI.FT_DEVICE_INFO_NODE[] devlist = new FTDI.FT_DEVICE_INFO_NODE[100]; FTDI.FT_STATUS status = _ftdi.GetDeviceList(devlist); FTDI.FT_DEVICE_INFO_NODE devinfo = devlist[dev_index]; FTD2XX_NET.FTDI.FT_DEVICE expected = FTD2XX_NET.FTDI.FT_DEVICE.FT_DEVICE_232H; Debug.Assert(devinfo.Type == expected, string.Format("Unexpected device type '{0}' at index {1}. Expected '{2}'", devinfo.Type, dev_index, expected.ToString())); // Enable MPSSE _ftdi.SetBitMode(0xFF, FTDI.FT_BIT_MODES.FT_BIT_MODE_MPSSE); //_ftdi.SetBitMode(0, 0); //_ftdi.SetBitMode(0, 2); // Set all outputs and data = all 0 //(0x80, level_low, dir_low, 0x82, level_high, dir_high) byte[] data = new byte[] { 0x80, _cha_state, 0xFF, 0x82, _chb_state, 0xFF }; uint n = 0; status = _ftdi.Write(data, data.Length, ref n); return status; }
/// <summary> /// Find the FDTI chip, connect, set baud to 9600, set sync bit-bang mode /// </summary> /// <returns></returns> public bool connect() { // Determine the number of FTDI devices connected to the machine ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus == FTDI.FT_STATUS.FT_OK) { Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString()); Console.WriteLine(""); } else { Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")"); return false; } if (ftdiDeviceCount == 0) { Console.WriteLine("Relay board not found, please try again"); return false; } else { // 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); // Open first device in our list by serial number ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber); if (ftStatus != FTDI.FT_STATUS.FT_OK) { Console.WriteLine("Error connecting to relay board"); return false; } // Set Baud rate to 9600 ftStatus = myFtdiDevice.SetBaudRate(9600); // Set FT245RL to synchronous bit-bang mode, used on sainsmart relay board myFtdiDevice.SetBitMode(0xFF, FTD2XX_NET.FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG); // Switch off all the relays myFtdiDevice.Write(startup, 1, ref bytesToSend); return true; } }
private void button5_Click( object sender, EventArgs e ) { UInt32 ftdiDeviceCount = 0; // Create new instance of the FTDI device class myFtdiDevice = new FTDI(); myFtdiDevice.GetNumberOfDevices( ref ftdiDeviceCount ); FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; myFtdiDevice.GetDeviceList( ftdiDeviceList ); myFtdiDevice.OpenBySerialNumber( ftdiDeviceList[0].SerialNumber ); myFtdiDevice.SetBaudRate( 9600 ); myFtdiDevice.SetDataCharacteristics( FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE ); myFtdiDevice.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13 ); myFtdiDevice.SetTimeouts( 5000, 0 ); richTextBox1.AppendText("polaczono"); }
static void Main(string[] args) { FTDI dev = new FTDI(); uint num_of_devices = 0; bool device_found = false; while (!device_found) { dev.GetNumberOfDevices(ref num_of_devices); if (num_of_devices > 0) { FTDI.FT_DEVICE_INFO_NODE[] devList = new FTDI.FT_DEVICE_INFO_NODE[num_of_devices]; dev.GetDeviceList(devList); foreach (FTDI.FT_DEVICE_INFO_NODE dev_info in devList) { if (dev_info.SerialNumber == bfmanager.kDevice_Descriptor) { device_found = true; break; } } if (device_found) { try { Application.Run(new MainForm(dev)); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } else { if (MessageBox.Show("Устройство не обнаружено. Подключите и нажмите ОК.", "Ошибка", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { break; } } } }
public List<FTDI.FT_DEVICE_INFO_NODE> GetDeviceList() { // 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); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (UInt32 i = 0; i < ftdiDeviceCount; i++) { Console.WriteLine("Device Index: " + i); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[i].Type); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber); Console.WriteLine("Description: " + ftdiDeviceList[i].Description); Console.WriteLine(""); } } return ftdiDeviceList.ToList(); }
private void AttemptConnect() { connected = false; UInt32 DeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class ftdi = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = ftdi.GetNumberOfDevices( ref DeviceCount ); // Check status if(ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0) { commStat = CommStatus.NoDevice; return; } commStat = CommStatus.NoElev8; // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount]; try { // Populate our device list ftStatus = ftdi.GetDeviceList( DeviceList ); bool FoundElev8 = false; for(int i = 0; i < DeviceCount && FoundElev8 == false; i++) { if(DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES) continue; ftStatus = ftdi.OpenBySerialNumber( DeviceList[i].SerialNumber ); if(ftStatus == FTDI.FT_STATUS.FT_OK) { string portName; ftdi.GetCOMPort( out portName ); if(portName == null || portName == "") { ftdi.Close(); continue; } ftdi.SetBaudRate( 115200 ); ftdi.SetDataCharacteristics( 8, 1, 0 ); ftdi.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0 ); txBuffer[0] = (byte)0; // Set it to MODE_None to stop it writing (reset in case it was disconnected) txBuffer[1] = (byte)0xff; // Send 0xff to the Prop to see if it replies uint written = 0; for(int j = 0; j < 10 && FoundElev8 == false; j++) // Keep pinging until it replies, or we give up { ftdi.Write( txBuffer, 2, ref written ); System.Threading.Thread.Sleep( 50 ); uint bytesAvail = 0; ftdi.GetRxBytesAvailable( ref bytesAvail ); // How much data is available from the serial port? if(bytesAvail > 0) { uint bytesRead = 0; ftdi.Read( rxBuffer, 1, ref bytesRead ); // If it comes back with 0xE8 it's the one we want if(bytesRead == 1 && rxBuffer[0] == 0xE8) { FoundElev8 = true; commStat = CommStatus.Connected; break; } } } if(FoundElev8) { connected = true; txBuffer[0] = 2; // MODE_Sensors written = 0; ftdi.Write( txBuffer, 1, ref written ); if(ConnectionStarted != null) { ConnectionStarted(); } break; } else { ftdi.Close(); } } } } catch(Exception) { return; } }
public void OpenByPID(uint pid) { uint locID = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; uint numDevices = CountDevices(); FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[numDevices]; try { ftStatus = ftdi.GetDeviceList(ftdiDeviceList); //the source of the mysterious buffering error! at long last! } catch (Exception e) { if (e.Message.Contains("supplied buffer is not big enough")) //retry. { System.Threading.Thread.Sleep(80); numDevices = CountDevices(); ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[numDevices]; ftStatus = ftdi.GetDeviceList(ftdiDeviceList); } else throw new Exception(e.Message + "7"); } if (ftStatus != FTDI.FT_STATUS.FT_OK) throw new Exception_Yellow("couldn't get device list; no device attached?"); if (pid != parameters.reference_radio_pid) { checkForOldFWPID(numDevices, ftdiDeviceList); //@@@@does this take extra time? checkForWrongPID(numDevices, ftdiDeviceList, parameters.PIDtoavoidopening); if (parameters.reprogramming_oldFW_HW3_SW2) pid = parameters.finalOldFWPID; } for (int k = 0; k < numDevices; k++) // look at all devices and get the LocID of the one which has the desired PID { if (ftdiDeviceList[k] != null) { //if (ftdiDeviceList[k].ID.ToString("X") == pid.ToString()) locID = ftdiDeviceList[k].LocId; // if the device has the specified Product ID, find its Location ID if (ftdiDeviceList[k].ID.ToString() == pid.ToString()) locID = ftdiDeviceList[k].LocId; // if the device has the specified Product ID, find its Location ID }// this only works if there is only one board with the specified PID } if (locID != 0) { if (FTDI.FT_STATUS.FT_OK != ftdi.OpenByLocation(locID)) //actually open the device, and if it doesn't work after many tries, fail. { //if (i > 70) throw new Exception_Yellow("Cannot find testing board. If an untested board is plugged in, unplug and plug in again. If error persists, board fails. Starting over:"); //database: cannot open at given locID (status of OpenByLocation was not OK). include pid so we know which device it was } if (FTDI.FT_STATUS.FT_OK != ftdi.SetBaudRate(Parameters.BaudRate_UARTandTesting) || FTDI.FT_STATUS.FT_OK != ftdi.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE) || FTDI.FT_STATUS.FT_OK != ftdi.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13)) { //if (ftdi.IsOpen) ftdi.Close(); //if (i > 70) throw new Exception_Yellow("Cannot connect to set FTDI port settings."); //database: cannot connect to device (status of OpenByLocation was not OK). include pid so we know which device it was } } }
public void Connect() { Logger.Log("CONNECTING USB GECKO."); ftStatus = FTDI.FT_STATUS.FT_OK; // determine the number of ftdi devices connected to the machine. uint ftdiDeviceCount = 0; ftStatus = this.ftdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); if (ftStatus == FTDI.FT_STATUS.FT_OK) { Logger.Log("Number of FTDI devices: " + ftdiDeviceCount.ToString()); } else { Logger.Log("FAILED to get number of devices (error " + ftStatus.ToString() + ")"); Disconnect(); return; } // if no devices available, return if (ftdiDeviceCount == 0) { Logger.Log("FAILED because no devices returned."); Disconnect(); return; } // allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // populate our device list ftStatus = this.ftdiDevice.GetDeviceList(ftdiDeviceList); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (uint i = 0; i < ftdiDeviceCount; i++) { Logger.Log("Device Index: " + i.ToString()); Logger.Log("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Logger.Log("Type: " + ftdiDeviceList[i].Type.ToString()); Logger.Log("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Logger.Log("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Logger.Log("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); Logger.Log("Description: " + ftdiDeviceList[i].Description.ToString()); } } else { Logger.Log("FAILED to get device list."); Disconnect(); return; } // open first device in our list by serial number ftStatus = this.ftdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber); if (ftStatus == FTDI.FT_STATUS.FT_OK) { Logger.Log("opened device with serial number " + ftdiDeviceList[0].SerialNumber); } else { Logger.Log("FAILED to open device (error " + ftStatus.ToString() + ")"); Disconnect(); return; } //// Set up device data parameters //// Set Baud rate to 9600 //ftStatus = myFtdiDevice.SetBaudRate(9600); //if (ftStatus != FTDI.FT_STATUS.FT_OK) //{ // // Wait for a key press // Logger.Log("Failed to set Baud rate (error " + ftStatus.ToString() + ")"); // Console.ReadKey(); // return; //} //// Set data characteristics - Data bits, Stop bits, Parity //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) //{ // // Wait for a key press // Logger.Log("Failed to set data characteristics (error " + ftStatus.ToString() + ")"); // Console.ReadKey(); // return; //} //// Set flow control - set RTS/CTS flow control //ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13); //if (ftStatus != FTDI.FT_STATUS.FT_OK) //{ // // Wait for a key press // Logger.Log("Failed to set flow control (error " + ftStatus.ToString() + ")"); // Console.ReadKey(); // return; //} // set read timeout to 1 seconds, write timeout to 1 second ftStatus = this.ftdiDevice.SetTimeouts(1000, 1000); if (ftStatus == FTDI.FT_STATUS.FT_OK) { Logger.Log("Set Timeouts to 5 seconds for reads and infinite for writes."); } else { Logger.Log("FAILED to set timeouts (error " + ftStatus.ToString() + ")"); Disconnect(); return; } // set latency timer to minimum of 2ms. byte latencyTimer = 2; ftStatus = this.ftdiDevice.SetLatency(latencyTimer); if (ftStatus == FTDI.FT_STATUS.FT_OK) { Logger.Log("Set latency timer to 2ms."); } else { Logger.Log("FAILED to set latency timer."); Disconnect(); return; } // set transfer rate from default of 4096 bytes to max 64k. uint transferSize = 65536; ftStatus = this.ftdiDevice.InTransferSize(transferSize); if (ftStatus == FTDI.FT_STATUS.FT_OK) { Logger.Log("Set transfer size to max 64kb."); } else { Logger.Log("FAILED to set transfer size"); Disconnect(); return; } // initialise usb gecko if (Initialize()) { this.isConnected = true; return; } else { Logger.Log("FAILED to initialize ftdi device."); Disconnect(); return; } }
private void createListDo(uint ftdiDeviceCount) { FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list ftStatus = ftdiDeviceWin.GetDeviceList(ftdiDeviceList); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (uint i = 0; i < ftdiDeviceCount; i++) { LogB.Information(String.Format("Device Index: " + i.ToString())); LogB.Information(String.Format("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags))); LogB.Information(String.Format("Type: " + ftdiDeviceList[i].Type.ToString())); LogB.Information(String.Format("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID))); LogB.Information(String.Format("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId))); LogB.Information(String.Format("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString())); LogB.Information(String.Format("Description: " + ftdiDeviceList[i].Description.ToString())); string port = getComPort(ftdiDeviceList[i]); ChronopicRegisterPort crp = new ChronopicRegisterPort(port); crp.FTDI = true; crp.SerialNumber = ftdiDeviceList[i].SerialNumber.ToString(); crp.Type = ChronopicRegisterPort.Types.UNKNOWN; LogB.Information(string.Format("crp: " + crp.ToString())); registerAddOrUpdate(crp); } } }
static void Main(string[] args) { UInt32 ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class FTDI myFtdiDevice = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus == FTDI.FT_STATUS.FT_OK) { Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString()); Console.WriteLine(""); } else { // Wait for a key press Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // If no devices available, return if (ftdiDeviceCount == 0) { // Wait for a key press Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // 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); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (UInt32 i = 0; i < ftdiDeviceCount; i++) { Console.WriteLine("Device Index: " + i.ToString()); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString()); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString()); Console.WriteLine(""); } } // Open first device in our list by serial number ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[1].SerialNumber); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Set up device data parameters // Set Baud rate to 9600 ftStatus = myFtdiDevice.SetBaudRate(9600); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to set Baud rate (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Set data characteristics - Data bits, Stop bits, Parity 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) { // Wait for a key press Console.WriteLine("Failed to set data characteristics (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Set flow control - set RTS/CTS flow control ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE,0,0); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to set flow control (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Set read timeout to 5 seconds, write timeout to infinite ftStatus = myFtdiDevice.SetTimeouts(5000, 0); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to set timeouts (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Perform loop back - make sure loop back connector is fitted to the device // Write string data to the device string dataToWrite = "Hello world!"; UInt32 numBytesWritten = 0; // Note that the Write method is overloaded, so can write string or byte array data ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten); myFtdiDevice.Purge(FTDI.FT_PURGE.FT_PURGE_TX); if (numBytesWritten != dataToWrite.Length) { Console.WriteLine("Error writting"); Console.Read(); return; } if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to write to device (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Check the amount of data available to read // In this case we know how much data we are expecting, // so wait until we have all of the bytes we have sent. UInt32 numBytesAvailable = 0; do { ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable); //Console.WriteLine(ftStatus + " " + numBytesAvailable); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } Thread.Sleep(10); } while (numBytesAvailable < dataToWrite.Length); // Now that we have the amount of data we want available, read it string readData; UInt32 numBytesRead = 0; // Note that the Read method is overloaded, so can read string or byte array data ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to read data (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } Console.WriteLine(readData); // Close our device ftStatus = myFtdiDevice.Close(); // Wait for a key press Console.WriteLine("Press any key to continue."); Console.ReadKey(); return; }
void ConnectFTDI() { UInt32 DeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class ftdi = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = ftdi.GetNumberOfDevices( ref DeviceCount ); // Check status if(ftStatus != FTDI.FT_STATUS.FT_OK || DeviceCount == 0) { commStat = CommStatus.NoDevices; return; } commStat = CommStatus.NoElev8; // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] DeviceList = new FTDI.FT_DEVICE_INFO_NODE[DeviceCount]; try { // Populate our device list ftStatus = ftdi.GetDeviceList( DeviceList ); bool FoundElev8 = false; for(int i = 0; i < DeviceCount && FoundElev8 == false; i++) { if(DeviceList[i].Type != FTDI.FT_DEVICE.FT_DEVICE_X_SERIES) continue; ftStatus = ftdi.OpenBySerialNumber( DeviceList[i].SerialNumber ); if(ftStatus == FTDI.FT_STATUS.FT_OK) { string portName; ftdi.GetCOMPort( out portName ); if(portName == null || portName == "") { ftdi.Close(); continue; } ftdi.SetBaudRate( 115200 ); ftdi.SetDataCharacteristics( 8, 1, 0 ); ftdi.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0 ); txBuffer[0] = (byte)0xff; // Send 0xff to the Prop to see if it replies uint written = 0; for(int j = 0; j < 10 && FoundElev8 == false; j++) // Keep pinging until it replies, or we give up { ftdi.Write( txBuffer, 1, ref written ); System.Threading.Thread.Sleep( 50 ); uint bytesAvail = 0; ftdi.GetRxBytesAvailable( ref bytesAvail ); // How much data is available from the serial port? if(bytesAvail > 0) { uint bytesRead = 0; ftdi.Read( rxBuffer, 1, ref bytesRead ); // If it comes back with 0xE8 it's the one we want if(bytesRead == 1 && rxBuffer[0] == 0xE8) { FoundElev8 = true; commStat = CommStatus.Connected; break; } } } if(FoundElev8) { break; } else { ftdi.Close(); } } } } catch(Exception) { return; } Active = true; if( ftdi.IsOpen ) { currentMode = (Mode)(tcMainTabs.SelectedIndex + 1); txBuffer[0] = (byte)currentMode; uint written = 0; ftdi.Write( txBuffer, 1, ref written ); // Which mode are we in? } // Start my 'tick timer' - It's set to tick every 20 milliseconds // (used to check the comm port periodically instead of using a thread) //tickTimer.Start(); }
public static TactonicDeviceList GetDeviceList() { Console.WriteLine("===================================================================="); Console.WriteLine("Copyright 2010-2013 Tactonics Technologies, LLC. All rights reserved"); Console.WriteLine("===================================================================="); deviceList = new TactonicDeviceList(); FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; UInt32 ftdiDeviceCount = 0; ftdi = new FTDI(); ftStatus = ftdi.GetNumberOfDevices(ref ftdiDeviceCount) | ftStatus; FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; ftStatus = ftdi.GetDeviceList(ftdiDeviceList) | ftStatus; tactonicUSBList = new TactonicUSB[ftdiDeviceCount]; deviceList.devices = new TactonicDevice[ftdiDeviceCount]; deviceList.numDevices = 0; for (int i = 0; i < ftdiDeviceCount; i++) { tactonicUSBList[deviceList.numDevices] = new TactonicUSB(ftdi); string desc = "Dual RS232-HS A"; if (desc.Equals(ftdiDeviceList[i].Description, StringComparison.OrdinalIgnoreCase)) { tactonicUSBList[deviceList.numDevices].isTiles = true; if (tactonicUSBList[deviceList.numDevices].OpenDevice(ftdiDeviceList[i])) { deviceList.devices[deviceList.numDevices] = new TactonicDevice(); tactonicUSBList[deviceList.numDevices].GetDeviceInfo(deviceList.devices[deviceList.numDevices]); Console.WriteLine("Initializing Device " + deviceList.devices[deviceList.numDevices].serialNumber); Console.WriteLine("Device Firmware " + tactonicUSBList[deviceList.numDevices].firmwareVersion[0] + "." + tactonicUSBList[deviceList.numDevices].firmwareVersion[1]); Console.WriteLine("Tactonic Library "+TactonicUSB.LIBRARY_VERSION_MAJOR+"."+TactonicUSB.LIBRARY_VERSION_MINOR); deviceList.numDevices++; } i++; } else { if (tactonicUSBList[deviceList.numDevices].OpenDevice(ftdiDeviceList[i])) { deviceList.devices[deviceList.numDevices] = new TactonicDevice(); tactonicUSBList[deviceList.numDevices].GetDeviceInfo(deviceList.devices[deviceList.numDevices]); Console.WriteLine("Initializing Device " + deviceList.devices[deviceList.numDevices].serialNumber); Console.WriteLine("Device Firmware " + tactonicUSBList[deviceList.numDevices].firmwareVersion[0] + "." + tactonicUSBList[deviceList.numDevices].firmwareVersion[1]); Console.WriteLine("Tactonic Library " + TactonicUSB.LIBRARY_VERSION_MAJOR + "." + TactonicUSB.LIBRARY_VERSION_MINOR); deviceList.numDevices++; } } } return deviceList; }
static void Main(string[] args) { string deviceSerialNumber = "33VRWQARA"; FTDI.FT_STATUS status = new FTDI.FT_STATUS(); FTDI device = new FTDI(); UInt32 numberOfDevices = 0; int sleepTime = 100; status = device.GetNumberOfDevices(ref numberOfDevices); FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[numberOfDevices]; status = device.GetDeviceList(devicelist); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("We have {0} devices", numberOfDevices); else Console.WriteLine("Failed to get number of devices"); status = device.OpenBySerialNumber(deviceSerialNumber); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("Device {0} is opened", deviceSerialNumber); else Console.WriteLine("Failed to open {0} device", deviceSerialNumber); status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("BitMode is resetted"); else Console.WriteLine("Failed to reset BitMode"); status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("BitMode is {0}", FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO); else Console.WriteLine("Failed to set BitMode"); byte latency = 2; device.SetLatency(latency); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("Latency timer value is {0}", latency); else Console.WriteLine("Failed to set latency"); uint inTransferSize = 0x10000; device.InTransferSize(inTransferSize); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("inTransferSize value is {0}", inTransferSize); else Console.WriteLine("Failed to set inTransferSize"); device.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("FlowControl is {0}", FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS); else Console.WriteLine("Failed to set FlowControl"); device.Purge(FTDI.FT_PURGE.FT_PURGE_RX); using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None)) { using (BinaryReader br = new BinaryReader(fs)) { int chunkLendth = 0x10000; byte[] chunk; uint numBytesWritten = 0; //uint TxQueue = 0; while (((chunk = br.ReadBytes(chunkLendth)).Length > 0) && (Console.KeyAvailable == false)) { //status = device.GetTxBytesWaiting(ref TxQueue); //if (status != FTDI.FT_STATUS.FT_OK) // Console.WriteLine("status!=ok"); //while (TxQueue != 0) // status = device.GetTxBytesWaiting(ref TxQueue); status = device.Write(chunk, chunk.Length, ref numBytesWritten); if (numBytesWritten != chunk.Length) Console.WriteLine("Error writting to the device,\r\nchunk.Length={0}\r\nnumBytesWritten={1}", chunk.Length, numBytesWritten); } } } Console.WriteLine("Key is pressed, end of file writting"); }
/// <summary> /// Returns the index of the first available FTDI 232H device found in the system /// </summary> /// <returns></returns> public int GetFirstDevIndex() { int count = 10; FTDI.FT_DEVICE_INFO_NODE[] devlist = new FTDI.FT_DEVICE_INFO_NODE[count]; FTDI.FT_STATUS status = _ftdi.GetDeviceList(devlist); Debug.Assert(status == FTDI.FT_STATUS.FT_OK, "Problem getting FTDI device list"); int index = -1; for (int i = 0; i < count; i++) { FTDI.FT_DEVICE_INFO_NODE devinfo = devlist[i]; if (devinfo != null) { if (devinfo.Type == FTD2XX_NET.FTDI.FT_DEVICE.FT_DEVICE_232H) { index = i; _dev_type = devinfo.Type; break; } } } return index; }
public string[] getDevicesNames() { uint nbDevices = 0; ftStatus = FtdiDevice.GetNumberOfDevices(ref nbDevices); if(nbDevices == 0) return null; else { // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[nbDevices]; // Populate our device list ftStatus = FtdiDevice.GetDeviceList(ftdiDeviceList); if(ftStatus==FTDI.FT_STATUS.FT_OK) { string[] deviceList = new string[nbDevices]; for (int i = 0; i < nbDevices; i++) deviceList[i] = ftdiDeviceList[i].Description.ToString(); return deviceList; } else return null; } }
public static FTDI.FT_DEVICE_INFO_NODE[] EnumFtdi() { FTDI d = new FTDI(); if (d.LoadLibrarySuccess) { uint n = 0; if (d.GetNumberOfDevices(ref n) == FTDI.FT_STATUS.FT_OK) { Console.WriteLine(n); } FTDI.FT_DEVICE_INFO_NODE[] list = new FTDI.FT_DEVICE_INFO_NODE[n]; d.GetDeviceList(list); return list; } return new FTDI.FT_DEVICE_INFO_NODE[0]; }
// public methods /// <summary> /// Attempt to open RHA2000-EVAL board connected to USB port. /// </summary> /// <param name="firmwareID1">Board ID number (1 of 3)</param> /// <param name="firmwareID2">Board ID number (2 of 3)</param> /// <param name="firmwareID3">Board ID number (3 of 3)</param> public void Open(ref int firmwareID1, ref int firmwareID2, ref int firmwareID3) { // Open FTDI USB device. UInt32 ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class. myFtdiDeviceA = new FTDI(); // Determine the number of FTDI devices connected to the machine. ftStatus = myFtdiDeviceA.GetNumberOfDevices(ref ftdiDeviceCount); // Check status. if (!(ftStatus == FTDI.FT_STATUS.FT_OK)) { UsbException e = new UsbException("USB Setup Error: Failed to get number devices"); throw e; } // If no devices available, return. if (ftdiDeviceCount == 0) { UsbException e = new UsbException("No valid USB devices detected"); throw e; } // Allocate storage for device info list. FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list. ftStatus = myFtdiDeviceA.GetDeviceList(ftdiDeviceList); // There could be a status error here, but we're not checking for it... // The Intan Technologies RHA2000-EVAL board uses an FTDI FT2232H chip to provide a USB // interface to a PC. Detailed information on this chip may be found at: // // http://www.ftdichip.com/Products/ICs/FT2232H.htm // // The FT2232H supports two independent FIFO channels. The channel used by the RHA2000-EVAL // board is factory-configured with the name "Intan I/O board 1.0 A". (FTDI provides software // routines to open device by its name.) ftStatus = myFtdiDeviceA.OpenByDescription("Intan I/O Board 1.0 A"); if (ftStatus != FTDI.FT_STATUS.FT_OK) { UsbException e = new UsbException("Intan USB device A not found"); throw e; } // Set read timeout to 5 seconds, write timeout to infinite ftStatus = myFtdiDeviceA.SetTimeouts(5000, 0); // There could be status error here, but we're not checking for it... this.Stop(); // Purge receive buffer myFtdiDeviceA.Purge(FTDI.FT_PURGE.FT_PURGE_RX); // Check board ID and version number // // The RHA2000-EVAL board is controlled by sending one-byte ASCII command characters over // the USB interface. The 'I' character commands the board to return a 3-byte ID/version // number. UInt32 numBytesWritten = 0; Byte[] myChars = { 73 }; // 'I' = request board ID and version number ftStatus = myFtdiDeviceA.Write(myChars, 1, ref numBytesWritten); if (ftStatus != FTDI.FT_STATUS.FT_OK) { UsbException e = new UsbException("Could not write to Intan USB device"); throw e; } const UInt32 numBytesToRead = 3; // Wait until the desired number of bytes have been received. UInt32 numBytesAvailable = 0; while (numBytesAvailable < numBytesToRead) { ftStatus = myFtdiDeviceA.GetRxBytesAvailable(ref numBytesAvailable); if (ftStatus != FTDI.FT_STATUS.FT_OK) { UsbException e = new UsbException("Failed to get number of USB bytes available to read"); throw e; } } // Now that we have the amount of data we want available, read it. UInt32 numBytesRead = 0; ftStatus = myFtdiDeviceA.Read(readDataBufferA, numBytesToRead, ref numBytesRead); if (ftStatus != FTDI.FT_STATUS.FT_OK) { UsbException e = new UsbException("USB read error"); throw e; } firmwareID1 = Convert.ToInt32(readDataBufferA[0]); firmwareID2 = Convert.ToInt32(readDataBufferA[1]); firmwareID3 = Convert.ToInt32(readDataBufferA[2]); Debug.WriteLine("Board ID: " + readDataBufferA[0] + " " + (Convert.ToInt32(readDataBufferA[1])).ToString() + " " + (Convert.ToInt32(readDataBufferA[2])).ToString()); this.ZCheckOff(); this.SettleOff(); // Purge receive buffer. myFtdiDeviceA.Purge(FTDI.FT_PURGE.FT_PURGE_RX); dataJustStarted = true; }
private void Open(FTDI port, string description) { uint deviceCount = 0; if (port == null) throw new Exception(description + "is not initialized"); if (port.IsOpen) { Global.ShowError(description + " is already open"); return; } // Determine the number of FTDI devices connected to the machine if ((ftStatus = port.GetNumberOfDevices(ref deviceCount)) != FTDI.FT_STATUS.FT_OK) { throw new FTDI.FT_EXCEPTION("Failed to get number of devices. err: " + ftStatus.ToString()); } // If no devices available, return if (deviceCount == 0) throw new Exception("No devices on " + description); // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[deviceCount]; // Populate our device list if ((ftStatus = port.GetDeviceList(ftdiDeviceList)) != FTDI.FT_STATUS.FT_OK) { throw new FTDI.FT_EXCEPTION("Failed to get device list. err " + ftStatus.ToString()); } int foundIndex = -1; for (int i = 0; i < deviceCount; i++) { if (ftdiDeviceList[i].Description.ToString().Contains(description)) { foundIndex = i; } /* Console.WriteLine("Device Index: " + i.ToString()); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString()); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString()); Console.WriteLine(""); */ } if (foundIndex < 0) { throw new Exception("Failed to find device with description " + description); } if ((ftStatus = port.OpenByIndex((uint) foundIndex)) != FTDI.FT_STATUS.FT_OK) { throw new FTDI.FT_EXCEPTION("Failed to open device. err: " + ftStatus.ToString()); } }
static void open(FTDI myFtdiDevice) { UInt32 ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Determine the number of FTDI devices connected to the machine ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus != FTDI.FT_STATUS.FT_OK) { return; } // If no devices available, return if (ftdiDeviceCount == 0) return; Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.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); int oIdx=-1; if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (Int32 i = 0; i < ftdiDeviceCount; i++) { if (ftdiDeviceList[i].Type == FTDI.FT_DEVICE.FT_DEVICE_232R) { oIdx = i; } } if (oIdx < 0) { Console.WriteLine("no matching device"); return; } } Console.WriteLine("Device Index: " + oIdx.ToString()); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[oIdx].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[oIdx].Type.ToString()); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[oIdx].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[oIdx].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[oIdx].SerialNumber.ToString()); Console.WriteLine("Description: " + ftdiDeviceList[oIdx].Description.ToString()); myFtdiDevice.SetDTR(false); myFtdiDevice.SetRTS(false); // Open first device in our list by serial number ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[oIdx].SerialNumber); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")"); //Console.ReadKey(); return; } myFtdiDevice.SetDTR(false); myFtdiDevice.SetRTS(false); // Set up device data parameters uint baud = 230400; if (ConfigurationManager.AppSettings["BaudRate"] != null) { baud = UInt32.Parse(ConfigurationManager.AppSettings["BaudRate"]); } ftStatus = myFtdiDevice.SetBaudRate(baud); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to set Baud rate (error " + ftStatus.ToString() + ")"); //Console.ReadKey(); return; } // Set data characteristics - Data bits, Stop bits, Parity 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) { // Wait for a key press Console.WriteLine("Failed to set data characteristics (error " + ftStatus.ToString() + ")"); //Console.ReadKey(); return; } // Set flow control - set RTS/CTS flow control ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x11, 0x13); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to set flow control (error " + ftStatus.ToString() + ")"); //Console.ReadKey(); return; } // times out at 100ms ftStatus = myFtdiDevice.SetTimeouts(100, 100); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to set timeouts (error " + ftStatus.ToString() + ")"); //Console.ReadKey(); return; } attention(); Console.WriteLine("CONNECTED. PRESS ANY KEY TO START."); Console.WriteLine("CTRL-R: RESET"); Console.WriteLine("CTRL-P: PROGRAM"); Console.WriteLine("CTRL-T: SET TIME"); prompt(); myFtdiDevice.SetDTR(true); Thread.Sleep(100); myFtdiDevice.SetDTR(false); while (true) { UInt32 numBytes = 0; byte[] buf = new byte[1024]; while (Console.KeyAvailable) { ConsoleKeyInfo cki = Console.ReadKey(true); //Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar); if ((cki.Modifiers & ConsoleModifiers.Control) != 0) { if (cki.Key.ToString().ToUpper().EndsWith("R")) { attention(); Console.WriteLine("\nRESET!"); prompt(); myFtdiDevice.SetRTS(false); myFtdiDevice.SetDTR(true); Thread.Sleep(100); myFtdiDevice.SetDTR(false); continue; } if (cki.Key.ToString().ToUpper().EndsWith("P")) { attention(); Console.WriteLine("\nPROGRAM MODE:"); myFtdiDevice.Close(); program(); string firmwareName = ConfigurationManager.AppSettings["FirmwareName"]; LpcProgrammer p = new LpcProgrammer(new StringOutDelegate(myStrDelegate)); //p.Prepare(); int r = p.Program(firmwareName); attention(); Console.WriteLine("\nRETURN VALUE: {0}", r); subdue(); throw new Exception("Leaving program mode"); } if (cki.Key.ToString().ToUpper().EndsWith("T")) { // Time: 10:11:0 // Date: 2827.5.9 string datePatt = @"yyyy.M.d"; string timePatt = @"HH:mm:ss"; DateTime dispDt = DateTime.Now; string dtString = "set_date " + dispDt.ToString(datePatt) + "\r"; string tmString = "set_time " + dispDt.ToString(timePatt) + "\r"; //Console.WriteLine(dtString); //Console.WriteLine(tmString); System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding(); Byte[] cmdBytes = ASCII.GetBytes(dtString); myFtdiDevice.Write(cmdBytes, cmdBytes.Length, ref numBytes); Thread.Sleep(200); cmdBytes = ASCII.GetBytes(tmString); myFtdiDevice.Write(cmdBytes, cmdBytes.Length, ref numBytes); continue; } } buf[0] = (byte)(cki.KeyChar & 0xff); numBytes = 0; ftStatus = myFtdiDevice.Write(buf, 1, ref numBytes); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new Exception("Failed to write to device (error " + ftStatus.ToString() + ")"); //return; } } numBytes = 0; ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytes); if (ftStatus != FTDI.FT_STATUS.FT_OK) { throw new Exception("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")"); //return; } if (numBytes > 0) { UInt32 numBytesRead = 0; byte[] buffer = new byte[1024]; // Note that the Read method is overloaded, so can read string or byte array data //ftStatus = myFtdiDevice.Read(out readData, 1024, ref numBytesRead); ftStatus = myFtdiDevice.Read(buffer, 1024, ref numBytesRead); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press throw new Exception("Failed to read data (error " + ftStatus.ToString() + ")"); //Console.ReadKey(); //return; } string readData = System.Text.Encoding.ASCII.GetString(buffer, 0, (int)numBytesRead); Console.Write(readData); } else { Thread.Sleep(10); } } }
private void button1_Click(object sender, EventArgs e) { //http://www.chd.at/blog/electronic/FTDI-in-CS //Note: don’t forget to connect the xDBUS1 with xDBUS2(!) = SDA Optifert.FTDI.Instance.Init(0); //Optifert.FTDI.Instance.close(); if (Optifert.FTDI.Instance.isConnected()) { Console.WriteLine("connected "); } else { Console.WriteLine("Not connected "); } Optifert.FTDI.Instance.close(); UInt32 ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class FTDI myFtdiDevice = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus == FTDI.FT_STATUS.FT_OK) { System.Diagnostics.Debug.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString()); System.Diagnostics.Debug.WriteLine(""); } else { // Wait for a key press System.Diagnostics.Debug.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")"); return; } // If no devices available, return if (ftdiDeviceCount == 0) { // Wait for a key press Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // 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); if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (UInt32 i = 0; i < ftdiDeviceCount; i++) { Console.WriteLine("Device Index: " + i.ToString()); Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString()); Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString()); Console.WriteLine(""); } } // Open first device in our list by serial number ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")"); Console.ReadKey(); return; } // Create our device EEPROM structure based on the type of device we have open if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_232H) { Console.WriteLine("is 232 H"); } myFtdiDevice.Close(); }
static void Main(string[] args) { string deviceSerialNumber = "33VRWQARA"; FTDI.FT_STATUS status = new FTDI.FT_STATUS(); FTDI device = new FTDI(); UInt32 numberOfDevices = 0; int sleepTime = 100; status = device.GetNumberOfDevices(ref numberOfDevices); FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[numberOfDevices]; status = device.GetDeviceList(devicelist); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("We have {0} devices", numberOfDevices); else Console.WriteLine("Failed to get number of devices"); status = device.OpenBySerialNumber(deviceSerialNumber); Thread.Sleep(sleepTime); if(status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("Device {0} is opened", deviceSerialNumber); else Console.WriteLine("Failed to open {0} device", deviceSerialNumber); status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("BitMode is resetted"); else Console.WriteLine("Failed to reset BitMode"); status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("BitMode is {0}", FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO); else Console.WriteLine("Failed to set BitMode"); byte latency = 2; device.SetLatency(latency); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("Latency timer value is {0}", latency); else Console.WriteLine("Failed to set latency"); uint inTransferSize = 0x10000; device.InTransferSize(inTransferSize); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("inTransferSize value is {0}", inTransferSize); else Console.WriteLine("Failed to set inTransferSize"); device.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00); Thread.Sleep(sleepTime); if (status == FTDI.FT_STATUS.FT_OK) Console.WriteLine("FlowControl is {0}", FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS); else Console.WriteLine("Failed to set FlowControl"); device.Purge(FTDI.FT_PURGE.FT_PURGE_RX); uint numBytes = 0; //int cycles = 0; using(FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { using (BinaryWriter bw = new BinaryWriter(fs)) { while (Console.KeyAvailable == false) { device.GetRxBytesAvailable(ref numBytes); if (numBytes >= 1) { //cycles++; byte[] bBuf = new byte[numBytes]; device.Read(bBuf, numBytes, ref numBytes); bw.Write(bBuf); //if (cycles == 1) //{ // cycles = 0; // Console.WriteLine("{0}", bBuf.Length); //} } if (numBytes >= 0x10000) { Console.WriteLine("Buffer overload!"); } } //Console.WriteLine("Press 'p' to erase control bytes, 'q' to quite"); //ConsoleKeyInfo cki = Console.ReadKey(false); //if (cki.Key == ConsoleKey.Q) // Environment.Exit(-1); //if (cki.Key == ConsoleKey.P) //{ //} } } Console.WriteLine("Key is pressed, end of file writting"); }
static String GetQuadCamSerial(UInt32 ftdiDeviceCount) { // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list ftStatus = Program.ftdiDevice.GetDeviceList(ftdiDeviceList); if ( ftStatus == FTDI.FT_STATUS.FT_OK ) { for ( UInt32 i = 0; i < ftdiDeviceCount; i++ ) { if ( ftdiDeviceList[i] != null ) { if ( ftdiDeviceList[i].Description.ToString() == "QuadCam" ) { return ftdiDeviceList[i].SerialNumber.ToString(); } } } } return ""; }
public SetupDialogForm() { InitializeComponent(); UInt32 ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class FTDI tempFtdiDevice = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = tempFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus == FTDI.FT_STATUS.FT_OK) AvailableDevicesListBox.Items.Add("# of FTDI devices = " + ftdiDeviceCount.ToString()); else throw new ASCOM.InvalidValueException("Error getting count FTDI devices"); if (ftdiDeviceCount > 0) { // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list ftStatus = tempFtdiDevice.GetDeviceList(ftdiDeviceList); //Show device properties if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (UInt32 i = 0; i < ftdiDeviceCount; i++) { if (ftdiDeviceList[i].SerialNumber.Contains("CAM8")) { AvailableDevicesListBox.Items.Add("Device Index: " + i.ToString()); AvailableDevicesListBox.Items.Add("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); AvailableDevicesListBox.Items.Add("Type: " + ftdiDeviceList[i].Type.ToString()); AvailableDevicesListBox.Items.Add("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); AvailableDevicesListBox.Items.Add("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); AvailableDevicesListBox.Items.Add("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); AvailableDevicesListBox.Items.Add("Description: " + ftdiDeviceList[i].Description.ToString()); AvailableDevicesListBox.Items.Add(""); } } } else throw new ASCOM.InvalidValueException("Error getting parameters from FTDI devices"); } //Close device ftStatus = tempFtdiDevice.Close(); // Initialise current values of user settings from the ASCOM Profile chkTrace.Checked = Camera.traceState; coolerCheckBox.Checked = Camera.coolerEnabledState; //find available com ports in system string[] comPorts; comPorts = SerialPort.GetPortNames(); int j; for (j = 0; j < comPorts.Length; j++) { coolerComPortComboBox.Items.Add(comPorts[j]); if (comPorts[j] == Camera.coolerComPortState) coolerComPortComboBox.SelectedIndex = j; } }
public SetupDialogForm() { InitializeComponent(); //Initialize components; UInt32 ftdiDeviceCount = 0; FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK; // Create new instance of the FTDI device class FTDI tempFtdiDevice = new FTDI(); // Determine the number of FTDI devices connected to the machine ftStatus = tempFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); // Check status if (ftStatus == FTDI.FT_STATUS.FT_OK) AvailableDevicesListBox.Items.Add("# of FTDI devices = " + ftdiDeviceCount.ToString()); else throw new ASCOM.InvalidValueException("Error getting count FTDI devices"); if (ftdiDeviceCount > 0) { // Allocate storage for device info list FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; // Populate our device list ftStatus = tempFtdiDevice.GetDeviceList(ftdiDeviceList); //Show device properties if (ftStatus == FTDI.FT_STATUS.FT_OK) { for (UInt32 i = 0; i < ftdiDeviceCount; i++) { AvailableDevicesListBox.Items.Add("Device Index: " + i.ToString()); AvailableDevicesListBox.Items.Add("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags)); AvailableDevicesListBox.Items.Add("Type: " + ftdiDeviceList[i].Type.ToString()); AvailableDevicesListBox.Items.Add("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID)); AvailableDevicesListBox.Items.Add("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId)); AvailableDevicesListBox.Items.Add("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString()); AvailableDevicesListBox.Items.Add("Description: " + ftdiDeviceList[i].Description.ToString()); AvailableDevicesListBox.Items.Add(""); } } else throw new ASCOM.InvalidValueException("Error getting parameters from FTDI devices"); } //Close device ftStatus = tempFtdiDevice.Close(); }