public Purge ( |
||
purgemask | Specifies which buffer(s) to be purged. Valid values are any combination of the following flags: FT_PURGE_RX, FT_PURGE_TX | |
return | FT_STATUS |
protected bool OpenFTDIDevice(FTDI.FT_DEVICE_INFO_NODE deviceToOpen) { bool connected = false; lock (mFTDIDevice) { if (IsFTDIDeviceOpen()) { CloseFTDIDevice(); } if (FTDI.IsFTD2XXDLLLoaded()) { if (deviceToOpen != null) { if (mFTDIDevice.OpenByLocation(deviceToOpen.LocId) == FTDI.FT_STATUS.FT_OK) { FTDI.FT_STATUS ftdiStatus = FTDI.FT_STATUS.FT_OK; ftdiStatus |= mFTDIDevice.ResetDevice(); //ftdiStatus |= mFTDIDevice.ResetPort(); ftdiStatus |= mFTDIDevice.Purge(FTDI.FT_PURGE.FT_PURGE_RX | FTDI.FT_PURGE.FT_PURGE_TX); if (ftdiStatus == FTDI.FT_STATUS.FT_OK) { DisplayStatusMessage("Opened FTDI device.", StatusMessageType.LOG); DisplayStatusMessage("FTDI device info - Description: " + deviceToOpen.Description + " Serial Number: " + deviceToOpen.SerialNumber + " Device Type: " + deviceToOpen.Type + " ID: 0x" + deviceToOpen.ID.ToString("X") + " Device Flags: 0x" + deviceToOpen.Flags.ToString("X"), StatusMessageType.LOG); if (FTDI.IsFTDChipIDDLLLoaded()) { DisplayStatusMessage("FTDI ChipID DLL is loaded, checking chip ID...", StatusMessageType.LOG); uint chipID = 0; if (mFTDIDevice.GetChipIDFromCurrentDevice(out chipID) == FTDI.FT_STATUS.FT_OK) { DisplayStatusMessage("FTDI device chip ID: 0x" + chipID.ToString("X"), StatusMessageType.LOG); } else { DisplayStatusMessage("Unable to read FTDI device chip ID", StatusMessageType.LOG); } } connected = true; } else { mFTDIDevice.Close(); } } else { DisplayStatusMessage("Could not open FTDI device", StatusMessageType.LOG); } } else { DisplayStatusMessage("No FTDI device selected", StatusMessageType.LOG); } } else { DisplayStatusMessage("FTDI FTD2XX device driver DLL could not be loaded.", StatusMessageType.USER); } } return(connected); }
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 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 Read() { byte[] packetStart = new byte[2]; byte[] packetBody = new byte[10]; uint read = 0; centralBoardCom.Read(packetStart, 2, ref read); if (packetStart[0] == 0xff && packetStart[1] == 0xff) //Start of packet detected { centralBoardCom.Read(packetBody, 10, ref read); int sum = 0; for (int i = 0; i < 9; i++) { sum += packetBody[i]; } if ((byte)(sum & 0xff) != packetBody[9]) { return; } byte IRInfo = packetBody[8]; Monitor.Enter(BottomSensorValue); BottomSensorValue[0] = ((IRInfo & 1) == 1 ? true : false); BottomSensorValue[1] = (((IRInfo >> 1) & 1) == 1 ? true : false); Monitor.Exit(BottomSensorValue); int IRChannel0Num = (IRInfo >> 2) & 7; int IRChannel1Num = (IRInfo >> 5) & 7; Monitor.Enter(IRSensorValue); IRSensorValue[IRChannel0Num] = (ushort)((packetBody[1] << 8) | packetBody[0]); IRSensorValue[IRChannel1Num] = (ushort)((packetBody[3] << 8) | packetBody[2]); Monitor.Exit(IRSensorValue); if (packetBody[4] == 0xFE && packetBody[5] == 0xFE) { Monitor.Enter(LaserError); LaserError[0] = true; Monitor.Exit(LaserError); Monitor.Enter(LaserValue); LaserValue[0] = 0; Monitor.Exit(LaserValue); } else { Monitor.Enter(LaserError); LaserError[0] = false; Monitor.Exit(LaserError); Monitor.Enter(LaserValue); LaserValue[0] = (ushort)((packetBody[5] << 8) | packetBody[4]); Monitor.Exit(LaserValue); } if (packetBody[6] == 0xFE && packetBody[7] == 0xFE) { Monitor.Enter(LaserError); LaserError[1] = true; Monitor.Exit(LaserError); Monitor.Enter(LaserValue); LaserValue[1] = 0; Monitor.Exit(LaserValue); } else { Monitor.Enter(LaserError); LaserError[1] = false; Monitor.Exit(LaserError); Monitor.Enter(LaserValue); LaserValue[1] = (ushort)((packetBody[7] << 8) | packetBody[6]); Monitor.Exit(LaserValue); } } else { centralBoardCom.Purge(FTDI.FT_PURGE.FT_PURGE_RX); } }
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; }