All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.jbidibc.ftdi.FtdiDeviceManipulator Maven / Gradle / Ivy

There is a newer version: 2.0.25
Show newest version
package org.bidib.jbidibc.ftdi;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ftdi.EEPROMData;
import com.ftdi.FTDevice;

public class FtdiDeviceManipulator {

    private static final Logger LOGGER = LoggerFactory.getLogger(FtdiDeviceManipulator.class);

    public List loadDevices() {

        List devices = new ArrayList<>();

        try {
            LOGGER.info("Load the FTDevices in the system.");
            List ftDevices = FTDevice.getDevices(true);
            if (ftDevices != null) {
                for (FTDevice device : ftDevices) {
                    LOGGER.info("Current device: {}", device);

                    try {
                        device.open();
                        int userAreaSize = device.getEEPROMUserAreaSize();
                        EEPROMData eepromData = device.readEEPROM();
                        LOGGER.info("userAreaSize: {}, eepromData: {}", userAreaSize, eepromData);
                        // byte[] userAreaData = device.readEEPROMUserArea(50);
                        // String userAreaDataString = device.readFullEEPROMUserAreaAsString();
                        // LOGGER.info("userAreaSize: {}, userAreaData: {}", userAreaSize, userAreaData);

                        LOGGER.info("manufacturer: {}", eepromData.getManufacturer());
                        LOGGER.info("manufacturerID: {}", eepromData.getManufacturerID());
                        LOGGER.info(String.format("vendorID: 0x%04x", eepromData.getVendorId()));
                        LOGGER.info(String.format("productID: 0x%04x", eepromData.getProductId()));
                        LOGGER.info("description: {}", eepromData.getDescription());
                        LOGGER.info("serialNumber: {}", eepromData.getSerialNumber());
                        LOGGER.info("maxPower: {}", eepromData.getMaxPower());
                        LOGGER.info("PnP: {}", eepromData.isPnP());
                        LOGGER.info("selfPowered: {}", eepromData.isSelfPowered());
                        LOGGER.info("remoteWakeup: {}", eepromData.isRemoteWakeup());

                        FtdiDeviceData ftdiDeviceData = new FtdiDeviceData();
                        ftdiDeviceData.setVendorId(eepromData.getVendorId());
                        ftdiDeviceData.setProductId(eepromData.getProductId());
                        ftdiDeviceData.setSerialNumber(eepromData.getSerialNumber());
                        ftdiDeviceData.setDescription(eepromData.getDescription());

                        devices.add(ftdiDeviceData);

                        // eepromData.setDescription("BiDiB-IF2");
                        // LOGGER.info("Change the description: {}", eepromData.getDescription());
                        //
                        // device.writeEEPROM(eepromData);
                        //
                        // device.cyclePort();
                    }
                    finally {
                        device.close();
                    }
                }
            }

        }
        catch (Exception ex) {
            LOGGER.warn("List FTDI devices failed.", ex);
        }

        return devices;
    }

    public void writeEeprom(String serialNumber, int vid, int pid) {

        try {
            List ftDevices = FTDevice.getDevicesBySerialNumber(serialNumber);
            if (ftDevices != null && !ftDevices.isEmpty()) {

                FTDevice device = ftDevices.get(0);
                try {
                    device.open();
                    int userAreaSize = device.getEEPROMUserAreaSize();
                    EEPROMData eepromData = device.readEEPROM();
                    LOGGER.info("userAreaSize: {}, eepromData: {}", userAreaSize, eepromData);
                    // eepromData.setDescription("BiDiB-IF2");
                    // LOGGER.info("Change the description: {}", eepromData.getDescription());
                    //

                    eepromData.setVendorId((short) vid);
                    eepromData.setProductId((short) pid);

                    device.writeEEPROM(eepromData);

                    device.cyclePort();
                }
                finally {
                    device.close();
                }

            }
            else {
                LOGGER.warn("No matching device found.");
            }
        }
        catch (Exception ex) {
            LOGGER.warn("List FTDI devices failed.", ex);
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy