public void Open() { Console.WriteLine("Open Port"); FT_STATUS status; status = _Ftdi.OpenBySerialNumber(_PortName); if (status == FT_STATUS.FT_OK) { status = _Ftdi.SetLatency(4); } if (status == FT_STATUS.FT_OK) { status = _Ftdi.SetBaudRate(3000000); } if (status == FT_STATUS.FT_OK) { status = _Ftdi.InTransferSize(64); } if (status == FT_STATUS.FT_OK) { status = _Ftdi.SetEventNotification(FT_EVENTS.FT_EVENT_RXCHAR, _WaitHandle); } if (status != FT_STATUS.FT_OK) { throw new Exception("Error opening device " + _PortName); } }
/// <summary> /// initialize device /// </summary> /// <returns></returns> public bool Init() { FTDI.FT_STATUS ftStatus = mDevice.ResetDevice(); ftStatus |= mDevice.GetRxBytesAvailable(ref dwNumInputBuffer);// Get number of bytes in the input buffer if ((ftStatus == FTDI.FT_STATUS.FT_OK) && (dwNumInputBuffer > 0)) { mDevice.Read(InputBuffer, dwNumInputBuffer, ref dwNumBytesRead); } ftStatus |= mDevice.InTransferSize(65536); // Set USB request transfer sizes ftStatus |= mDevice.SetCharacters(0, false, 0, false); // Disable event and error characters ftStatus |= mDevice.SetTimeouts(5000, 5000); // Set the read and write timeouts to 5 seconds ftStatus |= mDevice.SetLatency(16); // Keep the latency timer at default of 16ms ftStatus |= mDevice.SetBitMode(0x0, 0x00); // Reset the mode to whatever is set in EEPROM ftStatus |= mDevice.SetBitMode(0x0, 0x02); // Enable MPSSE mode // Inform the user if any errors were encountered if (ftStatus != FT_OK) { return(false); } Sleep(50); SynchroniseMPSSEbyAA(); SynchroniseMPSSEbyAB(); ConfigMPSSE_Setting(); ConfigMPSSE_IO_Pins(); return(ftStatus == FTDI.FT_STATUS.FT_OK); }
// 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); }
private void SetFifoMode() { FTDI.FT_STATUS status; //status = ftHandle.OpenBySerialNumber(serialNumber); status = ftHandle.OpenByIndex(0); if (status != FTDI.FT_STATUS.FT_OK) { MessageBox.Show("Cannot open device"); return; } ftHandle.SetBitMode(0xff, 0x00); System.Threading.Thread.Sleep(10); status = ftHandle.SetBitMode(0xff, 0x40); if (status != FTDI.FT_STATUS.FT_OK) { MessageBox.Show("Cannot set status"); return; } ftHandle.SetLatency(16); ftHandle.InTransferSize(0x10000); ftHandle.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0, 0); }
public void Init(int index) { _ftdi = new FTDI(); FTDI.FT_STATUS status = _ftdi.OpenByIndex((uint)index); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem opening FTDI device"); } status = _ftdi.ResetDevice(); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem resetting FTDI device"); } // Purge USB receive buffer first by reading out all old data from FT2232H receive buffer status = _ftdi.Purge(FTDI.FT_PURGE.FT_PURGE_RX | FTDI.FT_PURGE.FT_PURGE_TX); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem purging FTDI device"); } //Set USB request transfer size status = _ftdi.InTransferSize(USB_TRANSFERE_SIZE); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem setting USB transfer size"); } //Disable event and error characters status = _ftdi.SetCharacters(0, false, 0, false); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem disabling event and error characters"); } //Sets the read and write timeouts in milliseconds for the FT2232H status = _ftdi.SetTimeouts(5000, 5000); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem setting timeouts"); } //Set the latency timer status = _ftdi.SetLatency(16); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem setting latency timer"); } //Reset controller status = _ftdi.SetBitMode(0x0, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem resetting controller"); } // Set IO to only drive on 0 List <byte> buffer = new List <byte>(); buffer.Add(SET_IO_TO_ONLY_DRIVE_ON_0); buffer.Add(0xFF); buffer.Add(0xFF); rawWrite(buffer.ToArray()); // Send Command // enable MPSEE mode status = _ftdi.SetBitMode(0x0, FTDI.FT_BIT_MODES.FT_BIT_MODE_MPSSE); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem enabling MPSEE mode"); } /// Configure the MPSSE settings buffer = new List <byte>(); buffer.Add(DISABLE_CLOCK_DIVISOR); // Disables the clk divide by 5 to allow for a 60MHz master clock. buffer.Add(0x97); // Ensure adaptive clocking is off buffer.Add(0x8C); // Enables 3 phase data clocking. data VALID on both edges. rawWrite(buffer.ToArray()); // Send Command /* Low Byte (ADBUS) * ADBUS0 TCK/SK ---> SCL * ADBUS1 TDI/DO -+-> SDA * ADBUS2 TDO/DI -+ * ADBUS3 TMS/CS * ADBUSS GPIOL0 * ADBUS5 GPIOL1 * ADBUS6 GPIOl2 * ADBUS7 GPIOL3 */ //Command to set ADBUS state and direction //Set SDA high, SCL high //Set SDA, SCL as output with bit 1, other pins as input with bit 0 buffer.AddRange(formSetDataBits(BUS.ADBUS, 0x03, 0x03)); rawWrite(buffer.ToArray()); // Send Command // Set clock divisor buffer = new List <byte>(); buffer.Add(SET_TCK_SK_CLOCK_DIVISOR); //TCK/SK period = 12MHz*5 / ( (1 + (0xValueH << 8) | 0xValueL) ) * 2) //double frequency = (60E6) / ( (1+ ((0x00 << 8) | 0xCB)) * 2/3 ); UInt16 dwClockDivisor = 0x00CB; //(0x012B/3) =~ 100 KHz buffer.Add((byte)(dwClockDivisor & 0xFF)); // low byte buffer.Add((byte)((dwClockDivisor >> 8) & 0xFF)); // high byte rawWrite(buffer.ToArray()); // sent of commands // Turn off Loopback buffer = new List <byte>(); //buffer.Add(TURN_OFF_LOOPBACK);//Command to turn off loop back of TDI/TDO connection rawWrite(buffer.ToArray());// sent of commands }
public USBCntrl(string name, string serial) { this.Name = name; cts = new CancellationTokenSource(); tkn = cts.Token; rxTsk = Task.Run(async() => await ShowRxData(), tkn); txTsk = Task.Run(async() => await SendTxData(), tkn); do { // Open first device by serial number FTDI.FT_STATUS ftStatus = myFtdiDevice.OpenBySerialNumber(Serial); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to open device:" + Name + " (error " + ftStatus.ToString() + ")"); break; } ftStatus = myFtdiDevice.Purge(FTDI.FT_PURGE.FT_PURGE_RX | FTDI.FT_PURGE.FT_PURGE_TX); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to purge device:" + Name + " (error " + ftStatus.ToString() + ")"); break; } ftStatus = myFtdiDevice.InTransferSize(64); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to set InTransferSize device:" + Name + " (error " + ftStatus.ToString() + ")"); break; } Console.WriteLine("Set to set timeouts"); // 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 logger.Error("Failed to set timeouts:" + Name + " (error " + ftStatus.ToString() + ")"); break; } Console.WriteLine("clear the data"); UInt32 numBytesAvailable = 0; ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to get number of bytes available to read:" + Name + " (error " + ftStatus.ToString() + ")"); break; } if (numBytesAvailable > 0) { byte[] readData = new byte[numBytesAvailable]; UInt32 numBytesRead = 0; ftStatus = myFtdiDevice.Read(readData, numBytesAvailable, ref numBytesRead); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to read data:" + Name + " (error " + ftStatus.ToString() + ")"); break; } for (int i = 0; i < numBytesRead; i++) { Console.Write(readData[i].ToString("x") + " "); } } byte[] dataToWrite = { // command sequence length chksum (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, //Resync (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // (byte)0x84, (byte)0x00, (byte)0x01, (byte)0xff, //Stop (byte)0x81, (byte)0x00, (byte)0x02, (byte)0xff, //Set time (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, // <the time> (byte)0x85, (byte)0x00, (byte)0x01, (byte)0xff, //ReSeq (byte)0x82, (byte)0x00, (byte)0x01, (byte)0xff }; //Run uint numBytesWritten = 0; ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten); state = 0; } while (false); for (int ndx = 0; ndx < bufrPool.BoundedCapacity; ndx++) { bufrPool.Add(new USBMsg()); } logger.Info("Waiting for tasks to complete."); cts.CancelAfter(1000); Task.WhenAll(new[] { txTsk, rxTsk }); logger.Error("Done."); }
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"); }
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"); }
public USBCntrl(XElement el) { Name = (string)el.Attribute("nm"); Serial = (string)el.Attribute("serial"); strands.AddRange(from strnd in el.Elements("Strand") select new GECEStrand(strnd, this)); if (!Global.Instance.Settings.BypassUSBControllers) { myFtdiDevice = new FTDI(); do { // Open first device by serial number FTDI.FT_STATUS ftStatus = myFtdiDevice.OpenBySerialNumber(Serial); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to open device:" + Name + " (error " + ftStatus.ToString() + ")"); break; } ftStatus = myFtdiDevice.Purge(FTDI.FT_PURGE.FT_PURGE_RX + FTDI.FT_PURGE.FT_PURGE_TX); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to purge device:" + Name + " (error " + ftStatus.ToString() + ")"); break; } ftStatus = myFtdiDevice.InTransferSize(64); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to set InTransferSize device:" + Name + " (error " + ftStatus.ToString() + ")"); break; } Console.WriteLine("Set to set timeouts"); // 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 logger.Error("Failed to set timeouts:" + Name + " (error " + ftStatus.ToString() + ")"); break; } Console.WriteLine("clear the data"); UInt32 numBytesAvailable = 0; ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to get number of bytes available to read:" + Name + " (error " + ftStatus.ToString() + ")"); break; } if (numBytesAvailable > 0) { byte[] readData = new byte[numBytesAvailable]; UInt32 numBytesRead = 0; ftStatus = myFtdiDevice.Read(readData, numBytesAvailable, ref numBytesRead); if (ftStatus != FTDI.FT_STATUS.FT_OK) { // Wait for a key press logger.Error("Failed to read data:" + Name + " (error " + ftStatus.ToString() + ")"); break; } for (int i = 0; i < numBytesRead; i++) { Console.Write(readData[i].ToString("x") + " "); } } byte[] dataToWrite = { // command sequence length chksum (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, //Resync (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // (byte)0x84, (byte)0x00, (byte)0x01, (byte)0xff, //Stop (byte)0x81, (byte)0x00, (byte)0x02, (byte)0xff, //Set time (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, // <the time> (byte)0x85, (byte)0x00, (byte)0x01, (byte)0xff, //ReSeq (byte)0x82, (byte)0x00, (byte)0x01, (byte)0xff }; //Run uint numBytesWritten = 0; ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten); cts = new CancellationTokenSource(); tkn = cts.Token; rxTsk = Task.Factory.StartNew(() => ShowRxData(), tkn); txTsk = Task.Factory.StartNew(() => SendTxData(), tkn); state = 0; } while (false); } for (int ndx = 0; ndx < bufrPool.BoundedCapacity; ndx++) { bufrPool.Add(new USBMsg()); } }