FTD2XX_NET.FTDI.WriteFT232REEPROM C# (CSharp) Method

WriteFT232REEPROM() public method

Writes the specified values to the EEPROM of an FT232R or FT245R device. Calls FT_EE_Program in FTD2XX DLL
If the strings are too long, they will be truncated to their maximum permitted lengths
Thrown when the current device does not match the type required by this method.
public WriteFT232REEPROM ( FT232R_EEPROM_STRUCTURE ee232r ) : FT_STATUS
ee232r FT232R_EEPROM_STRUCTURE The EEPROM settings to be written to the device
return FT_STATUS
        public FT_STATUS WriteFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r)
        {
            // Initialise ftStatus to something other than FT_OK
            FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
            FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;

            // If the DLL hasn't been loaded, just return here
            if (hFTD2XXDLL == IntPtr.Zero)
                return ftStatus;

            // Check for our required function pointers being set up
            if (pFT_EE_Program != IntPtr.Zero)
            {
                tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_Program));

                if (ftHandle != IntPtr.Zero)
                {
                    FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
                    // Check that it is an FT232R or FT245R that we are trying to write
                    GetDeviceType(ref DeviceType);
                    if (DeviceType != FT_DEVICE.FT_DEVICE_232R)
                    {
                        // If it is not, throw an exception
                        ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
                        ErrorHandler(ftStatus, ftErrorCondition);
                    }

                    // Check for VID and PID of 0x0000
                    if ((ee232r.VendorID == 0x0000) | (ee232r.ProductID == 0x0000))
                    {
                        // Do not allow users to program the device with VID or PID of 0x0000
                        return FT_STATUS.FT_INVALID_PARAMETER;
                    }

                    FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();

                    // Set up structure headers
                    eedata.Signature1 = 0x00000000;
                    eedata.Signature2 = 0xFFFFFFFF;
                    eedata.Version = 2;

                    // Allocate space from unmanaged heap
                    eedata.Manufacturer = Marshal.AllocHGlobal(32);
                    eedata.ManufacturerID = Marshal.AllocHGlobal(16);
                    eedata.Description = Marshal.AllocHGlobal(64);
                    eedata.SerialNumber = Marshal.AllocHGlobal(16);

                    // Check lengths of strings to make sure that they are within our limits
                    // If not, trim them to make them our maximum length
                    if (ee232r.Manufacturer.Length > 32)
                        ee232r.Manufacturer = ee232r.Manufacturer.Substring(0, 32);
                    if (ee232r.ManufacturerID.Length > 16)
                        ee232r.ManufacturerID = ee232r.ManufacturerID.Substring(0, 16);
                    if (ee232r.Description.Length > 64)
                        ee232r.Description = ee232r.Description.Substring(0, 64);
                    if (ee232r.SerialNumber.Length > 16)
                        ee232r.SerialNumber = ee232r.SerialNumber.Substring(0, 16);

                    // Set string values
                    eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232r.Manufacturer);
                    eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232r.ManufacturerID);
                    eedata.Description = Marshal.StringToHGlobalAnsi(ee232r.Description);
                    eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232r.SerialNumber);

                    // Map non-string elements to structure
                    // Standard elements
                    eedata.VendorID = ee232r.VendorID;
                    eedata.ProductID = ee232r.ProductID;
                    eedata.MaxPower = ee232r.MaxPower;
                    eedata.SelfPowered = Convert.ToUInt16(ee232r.SelfPowered);
                    eedata.RemoteWakeup = Convert.ToUInt16(ee232r.RemoteWakeup);
                    // 232R specific fields
                    eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable);
                    eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable);
                    eedata.UseExtOsc = Convert.ToByte(ee232r.UseExtOsc);
                    eedata.HighDriveIOs = Convert.ToByte(ee232r.HighDriveIOs);
                    // Override any endpoint size the user has selected and force 64 bytes
                    // Some users have been known to wreck devices by setting 0 here...
                    eedata.EndpointSize = 64;
                    eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable);
                    eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable);
                    eedata.InvertTXD = Convert.ToByte(ee232r.InvertTXD);
                    eedata.InvertRXD = Convert.ToByte(ee232r.InvertRXD);
                    eedata.InvertRTS = Convert.ToByte(ee232r.InvertRTS);
                    eedata.InvertCTS = Convert.ToByte(ee232r.InvertCTS);
                    eedata.InvertDTR = Convert.ToByte(ee232r.InvertDTR);
                    eedata.InvertDSR = Convert.ToByte(ee232r.InvertDSR);
                    eedata.InvertDCD = Convert.ToByte(ee232r.InvertDCD);
                    eedata.InvertRI = Convert.ToByte(ee232r.InvertRI);
                    eedata.Cbus0 = ee232r.Cbus0;
                    eedata.Cbus1 = ee232r.Cbus1;
                    eedata.Cbus2 = ee232r.Cbus2;
                    eedata.Cbus3 = ee232r.Cbus3;
                    eedata.Cbus4 = ee232r.Cbus4;
                    eedata.RIsD2XX = Convert.ToByte(ee232r.RIsD2XX);

                    // Call FT_EE_Program
                    ftStatus = FT_EE_Program(ftHandle, eedata);

                    // Free unmanaged buffers
                    Marshal.FreeHGlobal(eedata.Manufacturer);
                    Marshal.FreeHGlobal(eedata.ManufacturerID);
                    Marshal.FreeHGlobal(eedata.Description);
                    Marshal.FreeHGlobal(eedata.SerialNumber);
                }
            }
            else
            {
                if (pFT_EE_Program == IntPtr.Zero)
                {
                    LogB.Debug("FTD2XX: Failed to load function FT_EE_Program.");
                }
            }
            return ftStatus;
        }

Usage Example

コード例 #1
0
 /// <summary>
 /// Take the previously read EEPROM_STRUCTURE 
 /// and write it back to the FT EEPROM
 /// after elsewhere checking and modifying the 
 /// product description if necessary
 /// </summary>
 /// <param name="dev"></param>
 /// <returns></returns>
 public FTDI.FT_STATUS writeFtdiEEProm(FTDI dev)
 {
     FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
     try
     {
         ftStatus = dev.WriteFT232REEPROM(ftEeprom);
     }
     catch (FTDI.FT_EXCEPTION ex)
     {
         throw ex;
     }
     return ftStatus;
 }