Example #1
0
        static void Main(string[] args)
        {
            UI Menu = new UI();

            Menu.StartPage();
            Menu.UserToSave(myFile);

            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("\nNumber 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("");
                }
            }

            //Check which ftdi adapter to use
            int inDevice = 0;

            if (ftdiDeviceCount > 1)
            {
                Console.WriteLine("Input device index to use: ");
                inDevice = (int)Console.ReadKey(true).KeyChar - '0';
            }

            // Open user selected device in our list by serial number(if only one device use first device in list)
            ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[inDevice].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 the Baud rate
            Console.WriteLine("Baudrate: ");
            UInt32 baud = UInt32.Parse(Console.ReadLine());

            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, 0, 0);
            //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
                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);
             * 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.
            do
            {
                while (!Console.KeyAvailable)
                {
                    UInt32 numBytesAvailable = 0;
                    // do
                    // {
                    ftStatus = myFtdiDevice.GetRxBytesAvailable(ref 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;
                    }

                    //} 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.Write(readData);

                    // Format our data for diffent hardware inputs
                    /***Richards Hardware***/
                    int[] iData = null;
                    RichFormatInput(readData, iData);

                    /***Ben's Hardware***/

                    /*
                     * BenFormatInput(readData);
                     * Thread.Sleep(5);
                     */

                    if (Menu.BSave)
                    {
                        //dirty check for null data TODO: add clean exception handler
                        if (iData != null)
                        {
                            myFile.SaveToFile(iData);
                            Console.WriteLine("Saved %hi Bytes to %s ", iData.Length, myFile.FilePath);
                        }
                    }
                }
            } while (Console.ReadKey().Key != ConsoleKey.Escape);

            // Close our device connection
            ftStatus = myFtdiDevice.Close();

            // Wait for a key press
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
            return;
        }
Example #2
0
    static void Main2(string[] args)
    {
        System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();

        // device identification from the FTDI chip's EEPROM
        // as reported by FTPROG utility
        // note: DO NOT use FTPROG to write to Digilent devices, it will overwrite the license key for Xilinx tools
        string devSearchString = "DIGILENT ADEPT USB DEVICE A"; // CMOD A7

        // string devSearchString = "DIGILENT USB DEVICE A"; // other devices may use this description

        // === identify suitable FTDI device ===
        print("Enumerating FTDI devices..."); sw2.Reset(); sw2.Start();
        FTDI   myFtdiDevice = new FTDI();
        UInt32 n            = 0;

        FTDI.FT_STATUS s = myFtdiDevice.GetNumberOfDevices(ref n); chk(s);
        if (n == 0)
        {
            throw new Exception("no FTDI devices");
        }
        FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[n];
        s = myFtdiDevice.GetDeviceList(ftdiDeviceList); chk(s);
        printLine(sw2.ElapsedMilliseconds + " ms");

        printLine("Devices found:");
        for (UInt32 i = 0; i < n; i++)
        {
            if (ftdiDeviceList[i] != null)
            {
                Console.WriteLine(">> '" + ftdiDeviceList[i].Description + "' SN:" + ftdiDeviceList[i].SerialNumber);
            }
        }

        print("Scanning FTDI devices for name '" + devSearchString + "'..."); sw2.Reset(); sw2.Start();

        int ixDev = -1;

        for (UInt32 i = 0; i < n; i++)
        {
            if (ftdiDeviceList[i] == null)
            {
                continue;
            }
            if (ftdiDeviceList[i].Description.ToUpper().Contains(devSearchString))
            {
                ixDev = (int)i;
            }
        }
        if (ixDev < 0)
        {
            throw new Exception("No suitable FTDI device found\nHint: is the device already claimed by a running instance of this program?\n");
        }
        printLine(sw2.ElapsedMilliseconds + " ms");

        // === open FTDI device ===
        print("Opening device..."); sw2.Reset(); sw2.Start();
        s = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[ixDev].SerialNumber); chk(s);
        printLine(sw2.ElapsedMilliseconds + " ms");

        // === create FTDI MPSSE-level IO object ===
        ftdi2_io io = new ftdi2_io(myFtdiDevice, maxTransferSize: 500000); // maxTransferSize can strongly affect roundtrip time / latency. Experiment!

        // === create JTAG-level IO ===
        uint clkDiv = 0;
        //clkDiv = 10; Console.WriteLine("DEBUG: clkDiv="+clkDiv);
        ftdi_jtag jtag = new ftdi_jtag(io, clkDiv: clkDiv);

        byte[] bufa = null;

        // === verify that there is exactly one chained device on the bus ===
        print("(optional): testing bypass register..."); sw2.Reset(); sw2.Start();
        bypassTest(jtag);
        printLine(sw2.ElapsedMilliseconds + " ms");

        // === internal test (largely SW) ===
        print("(optional): testing internal split reads..."); sw2.Reset(); sw2.Start();
        internalSplitReadTest(jtag);
        printLine(sw2.ElapsedMilliseconds + " ms");

        // === get IDCODE ===
        print("getting IDCODE..."); sw2.Reset(); sw2.Start();
        jtag.state_testLogicReset();
        jtag.state_shiftIr();
        // https://www.xilinx.com/support/documentation/user_guides/ug470_7Series_Config.pdf page 173 IDCODE == 0b001001
        bufa = new byte[] { /* opcode for IDCODE */ 0x09 };
        jtag.rwNBits(6, bufa, false); // 6-bit opcode length
        bufa = new byte[4];
        jtag.state_shiftDr();
        jtag.rwNBits(32, bufa, true);
        bufa     = jtag.getReadCopy(jtag.exec());
        bufa[3] &= 0x0F; // mask out revision bytes
        UInt64 idCode = ((UInt64)bufa[3] << 24) | ((UInt64)bufa[2] << 16) | ((UInt64)bufa[1] << 8) | (UInt64)bufa[0];

        printLine(sw2.ElapsedMilliseconds + " ms");
        Console.WriteLine("IDCODE {0:X8}", idCode);

#if false
        // === determine FPGA ===
        // https://www.xilinx.com/support/documentation/user_guides/ug470_7Series_Config.pdf page 14
        string bitstreamFile;
        switch (idCode)
        {
        case 0x362E093: bitstreamFile = "XC7A15T.bit"; break;

        case 0x362D093: bitstreamFile = "XC7A35T.bit"; break;

        case 0x362C093: bitstreamFile = "XC7A50T.bit"; break;

        case 0x3632093: bitstreamFile = "XC7A75T.bit"; break;

        case 0x3631093: bitstreamFile = "XC7A100T.bit"; break;

        case 0x3636093: bitstreamFile = "XC7A200T.bit"; break;

        default: throw new Exception(String.Format("unsupported FPGA (unknown IDCODE 0x{0:X7})", idCode));
        }
#else
        string bitstreamFile = @"..\..\..\busBridge3_RTL\busBridge3_RTL.runs\impl_1\top.bit"; Console.WriteLine("DEBUG: Trying to open bitstream from " + bitstreamFile);
#endif
        byte[] bitstream = System.IO.File.ReadAllBytes(bitstreamFile);

        // === upload to FPGA ===
        sw2.Reset(); sw2.Start();
        uploadBitstream(jtag, bitstream);
        Console.WriteLine("bitstream upload: " + sw2.ElapsedMilliseconds + " ms");
#if false
        // === exercises the USERx opcode ===
        // use this as template to work with user circuitry that is directly attached to the BSCANE2 component (without using the "higher" busbridge layers)
        byte[] tmp = new byte[32];
        for (int ix = 0; ix < tmp.Length; ++ix)
        {
            tmp[ix] = (byte)ix;
        }

        // === USERx instruction ===
        byte[] buf1 = new byte[] { 0x02 }; // USER1 opcode
        jtag.state_shiftIr();
        jtag.rwNBits(6, buf1, false);
        jtag.state_shiftDr();
        jtag.rwNBits(tmp.Length * 8, tmp, true);
        jtag.exec();
        byte[] bRead = jtag.getReadCopy(tmp.Length);
        foreach (byte b in bRead)
        {
            Console.WriteLine(String.Format("{0:X2}", b));
        }
#endif

        // === open memory interface ===
        memIf_cl m = new memIf_cl(jtag);
#if false
        int h = m.readUInt32(addr: 0x12345678, nWords: 2, addrInc: 1);
        m.exec();
        uint num = m.getUInt32(h);
        Console.WriteLine(num);
#endif

        // === self test ===
        for (long count = 0; count > -1; ++count)
        {
            // === simple, byte-level demo on USER2 opcode (no bus interface, no protocol) ===
            USER2_demo(jtag);
            USER2_demo(jtag); // run twice

            // === bus-interface based demo on USER1 opcode ===
            int  memSize = 16384;
            uint ram     = 0xF0000000;

            sw2.Reset(); sw2.Start();
            int nRep = 1000;
            m.memTest32(memSize: 1, baseAddr: 0x87654321, nIter: nRep);
            Console.WriteLine("roundtrip time " + (1000 * (double)sw2.ElapsedMilliseconds / (double)nRep) + " microseconds");

            m.memTest8(memSize: memSize, baseAddr: ram, nIter: 40);
            m.memTest16(memSize: memSize, baseAddr: ram, nIter: 20);
            m.memTest32(memSize: memSize, baseAddr: ram, nIter: 10);

            // === build one transaction (note: memTestxy has its own "exec()" internally) ===
            m.write(addr: 0x12345678, data: (uint)count & 1);
            m.queryMargin(); // reset timing margin tracker

            // queue a read and check margin
            m.readUInt32(addr: ram);
            int handle = m.queryMargin();

            // queue a read and check margin
            m.readUInt32(addr: 0x12345678);
            int handle2 = m.queryMargin();

            // configure test register delay, read and check margin
            // see RTL code
            UInt32 regVarReadLen = 0x98765432;
            m.write(addr: regVarReadLen, data: 14); // 14 is the limit for 30 MHz JTAG, ~65 MHz FPGA clock
            int h0      = m.readUInt32(addr: regVarReadLen);
            int handle3 = m.queryMargin();

            // === run in hardware ===
            m.exec();

            // === determine for the reads, how many FPGA clock cycles were left before a read timeout ===
            UInt16 margin = m.getUInt16(handle);
            Console.WriteLine("readback margin 1: " + margin + " FPGA clock cycles");
            if (margin < 1)
            {
                Console.WriteLine("WARNING: Read timed out. Slow down JTAG or increase FPGA clock frequency.");
            }
            UInt16 margin2 = m.getUInt16(handle2);
            Console.WriteLine("margin 2: " + margin2 + " FPGA clock cycles");
            if (margin2 < 1)
            {
                Console.WriteLine("WARNING: Read timed out. Slow down JTAG or increase FPGA clock frequency.");
            }
            UInt16 margin3 = m.getUInt16(handle3);
            UInt16 m3      = m.getUInt16(h0);
            Console.WriteLine("configured test register delay: " + m3 + " remaining margin: " + margin3 + " FPGA clock cycles");
            if (margin3 < 1)
            {
                Console.WriteLine("INFO: Read of slow register timed out.");
            }

            if (count == 0)
            {
                Console.WriteLine("#########################################################################");
                Console.WriteLine("### All tests passed. Press RETURN to proceed with continuous testing ###");
                Console.WriteLine("#########################################################################");
                Console.ReadLine();
            }
            Console.WriteLine("press CTRL-C or close console window to quit");
        }
    }
Example #3
0
        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");
        }
Example #4
0
        public override bool Connect()
        {
            var foundDevices = new Dictionary <string, ArccDevice>();

            try
            {
                var  myFtdiDevice    = new FTDI();
                uint ftdiDeviceCount = 0;

                var ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    return(false);
                }
                var ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
                ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    return(false);
                }

                for (var i = 0; i < ftdiDeviceList.Length; i++)
                {
                    const string descriptionPattern = "ARCC";
                    if (!ftdiDeviceList[i].Description.StartsWith(descriptionPattern))
                    {
                        continue;
                    }
                    ftStatus = myFtdiDevice.OpenByIndex((uint)i);
                    if (ftStatus != FTDI.FT_STATUS.FT_OK)
                    {
                        continue;
                    }

                    string comPort;
                    var    chipId = 0;

                    ftStatus = myFtdiDevice.GetCOMPort(out comPort);
                    if (ftStatus != FTDI.FT_STATUS.FT_OK)
                    {
                        continue;
                    }

                    myFtdiDevice.Close();
                    FTChipID.ChipID.GetDeviceChipID(i, ref chipId);
                    var id     = DevicePrefix + ":" + chipId.ToString("X");
                    var device = new ArccDevice(id, comPort);
                    foundDevices.Add(id, device);
                }
            }
            catch (Exception)
            {
                return(false);
            }
            foreach (var arccDevice in foundDevices)
            {
                if (arccDevice.Value.Connect())
                {
                    Devices.Add(arccDevice.Key, arccDevice.Value);
                }
            }
            return(true);
        }
        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;
            }
        }
Example #6
0
        public bool Initialize()
        {
            SystemInitialized = false;

            UInt32 index = 0;  // this is the index of the Lambda device found in the list of USB devices connected

            // Determine the number of FTDI devices connected to the machine
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);

            // Check if devices found
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                if (ftdiDeviceCount == 0)
                {
                    PostError("No USB devices found (Lambda Filter Controller)");
                    return(false); // no devices found
                }
            }
            else
            {
                PostError("Error Communicating with FTDI Device");
                return(false); // no devices found
            }


            // 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);

            // Search list for Lambda device
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                index = 100;
                for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                {
                    if (ftdiDeviceList[i].Description.ToString().Contains("Lambda"))
                    {
                        index = i;
                    }
                }

                if (index == 100)
                {
                    PostError("Lambda Filter Controller not found");
                    return(false); // no Lambda devices found
                }
            }


            // Open the Lambda device found
            ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[index].SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                PostError("Failed to open Lambda Filter Controller");
                return(false);  // failed to open device
            }

            // Set up device data parameters
            // Set Baud rate to 9600
            ftStatus = myFtdiDevice.SetBaudRate(9600);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                PostError("Failed to set Lambda Filter Controller baud rate");
                return(false);  // failed to set baud rate
            }

            // 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)
            {
                PostError("Failed to Lambda Filter Controller data characteristics");
                return(false);  // failed to set data characteristics (data bits, stop bits, parity)
            }

            // 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)
            {
                PostError("Failed to set Lambda Filter Controller flow control");
                return(false);  // failed to set flow control
            }

            // Set read timeout to 5 seconds, write timeout to infinite
            ftStatus = myFtdiDevice.SetTimeouts(5000, 0);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                PostError("Failed to set Lambda Filter Controller read/write timeout durations");
                return(false);  // failed to set read/write timeout durations
            }

            SystemInitialized = true;

            PostMessage("Lambda Filter Controller initialized");

            return(true);
        }
Example #7
0
        public bool OpenDevice()
        {
            UInt32 ftdiDeviceCount = 0;

            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
            ftStatus = device.GetNumberOfDevices(ref ftdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            if (ftdiDeviceCount == 0)
            {
                return(false);
            }

            // Allocate storage for device info list
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

            // Populate our device list
            ftStatus = device.GetDeviceList(ftdiDeviceList);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            // Open first device in our list by serial number
            ftStatus = device.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            // Set up device data parameters
            // Set baud rate
            ftStatus = device.SetBaudRate(BAUD_RATE);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            // Set data characteristics - Data bits, Stop bits, Parity
            ftStatus = device.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)
            {
                return(false);
            }

            // Set flow control - set RTS/CTS flow control
            ftStatus = device.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            // Set read timeout to 5 seconds, write timeout to 5s
            ftStatus = device.SetTimeouts(5000, 5000);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            return(true);
        }
Example #8
0
        protected void InitBase()
        {
            UInt32 ftdiDeviceCount = 0;

            ftStatus = FTDI.FT_STATUS.FT_OK;

            // Create new instance of the FTDI device class
            if (myFtdiDevice != null && myFtdiDevice.IsOpen == true)
            {
                myFtdiDevice.Close();
                myFtdiDevice = null;
            }
            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)
            {
                Log("Number of FTDI devices: " + ftdiDeviceCount.ToString());
                // If no devices available, return
                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 = myFtdiDevice.GetDeviceList(ftdiDeviceList);

                    if (ftStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                        {
                            Log("Device Index: " + i.ToString());
                            Log("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                            Log("Type: " + ftdiDeviceList[i].Type.ToString());
                            Log("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                            Log("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                            Log("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                            Log("Description: " + ftdiDeviceList[i].Description.ToString());
                            Log("");
                        }
                    }

                    ftStatus = myFtdiDevice.OpenByIndex(0);
                    if (ftStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        Log("Set OpenByIndex");
                    }
                    else
                    {
                        Log("Not set OpenByIndex=" + ftStatus.ToString());
                    }

                    ftStatus = myFtdiDevice.SetBitMode(0xFF, FTD2XX_NET.FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                    if (ftStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        Log("Set bit mode async  bitbang");
                    }
                    else
                    {
                        Log("Not set bit mode async  bitbang error=" + ftStatus.ToString());
                    }


                    ftStatus = myFtdiDevice.SetBaudRate(9600);
                    if (ftStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        Log("Set SetBaudRate");
                    }
                    else
                    {
                        Log("Not set SetBaudRate=" + ftStatus.ToString());
                    }

                    DisposeThread();
                    thread = new Thread(new ThreadStart(OneRound));
                    thread.Start();

                    // Wait for a key press
                    Log("Press any key to continue.");
                }
                else
                {
                    // Wait for a key press
                    Log("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                }
            }
            else
            {
                // Wait for a key press
                Log("Failed to get number of devices (error " + ftStatus.ToString() + ")");
            }
        }
Example #9
0
        private void WorkingThread()
        {
            try
            {
                // opóźnienie startu
                Thread.Sleep(3000);

                // skasowanie listy urządzeń
                Globals.Instance.ClearDevices();

                List <string> serialNumbers = new List <string>();

                FTDI.FT_STATUS result = FTDI.FT_STATUS.FT_OK;
                FTDI           ftdi   = new FTDI();

                while (!_stop)
                {
                    // łączenie z FSUIPC
                    if (!FS.FSUIPC.FS.IsConnected)
                    {
                        FS.FSUIPC.FS.Connect();
                    }

                    uint devCount = 0;
                    try
                    {
                        result = ftdi.GetNumberOfDevices(ref devCount);
                    }
                    catch { }
                    if (result == FTDI.FT_STATUS.FT_OK)
                    {
                        FTDI.FT_DEVICE_INFO_NODE[] deviceList = new FTDI.FT_DEVICE_INFO_NODE[devCount];
                        if (devCount > 0)
                        {
                            try
                            {
                                result = ftdi.GetDeviceList(deviceList);
                            }
                            catch (Exception)
                            {
                            }
                        }
                        else
                        {
                            result = FTDI.FT_STATUS.FT_OK;
                        }
                        if (result == FTDI.FT_STATUS.FT_OK)
                        {
                            // usunięcie urządzeń niepodpiętych
                            Device[] _devices = Globals.Instance.Devices;
                            {
                                int index = _devices.Length;
                                while (index-- > 0)
                                {
                                    Device device = _devices[index];
                                    if (device.SerialNumber != "FAKE" && Array.Find <FTDI.FT_DEVICE_INFO_NODE>(deviceList, delegate(FTDI.FT_DEVICE_INFO_NODE o)
                                    {
                                        return(o.SerialNumber == device.SerialNumber);
                                    }) == null)
                                    {
                                        Globals.Instance.RemoveDevice(_devices[index]);
                                        serialNumbers.Remove(device.SerialNumber);

                                        device.TurnOff();
                                    }
                                }
                            }

                            for (int i = 0; i < deviceList.Length; i++)
                            {
                                if (deviceList[i].Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
                                {
                                    string sn = deviceList[i].SerialNumber;
                                    if (!serialNumbers.Contains(sn))
                                    {
                                        serialNumbers.Add(sn);

                                        // otwarcie połączenia z urządzeniem
                                        FTD2XX_NET.FTDI           driver = new FTDI();
                                        FTD2XX_NET.FTDI.FT_STATUS status = driver.OpenBySerialNumber(sn);
                                        if (status != FTD2XX_NET.FTDI.FT_STATUS.FT_OK)
                                        {
                                            driver.Close();
                                            driver = null;
                                            continue;
                                        }

                                        // odczytanie EEPROM
                                        FTD2XX_NET.FTDI.FT232R_EEPROM_STRUCTURE eeprom = new FTD2XX_NET.FTDI.FT232R_EEPROM_STRUCTURE();
                                        status = driver.ReadFT232REEPROM(eeprom);
                                        if (status != FTD2XX_NET.FTDI.FT_STATUS.FT_OK)
                                        {
                                            driver.Close();
                                            driver = null;
                                            continue;
                                        }

                                        // odczytanie producenta "simPROJECT"
                                        if (eeprom.Manufacturer.ToUpperInvariant() != "SIMPROJECT")
                                        {
                                            driver.Close();
                                            driver = null;
                                            continue;
                                        }

                                        driver.Close();
                                        driver = null;

                                        Device device = DeviceFactory.CreateDevice(deviceList[i]);
                                        if (device != null)
                                        {
                                            Globals.Instance.AddDevice(device);
                                            device.SetPlane(Globals.Instance.CurrentPlane);
                                            device.TurnOn();
                                        }
                                    }
                                }
                            }
                        }
                    }

                    Thread.Sleep(3000);
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("Błąd w wątku śledzącym urządzenia: {0}", ex));
            }
            finally
            {
            }
        }
Example #10
0
        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[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;
            }

            // 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_RTS_CTS, 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;
            }

            // 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);
            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);
                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;
        }
Example #11
0
        private static void ListenForData()
        {
            try
            {
                UInt32         ftdiDeviceCount = 0;
                FTDI.FT_STATUS ftStatus        = FTDI.FT_STATUS.FT_OK;

                FTDI myFtdiDevice = new FTDI();

                ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);

                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() + ")");
                    Console.ReadKey();
                    return;
                }

                if (ftdiDeviceCount == 0)
                {
                    Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }

                FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

                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("");
                    }
                }

                ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }

                ftStatus = myFtdiDevice.SetBaudRate(115200);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Failed to set Baud rate (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }

                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)
                {
                    Console.WriteLine("Failed to set data characteristics (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }

                ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Failed to set flow control (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }

                // Set read timeout to 12 seconds, write timeout to infinite
                ftStatus = myFtdiDevice.SetTimeouts(12000, 0);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Failed to set timeouts (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }

                UInt32 numBytesAvailable = 0;

                ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
                Thread.Sleep(10);

                string readData     = "";
                UInt32 numBytesRead = 0;
                byte[] dataBuffer   = new byte[1024];

                ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
                while (readData == "")
                {
                    ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Failed to read data (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
                Console.WriteLine(readData);

                ftStatus = myFtdiDevice.Close();

                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                return;
            }
            catch
            {
                Console.WriteLine("Something realy bad happened..");
            }
        }
Example #12
0
        FTDI OpenAndPrepareDevice()
        {
            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(null);
            }

            // 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(null);
            }

            // 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(null);
            }

            //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.
            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
                Console.WriteLine("Failed to purge (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return(null);
            }

            return(myFtdiDevice);
        }
        //-------------------------------------------------------------------------------------------------
        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);
        }
Example #14
0
        static void Main(string[] args)
        {
            UInt32 ftdiDeviceCount = 0;

            //1

            // Create new instance of the FTDI device class
            //2

            // 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[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;
            }
            else
            {
                // ZJ
                Console.WriteLine(ftdiDeviceList[0].Description.ToString());
                Console.WriteLine("Open OK: ");
            }


            ftStatus = myFtdiDevice.ResetDevice();
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to Reset Device (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }
            else
            {
                Console.WriteLine("Reset OK: ");
            }



            //3
            UInt32 numBytesRead = 0;
            UInt32 data         = 0;

            UInt32  numBytesWritten = 5;
            Boolean calibrated      = false;

            //4//Boolean Meas_Ready = false;

            ftStatus = myFtdiDevice.Write(cmd_ctrl, cmd_ctrl.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_en, cmd_en.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_wr_s, cmd_wr_s.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_wr_dac, cmd_wr_dac.Length, ref numBytesWritten);

            // wait on finish of calibration
            Console.WriteLine("Wait on calibration");


            ftStatus = myFtdiDevice.Write(cmd_res, 5, ref numBytesWritten);

            do
            {
                Console.Write(".");
                ftStatus = myFtdiDevice.Write(cmd_rd_s, cmd_rd_s.Length, ref numBytesWritten);
                ftStatus = myFtdiDevice.Read(read_buffer, 4, ref numBytesRead);
                data     = BitConverter.ToUInt32(read_buffer, 0);
                if ((data & 0x800) == 0x800)
                {
                    calibrated = true;
                }
            } while (!calibrated);
            Console.WriteLine("");
            Console.WriteLine("Calibration done");

            // read the offset value - the simpliest version without averaging (you should read more consecutive data to calculate average value of OFFSET, see page 2.7)

            Double Offset   = 0.000000000000000;
            UInt32 Offset_F = 0;
            Double Meas     = 0.000000000000000;
            UInt64 Count    = 0;
            UInt32 A        = 0;
            UInt32 B        = 0;


            cmd_rd_f_data[1] = 0x02; // number of 32-bit data = 2
            ftStatus         = myFtdiDevice.Write(cmd_rd_f_data, 5, ref numBytesWritten);

            do
            {
                ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesRead);
            } while (numBytesRead < 8);

            ftStatus = myFtdiDevice.Read(read_buffer, 8, ref numBytesRead);
            data     = BitConverter.ToUInt32(read_buffer, 0); // first 32-bit word

            Count = data >> 20;
            A     = data & 0x3FF;
            B     = (data >> 10) & 0x3FF;

            data = BitConverter.ToUInt32(read_buffer, 4);   // second 32-bit word

            Count  = (data << 12) | Count;
            Offset = 4 * ((double)Count + (((double)A - (double)B) / 1024));
            Console.WriteLine("Offset = " + Offset.ToString() + " ns");
            Offset = Offset / 1000000000;

///////////////freq offset
            ftStatus = myFtdiDevice.Write(cmd_rd_offset_f, 5, ref numBytesWritten);

            do
            {
                ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesRead);
            } while (numBytesRead < 4);

            ftStatus = myFtdiDevice.Read(read_buffer, 4, ref numBytesRead);
            data     = BitConverter.ToUInt32(read_buffer, 0); // first 32-bit word

            //Count = (data << 12) | Count;
            Offset_F = data;
            Console.WriteLine("Offset_F = " + Offset_F.ToString() + " Hz");
            //Offset_F = Offset_F / 1000000000;



            //TimeIntervalMeas();

            do
            {
                FrequencyMeas(Offset_F);
                //WriteToFile();
                ////////////////////////
                Console.ReadKey();
            }while(true);
            // Close our device
            ftStatus = myFtdiDevice.Close();

            return;
        }
Example #15
0
        private static bool FT232H_Initial()
        {
            UInt32 ftdiDeviceCount = 0;

            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

            // Create new instance of the FTDI device class
            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(false);
            }

            // 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(false);
            }

            // 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.OpenByDescription("FT232H");
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return(false);
            }

            return(true);
        }
Example #16
0
        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[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_232R)
            {
                // We have an FT232R or FT245R so use FT232R EEPROM structure
                FTDI.FT232R_EEPROM_STRUCTURE myEEData = new FTDI.FT232R_EEPROM_STRUCTURE();
                // Read the device EEPROM
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT232REEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT232REEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
            {
                // We have an FT2232 so use FT2232 EEPROM structure
                FTDI.FT2232_EEPROM_STRUCTURE myEEData = new FTDI.FT2232_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT2232EEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT2232EEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_BM)
            {
                // We have an FT232B or FT245B so use FT232B EEPROM structure
                FTDI.FT232B_EEPROM_STRUCTURE myEEData = new FTDI.FT232B_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT232BEEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT232BEEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }


            // Use cycle port to force a re-enumeration of the device.
            // In the FTD2XX_NET class library, the cycle port method also
            // closes the open handle so no need to call the Close method separately.
            ftStatus = myFtdiDevice.CyclePort();

            UInt32 newFtdiDeviceCount = 0;

            do
            {
                // Wait for device to be re-enumerated
                // The device will have the same location since it has not been
                // physically unplugged, so we will keep trying to open it until it succeeds
                ftStatus = myFtdiDevice.OpenByLocation(ftdiDeviceList[0].LocId);
                Thread.Sleep(1000);
            } while (ftStatus != FTDI.FT_STATUS.FT_OK);

            // Close the device
            myFtdiDevice.Close();

            // Re-create our device list
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref newFtdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

            // Re-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("");
                }
            }

            // Wait for a key press
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            return;
        }
        private string[] GetPotentialSensorSerialNumbers()
        {
            List <string> SerialNumbers = new List <string>();

            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)
            {
                DebugLog.Enqueue("Number of FTDI devices (A and B): " + ftdiDeviceCount.ToString());
            }
            else
            {
                DebugLog.Enqueue("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                return(SerialNumbers.ToArray());
            }

            // If no devices available, return
            if (ftdiDeviceCount == 0)
            {
                DebugLog.Enqueue("No FTDI devices found");
                return(SerialNumbers.ToArray());
            }

            // Allocate storage for device info list
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

            // Wrap the following code in try-catch to avoid exceptions when devices are being added
            try
            {
                // Populate our device list
                ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);

                // Add potential sensors to the list
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                    {
                        if (ftdiDeviceList[i].Description.ToString() == PediatricSoftConstants.ValidIDN)
                        {
                            SerialNumbers.Add(ftdiDeviceList[i].SerialNumber.ToString());
                        }
                    }
                }
                else
                {
                    DebugLog.Enqueue("Failed to get FTDI serial numbers (error " + ftStatus.ToString() + ")");
                    return(SerialNumbers.ToArray());
                }
            }
            catch
            {
                DebugLog.Enqueue("Failed to get FTDI serial numbers - devices are still being added. Please wait a few minutes.");
                return(SerialNumbers.ToArray());
            }

            return(SerialNumbers.ToArray());
        }
Example #18
0
        public bool Initialize(out string message)
        // Desc: Initilize USB communication
        // Output: message: Message
        // bool: true = success, false = fail
        {
            uint ftdiDeviceCount = 0;
            uint i;
            uint myDeviceNum = 0;

            FTDI.FT_DEVICE_INFO_NODE[]    ftdiDeviceList;
            FTDI.FT2232H_EEPROM_STRUCTURE myEEData = new FTDI.FT2232H_EEPROM_STRUCTURE();

            // Determine the number of FTDI devices connected to the machine
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                message = "FTDI GetNumberOfDevices Failed";
                return(false);
            }
            if (ftdiDeviceCount == 0)
            {
                message = "No device found";
                return(false);
            }
            // Get device info
            ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
            ftStatus       = myFtdiDevice.GetDeviceList(ftdiDeviceList);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                message = "FTDI GetDeviceList Failed";
                return(false);
            }
            if (ftdiDeviceList[0] == null)
            {
                message = "FTDI GetDeviceList Failed";
                return(false);
            }
            // Determine which device to use
            for (i = 0; i < ftdiDeviceCount; i++)
            {
                if (ftdiDeviceList[i].Description == "TTL232R")
                {
                    myDeviceNum = i;
                    break;
                }
            }
            if (i == ftdiDeviceCount)
            {
                message = "FTDI devices doesn't fit the description";
                return(false);
            }
            // Open the selected device
            ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[myDeviceNum].SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                message = "FTDI OpenBySerialNumber Failed";
                return(false);
            }
            // Setup baud rate
            ftStatus = myFtdiDevice.SetBaudRate(1250000); // 9600
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                message = "FTDI SetBaudRate Failed";
                return(false);
            }
            // 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)
            {
                message = "FTDI SetDataCharacteristics Failed";
                return(false);
            }
            // Set flow control - set RTS/CTS flow control
            ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x00, 0x00);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                message = "FTDI SetFlowControl Failed";
                return(false);
            }
            // Set read timeout, write timeout
            ftStatus = myFtdiDevice.SetTimeouts(3000, 3000);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                message = "FTDI SetTimeouts Failed";
                return(false);
            }

            // Show result
            message     = "Initialization complete";
            initialized = true;
            return(true);
        }
 private void Polacz()
 {
     ftstatus = device.GetDeviceList(devicelist);
     ftstatus = device.OpenByDescription(devicelist[0].Description);
     ftstatus = device.SetBitMode(0xff, 1);
 }
Example #20
0
        static void Main(string[] args)
        {
            FTDI    ftdi = new FTDI();
            MAX7219 Display;

            uint devcount = 0;

            ftdi.GetNumberOfDevices(ref devcount);

            if (devcount > 0)
            {
                byte[] TransferBuffer = new byte[88];
                uint   NumBytesToTransfer;
                uint   NumBytesTransfered = 0;

                Display = new MAX7219(TransferBuffer, WriteOut);

                //FTDI AN_108 3.8 Clock Divisor
                uint dwClockDivisor = 29; //Value of clock divisor, SCL Frequency = 60/((1+29)*2) (MHz) = 1Mhz

                FTDI.FT_DEVICE_INFO_NODE[] devices = new FTDI.FT_DEVICE_INFO_NODE[devcount];

                string Buffer;

                FTDI.FT_STATUS s = ftdi.GetDeviceList(devices);

                for (uint ix = 0; ix < devcount; ix++)
                {
                    ftdi.OpenBySerialNumber(devices[ix].SerialNumber);

                    ftdi.GetCOMPort(out Buffer);
                    Console.WriteLine(Buffer);


                    ftdi.GetDescription(out Buffer);
                    Console.WriteLine(Buffer);


                    //FTDI Set Mode MPSSE
                    s  = ftdi.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                    s |= ftdi.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_MPSSE);

                    if (s != FTDI.FT_STATUS.FT_OK)
                    {
                        Console.WriteLine("Fehler SetBitMode");
                        ftdi.Close();
                        return;
                    }

                    //FTDI Sync MPSSE (Check) FTDI_AN114 Page 10
                    if (Sync_MPSSE(ref ftdi, TransferBuffer) != FTDI.FT_STATUS.FT_OK)
                    {
                        Console.WriteLine("Fehler Sync MPSSE");
                        ftdi.Close();
                        return;
                    }


                    //Init FTDI SPI  See FTDI AN_108
                    NumBytesToTransfer = 0;
                    TransferBuffer[NumBytesToTransfer++] = 0x8a; // Disable Clock divide /5
                    TransferBuffer[NumBytesToTransfer++] = 0x97; // Disable adaptiv clockink
                    TransferBuffer[NumBytesToTransfer++] = 0x8d; // Disables 3 phase data clocking

                    // I think is not nesacery
                    //TransferBuffer[NumBytesToTransfer++] = 0x80;
                    //TransferBuffer[NumBytesToTransfer++] = 0x08;
                    //TransferBuffer[NumBytesToTransfer++] = 0x0b;

                    TransferBuffer[NumBytesToTransfer++] = 0x86; //3.8 Clock Divisor
                    TransferBuffer[NumBytesToTransfer++] = (byte)(dwClockDivisor & 0xff);
                    TransferBuffer[NumBytesToTransfer++] = (byte)(dwClockDivisor >> 8);

                    s = ftdi.Write(TransferBuffer, NumBytesToTransfer, ref NumBytesTransfered);
                    NumBytesToTransfer = 0;
                    Thread.Sleep(20);


                    TransferBuffer[NumBytesToTransfer++] = 0x85; // Disable loopback
                    s |= ftdi.Write(TransferBuffer, NumBytesToTransfer, ref NumBytesTransfered);
                    NumBytesToTransfer = 0;
                    if (s != FTDI.FT_STATUS.FT_OK)
                    {
                        Console.WriteLine("SPI Init Fehler");
                        ftdi.Close();
                        return;
                    }
                    Console.WriteLine("SPI Init OK");

                    Console.WriteLine("Press ESC to Exit");

                    //Init 7219
                    s = ftdi.Write(TransferBuffer, Display.Init(8), ref NumBytesTransfered);

                    UInt32 count = 0;

                    while (true)
                    {
                        //Data
                        s |= ftdi.Write(TransferBuffer, Display.WriteDec(count, (byte)(1 << ((byte)(count++ % 8)))), ref NumBytesTransfered);

                        Console.WriteLine("SPI {0} Bytes Write", NumBytesTransfered);

                        Thread.Sleep(100);

                        if (Console.KeyAvailable && (Console.ReadKey(true)).Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                    }



                    s |= ftdi.Write(TransferBuffer, Display.Clr(), ref NumBytesTransfered);


                    if (s != FTDI.FT_STATUS.FT_OK)
                    {
                        Console.WriteLine("SPI Fehler Write Data");
                        ftdi.Close();
                        return;
                    }

                    ftdi.Close();
                }
            }
            else
            {
                Console.WriteLine("Kein FTDI gefunden :-(");
            }
        }
Example #21
0
        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;
                }
            }
        }
Example #22
0
        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[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;
            }
            else
            {
                // ZJ
                Console.WriteLine(ftdiDeviceList[0].Description.ToString());
                Console.WriteLine("Open OK: ");
            }


            ftStatus = myFtdiDevice.ResetDevice();
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to Reset Device (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }
            else
            {
                Console.WriteLine("Reset OK: ");
            }



            // you can define control commands (see Programmer's Guide - page 4)

            byte[] cmd_res    = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00 };
            byte[] cmd_ctrl   = new byte[] { 0x04, 0x23, 0x00, 0x00, 0x00 }; // time interval, A<->B, 1 s range
            byte[] cmd_en     = new byte[] { 0x03, 0x04, 0x00, 0x00, 0x00 }; // Ihibit
            byte[] cmd_wr_s   = new byte[] { 0x02, 0xee, 0x04, 0x00, 0x00 }; // set START A, STOP B, slope rise (both), internal clock ON
            byte[] cmd_wr_dac = new byte[] { 0x05, 0x8f, 0x8f, 0x00, 0x00 }; // threshold 0.5 V to START/STOP.

            byte[] cmd_rd_s = new byte[] { 0xF2, 0x01, 0x00, 0x00, 0x00 };

            byte[] cmd_meas       = new byte[] { 0x01, 0x01, 0x00, 0x00, 0x00 };
            byte[] cmd_rd_meas_no = new byte[] { 0xF1, 0x01, 0x00, 0x00, 0x00 };
            byte[] cmd_rd_f_data  = new byte[] { 0xF0, 0x02, 0x00, 0x00, 0x00 };

            byte[] read_buffer = new byte[4 * 1024];   // set the size of buffer

            UInt32 numBytesRead = 0;
            UInt32 data         = 0;

            UInt32  numBytesWritten = 5;
            Boolean calibrated      = false;
            Boolean Meas_Ready      = false;

            ftStatus = myFtdiDevice.Write(cmd_ctrl, cmd_ctrl.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_en, cmd_en.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_wr_s, cmd_wr_s.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_wr_dac, cmd_wr_dac.Length, ref numBytesWritten);

// wait on finish of calibration
            Console.WriteLine("Wait on calibration");


            ftStatus = myFtdiDevice.Write(cmd_res, 5, ref numBytesWritten);

            do
            {
                Console.Write(".");
                ftStatus = myFtdiDevice.Write(cmd_rd_s, cmd_rd_s.Length, ref numBytesWritten);
                ftStatus = myFtdiDevice.Read(read_buffer, 4, ref numBytesRead);
                data     = BitConverter.ToUInt32(read_buffer, 0);
                if ((data & 0x800) == 0x800)
                {
                    calibrated = true;
                }
            } while (!calibrated);
            Console.WriteLine("");
            Console.WriteLine("Calibration done");

// read the offset value - the simpliest version without averaging (you should read more consecutive data to calculate average value of OFFSET, see page 2.7)

            Double Offset = 0.000000000000000;
            Double Meas   = 0.000000000000000;
            UInt64 Count  = 0;
            UInt32 A      = 0;
            UInt32 B      = 0;


            cmd_rd_f_data[1] = 0x02; // number of 32-bit data = 2
            ftStatus         = myFtdiDevice.Write(cmd_rd_f_data, 5, ref numBytesWritten);

            do
            {
                ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesRead);
            } while (numBytesRead < 8);

            ftStatus = myFtdiDevice.Read(read_buffer, 8, ref numBytesRead);
            data     = BitConverter.ToUInt32(read_buffer, 0);   // first 32-bit word

            Count = data >> 20;
            A     = data & 0x3FF;
            B     = (data >> 10) & 0x3FF;

            data = BitConverter.ToUInt32(read_buffer, 4);   // second 32-bit word

            Count  = (data << 12) | Count;
            Offset = 4 * ((double)Count + (((double)A - (double)B) / 1024));
            Console.WriteLine("Offset = " + Offset.ToString() + " ns");
            Offset = Offset / 1000000000;



// Measurement loop (in sequence: Start single measurement -> read the result)
            // refresh settings (the settings are not restored after calibration)
            ftStatus = myFtdiDevice.Write(cmd_ctrl, cmd_ctrl.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_en, cmd_en.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_wr_s, cmd_wr_s.Length, ref numBytesWritten);
            ftStatus = myFtdiDevice.Write(cmd_wr_dac, cmd_wr_dac.Length, ref numBytesWritten);
            Thread.Sleep(20);     //wait for DAC


            Console.WriteLine("");
            Console.WriteLine("Press CTRL C to intrrupt");
            Console.WriteLine("");

            cmd_meas = new byte[] { 0x01, 0x01, 0x00, 0x00, 0x00 };     // number of measurements to do = 1
            do
            {
                ftStatus   = myFtdiDevice.Write(cmd_meas, 5, ref numBytesWritten); // start measuring process
                Meas_Ready = false;
                do                                                                 //check the value of RD_MEAS_NO register
                {
                    ftStatus = myFtdiDevice.Write(cmd_rd_meas_no, 5, ref numBytesWritten);
                    ftStatus = myFtdiDevice.Read(read_buffer, 4, ref numBytesRead);
                    data     = BitConverter.ToUInt32(read_buffer, 0);
                    if (data > 0)
                    {
                        Meas_Ready = true;
                    }
                } while (!Meas_Ready);


                // READ RESULTS

                cmd_rd_f_data = new byte[] { 0xF0, 0x02, 0x00, 0x00, 0x00 }; // how many words do you want to read? (2)
                ftStatus      = myFtdiDevice.Write(cmd_rd_f_data, 5, ref numBytesWritten);
                do
                {
                    ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesRead);
                } while (numBytesRead < 8);
                numBytesRead = 0;

                ftStatus = myFtdiDevice.Read(read_buffer, 8, ref numBytesRead);
                data     = BitConverter.ToUInt32(read_buffer, 0); // the first 32-bit word

                Count = data >> 20;
                A     = data & 0x3FF;
                B     = (data >> 10) & 0x3FF;

                data = BitConverter.ToUInt32(read_buffer, 4);   //  the second 32-bit word

                Count = (data << 12) | Count;
                Meas  = 4 * ((double)Count + (((double)A - (double)B) / 1024));
                Meas  = Meas / 1000000000;
                Meas  = Meas - Offset;
                Console.WriteLine("Time interval = " + Meas.ToString());

                // you should write something to exit the loop
            } while (true);


            // Close our device
            ftStatus = myFtdiDevice.Close();

            return;
        }
Example #23
0
        public void fdti_init()
        {
            // Determine the number of FTDI devices connected to the machine
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
            // Check status
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                // MessageBox.Show("Number of FTDI devices: " + ftdiDeviceCount.ToString());
                //  richTextBox1.AppendText("Number of FTDI devices: " + ftdiDeviceCount.ToString());
            }
            else
            {
                // Wait for a key press

                MessageBox.Show("Failed to get number of devices (error " + ftStatus.ToString() + ")");

                this.Close();
            }

            // If no devices available, return
            if (ftdiDeviceCount == 0)
            {
                // Wait for a key press
                MessageBox.Show("Failed to get number of devices (error " + ftStatus.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);

            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                //for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                //{
                //    // + Environment.NewLine
                //    richTextBox1.Text += ("Device Index: " + i.ToString());
                //    richTextBox1.Text += ("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                //    richTextBox1.Text += ("Type: " + ftdiDeviceList[i].Type.ToString());
                //    richTextBox1.Text += ("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                //    richTextBox1.Text += ("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                //    richTextBox1.Text += ("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                //    richTextBox1.Text += ("Description: " + ftdiDeviceList[i].Description.ToString());
                //    richTextBox1.Text += ("");
                //}
                //toolStripStatusLabel2.Text += "FT_OK";
                statusLed.Value = true;
            }


            // 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
                MessageBox.Show("Failed to open device (error " + ftStatus.ToString() + ")");
            }



            ftStatus = myFtdiDevice.SetBitMode(0xFF, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                MessageBox.Show("Failed to set Reset mode (error " + ftStatus.ToString() + ")");
            }
            // Thread.Sleep(10);


            ftStatus = myFtdiDevice.SetBitMode(0xFF, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                MessageBox.Show("Failed to set FIFO mode (error " + ftStatus.ToString() + ")");
            }
            // 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
                MessageBox.Show("Failed to set flow control (error " + ftStatus.ToString() + ")");
            }
            //++++++++++++++++++++++++++++++++++++++++++
            ftStatus = myFtdiDevice.SetLatency(16);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                MessageBox.Show("Failed to st latency (error " + ftStatus.ToString() + ")");
                return;
            }
            //++++++++++++++++++++++++++++++++++++++++++

            //++++++++++++++++++++++++++++++++++++++++++
            // Set read timeout to 5 seconds, write timeout to infinite
            ftStatus = myFtdiDevice.SetTimeouts(500, 0);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                MessageBox.Show("Failed to set timeouts (error " + ftStatus.ToString() + ")");
            }
        }
Example #24
0
        protected FTDI OpenChannel(string channelName, uint baudRate)
        {
            var res = new FTDI();

            FTDI.FT_STATUS status = FTDI.FT_STATUS.FT_OTHER_ERROR;

            FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[255];

            status = res.GetDeviceList(devicelist);

            // ON LINUX RUN APP WITH SUDO OR CONFIGURE ACCESS TO USB FTDI DEVICES
            Console.WriteLine($"getdevicelist status is {status}");

            devicelist = devicelist.Where(x => x != null).ToArray();

            if (!devicelist.Any())
            {
                throw new Exception("No FTDI devices found.");
            }

            foreach (var device in devicelist)
            {
                Console.WriteLine($"Description is '{device.Description}'");
                Console.WriteLine($"SerialNumber is '{device.SerialNumber}'");
                Console.WriteLine($"ID is '{device.ID}'");
                Console.WriteLine($"LocId is '{device.LocId}'");
                Console.WriteLine($"Type is '{device.Type}'");
                Console.WriteLine($"------");
            }


            status = res.OpenBySerialNumber(channelName);
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            res.ResetDevice();
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            // for (int i = 0; i < 60; i++)
            // {
            //     status = res.OpenBySerialNumber(channelName);
            //     if (
            //         status != FTD2XX_NET.FTDI.FT_STATUS.FT_DEVICE_NOT_FOUND &&
            //         status != FTD2XX_NET.FTDI.FT_STATUS.FT_DEVICE_NOT_OPENED
            //         )
            //         break;
            //     Thread.Sleep(1000);
            //     res = new FTDI();
            //     FTDI.FT_DEVICE_INFO_NODE[] list = new FTDI.FT_DEVICE_INFO_NODE[200];
            //     status = res.GetDeviceList(list);
            // }
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            status = res.SetBaudRate(baudRate);
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            status = res.SetLatency(0);
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            // enable async bitbang mode for all 8 pins
            status = res.SetBitMode(0b11111111, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            status = res.SetTimeouts(1, 1);
            Debug.Assert(status == FTDI.FT_STATUS.FT_OK);

            return(res);
        }
Example #25
0
        private void GetIdFTDI()
        {
            // Determine the number of FTDI devices connected to the machine
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
            // Check status
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                Log("Number of FTDI devices: " + ftdiDeviceCount.ToString());
                Log("");
                Text = "BitBang - Devices found " + ftdiDeviceCount.ToString();
            }
            else
            {
                // Wait for a key press
                Log("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Log("");
                Text = "BitBang - Error";
                connectBtn.Enabled = false;
                setBtn.Enabled     = false;
                comboBox1.Enabled  = false;
                return;
            }

            // If no devices available, return
            if (ftdiDeviceCount == 0)
            {
                // Wait for a key press
                //Log("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Log("Failed to get IDs of devices");
                Text = "BitBang - No devices";
                connectBtn.Enabled = false;
                setBtn.Enabled     = false;
                comboBox1.Enabled  = false;
                Log("_____________________________________");
                return;
            }
            else
            {
                comboBox1.Enabled = true;
                // Allocate storage for device info list
                //FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
                ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

                // Populate our device list
                ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
                comboBox1.Items.Clear();
                foreach (FTDI.FT_DEVICE_INFO_NODE device in ftdiDeviceList)
                {
                    comboBox1.Items.Add(device.LocId);
                }
                if (ftdiDeviceCount == 1)
                {
                    comboBox1.Text = ftdiDeviceList[0].LocId.ToString();
                }
                //ftStatus = myFtdiDevice.OpenByIndex
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                    {
                        Log("Device Index: " + i.ToString());
                        Log("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                        Log("Type: " + ftdiDeviceList[i].Type.ToString());
                        Log("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                        Log("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                        Log("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                        Log("Description: " + ftdiDeviceList[i].Description.ToString());
                        Log("");
                    }
                }
                connectBtn.Enabled = true;
            }

            Log("_____________________________________");
        }
        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;
            }
        }
Example #27
0
        private void SetupDialogForm_Shown(object sender, EventArgs e)
        {
            labelVersionInformation.Text  = "CAM86 " + "v=" + Camera.driverVersion + ", LL=" + Camera.driverLLversion;
            backuplabelVersionInformation = labelVersionInformation.Text;

            // should we show all camera parameters or only some
            windowSize = Camera.settingsWindowSizeState;

            if (cameraConnected)
            {
                // make sure the value is valid
                if (windowSize == settingsWindowSizeE.cameraOff)
                {
                    windowSize = settingsWindowSizeE.cameraOnNoOptions;
                }

                setupWindowSize();

                // add firmware label
                labelVersionInformation.Text += ", FW=" + Camera.driverFirmwareVersion; // will always be zero unless read after initial camera connection
                backuplabelVersionInformation = labelVersionInformation.Text;
            }
            else
            {
                // make sure the value is valid
                if (windowSize != settingsWindowSizeE.cameraOff)
                {
                    windowSize = settingsWindowSizeE.cameraOff;
                }

                setupWindowSize();

                // Get info about the connected FTDI Cam86 devices
                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("CAM86"))
                            {
                                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;
            }

            // Initialise current values of user settings from the ASCOM Profile
            checkBoxNightMode.Checked                 = Camera.nightModeSettingsState;
            checkBoxOnTop.Checked                     = Camera.onTopState;
            checkBoxTEConDuringRead.Checked           = Camera.coolerDuringReadingState;
            checkBoxMono.Checked                      = Camera.monoSensorState;
            slowCoolingCheckBox.Checked               = Camera.slowCoolingEnabledState;
            slowCoolingNumUpDown.Value                = (decimal)(Camera.slowCoolingSpeedState / 10.0);
            numericUpDownSensorClearTime.Value        = Camera.sensorClearBeforeExposureTimeState;
            numericUpDownTECstartupPowerPercent.Value = Camera.coolingStartingPowerPercentState;
            numericUpDownTECmaximumPowerPercent.Value = Camera.coolingMaxPowerPercentState;
            checkBoxOpenSettingsOnConnect.Checked     = Camera.settingsWindowOpenOnConnectState;

            gainNumUpDown.Value            = Camera.gainState;
            offsetNumUpDown.Value          = Camera.offsetState;
            numericUpDownReadingTime.Value = Camera.readingTimeState;
            numericUpDownPIDKp.Value       = (decimal)Camera.PIDproportionalGainState;
            numericUpDownPIDKi.Value       = (decimal)Camera.PIDintegralGainState;
            numericUpDownPIDKd.Value       = (decimal)Camera.PIDderivativeGainState;

            checkBoxDHT22.Checked = Camera.DHT22presentState;
            if (!checkBoxDHT22.Checked)
            {
                labelDHTinfo.Text           = "DHT: no sensor";
                labelDHTinfo.Visible        = false;
                buttonHideSettings.Location = new Point(86, buttonHideSettings.Location.Y); // shift the button to left
            }
            else
            {
                labelDHTinfo.Visible        = true;
                buttonHideSettings.Location = new Point(6, buttonHideSettings.Location.Y); // center the button
            }

            // check date for the April Fools' Day joke
            if (DateTime.Now.Day == 1 && DateTime.Now.Month == 4)
            {
                buttonCloudsOffAprilFoolsDay.Visible = true;
            }
            else
            {
                buttonCloudsOffAprilFoolsDay.Visible = false;
            }
        }
Example #28
0
        public ThorController(ThorStageModel stageModel, string serialNumber)
        {
            stage = new ThorStage(stageModel);

            UInt32 deviceCount     = 0;
            uint   numBytesWritten = 0;

            this.serialNumber = serialNumber;
            deviceHandle      = new FTDI();

            // Determine the number of FTDI devices connected to the machine
            ftStatus = deviceHandle.GetNumberOfDevices(ref deviceCount);

            // Populate device list
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[deviceCount];
            ftStatus = deviceHandle.GetDeviceList(ftdiDeviceList);

            ftStatus = deviceHandle.OpenBySerialNumber(serialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                throw new Exception("Failed to open FTDI device " + ftStatus.ToString());
            }


            // Set baud rate to 115200.
            ftStatus = deviceHandle.SetBaudRate(115200);

            // 8 data bits, 1 stop bit, no parity
            ftStatus = deviceHandle.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);

            // Pre purge dwell 50ms.
            Thread.Sleep(50);

            // Purge the device.
            ftStatus = deviceHandle.Purge(FTDI.FT_PURGE.FT_PURGE_RX | FTDI.FT_PURGE.FT_PURGE_TX);

            // Post purge dwell 50ms.
            Thread.Sleep(50);

            // Reset device.
            ftStatus = deviceHandle.ResetDevice();

            // Set flow control to RTS/CTS.
            ftStatus = deviceHandle.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0, 0);

            // Set RTS.
            ftStatus = deviceHandle.SetRTS(true);

            // Disable then enable channel
            byte[] change_enstate = { 0x10, 0x02, 0x01, 0x02, 0x50, 0x1 };
            ftStatus          = deviceHandle.Write(change_enstate, 6, ref numBytesWritten);
            change_enstate[3] = 0x01;
            ftStatus          = deviceHandle.Write(change_enstate, 6, ref numBytesWritten);

            // Move home
            byte[] movehome = { 0x43, 0x04, 0x01, 0x00, 0x50, 0x01 };

            /*ftStatus = deviceHandle.Write(movehome, 6, ref numBytesWritten);
             * Thread.Sleep(3000);
             * do
             * {
             *  statusBits = GetStatus();
             *  //Console.WriteLine("{0:X}", statusbits);
             * } while ((statusBits & 0x400) != 0x400 || statusBits == 0);*/
        }
Example #29
0
        public void Init()
        {
            // get number of FTDI devices connected to the machine
            uint ftdiDeviceCount = 0;
            var  status          = Ftdi.GetNumberOfDevices(ref ftdiDeviceCount);

            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Logger.Error("Failed to get number of FTDI devices: {0}", status);
                return;
            }

            // if no FTDI device found, return.
            if (ftdiDeviceCount == 0)
            {
                Logger.Debug("PinDMDv1 device not found.");
                return;
            }

            // allocate storage for device info list
            var ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

            // populate device list
            status = Ftdi.GetDeviceList(ftdiDeviceList);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Logger.Error("Failed to get FTDI devices: {0}", status);
                return;
            }

            // loop through list and find PinDMDv1
            for (uint i = 0; i < ftdiDeviceCount; i++)
            {
                var serialNumber = ftdiDeviceList[i].SerialNumber;
                if (serialNumber == "DMD1000" || serialNumber == "DMD1001")
                {
                    _pinDmd1Device = ftdiDeviceList[i];
                    IsAvailable    = true;

                    Logger.Info("Found PinDMDv1 device.");
                    Logger.Debug("   Device Index:  {0}", i);
                    Logger.Debug("   Flags:         {0:x}", _pinDmd1Device.Flags);
                    Logger.Debug("   Type:          {0}", _pinDmd1Device.Type);
                    Logger.Debug("   ID:            {0:x}", _pinDmd1Device.ID);
                    Logger.Debug("   Location ID:   {0}", _pinDmd1Device.LocId);
                    Logger.Debug("   Serial Number: {0}", _pinDmd1Device.SerialNumber);
                    Logger.Debug("   Description:   {0}", _pinDmd1Device.Description);
                    break;
                }
            }

            if (!IsAvailable)
            {
                Logger.Debug("PinDMDv1 device not found.");
                return;
            }

            // open device by serial number
            status = Ftdi.OpenBySerialNumber(_pinDmd1Device.SerialNumber);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Logger.Error("Failed to open device: {0}", status);
                IsAvailable = false;
                return;
            }

            // set bit mode
            status = Ftdi.SetBitMode(0xff, 0x1);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Logger.Error("Failed to set bit mode: {0}", status);
                IsAvailable = false;
                return;
            }

            // set baud rate
            status = Ftdi.SetBaudRate(12000);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Logger.Error("Failed to set baud rate: {0}", status);
                IsAvailable = false;
                return;
            }
            Logger.Info("Connected to PinDMDv1.");
        }
Example #30
0
        //finds the ftdi port
        private string getFTDIPort()
        {
            FTDI ftdi = new FTDI();

            FTDI.FT_STATUS state;

            uint deviceCount = 0;

            state = ftdi.GetNumberOfDevices(ref deviceCount);

            if (state != FTDI.FT_STATUS.FT_OK)
            {
                return(null);
            }

            FTDI.FT_DEVICE_INFO_NODE[] deviceNodes = new FTDI.FT_DEVICE_INFO_NODE[deviceCount];
            state = ftdi.GetDeviceList(deviceNodes);

            if (state != FTDI.FT_STATUS.FT_OK)
            {
                return(null);
            }

            string port = null;

            foreach (FTDI.FT_DEVICE_INFO_NODE node in deviceNodes)
            {
                if (node.Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
                {
                    //Dieses Gerät hat 2 serielle Anschlüsse. Wir brauchen den Typ B.
                    if (node.Description.EndsWith("B"))
                    {
                        state = ftdi.OpenBySerialNumber(node.SerialNumber);

                        if (state != FTDI.FT_STATUS.FT_OK)
                        {
                            continue;
                        }

                        String tmpPort;
                        state = ftdi.GetCOMPort(out tmpPort);

                        if (state != FTDI.FT_STATUS.FT_OK)
                        {
                            Trace.WriteLine("Failed to get COM-Port (error " + state.ToString() + ")");
                            ftdi.Close();
                            continue;
                        }

                        if (tmpPort == null || tmpPort.Length == 0)
                        {
                            Trace.WriteLine(string.Format("Failed to get COM-Port for device {0}.", node.SerialNumber));
                            ftdi.Close();
                            continue;
                        }

                        FTDI.FT2232_EEPROM_STRUCTURE eepromData = new FTDI.FT2232_EEPROM_STRUCTURE();

                        try
                        {
                            state = ftdi.ReadFT2232EEPROM(eepromData);
                        }
                        catch (Exception)
                        {
                            Trace.WriteLine("Exception thrown when calling ReadFT2232EEPROM");
                        }

                        if (state != FTDI.FT_STATUS.FT_OK)
                        {
                            Trace.WriteLine("Failed to read device EEPROM (error " + state.ToString() + ")");
                            ftdi.Close();
                            continue;
                        }

                        Debug.WriteLine(port);
                        ftdi.Close();

                        if (!eepromData.Manufacturer.Equals("EnOcean GmbH") && !eepromData.Manufacturer.Equals("Viessmann") && !eepromData.Manufacturer.Equals("Eltako GmbH"))
                        {
                            Trace.WriteLine(String.Format("Wrong Manufacturer {0}", eepromData.Manufacturer));
                            continue;
                        }

                        if (tmpPort != null)
                        {
                            port = tmpPort;
                            break;
                        }
                    }
                }
                else if (node.Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
                {
                    state = ftdi.OpenBySerialNumber(node.SerialNumber);

                    if (state != FTDI.FT_STATUS.FT_OK)
                    {
                        continue;
                    }

                    String tmpPort;
                    state = ftdi.GetCOMPort(out tmpPort);

                    if (state != FTDI.FT_STATUS.FT_OK)
                    {
                        Trace.WriteLine("Failed to get COM-Port (error " + state.ToString() + ")");
                        ftdi.Close();
                        continue;
                    }

                    if (tmpPort == null || tmpPort.Length == 0)
                    {
                        Trace.WriteLine(string.Format("Failed to get COM-Port for device {0}.", node.SerialNumber));
                        ftdi.Close();
                        continue;
                    }

                    FTDI.FT232R_EEPROM_STRUCTURE eepromData = new FTDI.FT232R_EEPROM_STRUCTURE();

                    try
                    {
                        state = ftdi.ReadFT232REEPROM(eepromData);
                    }
                    catch (Exception)
                    {
                        Trace.WriteLine("Exception thrown when calling ReadFT232REEPROM");
                    }

                    if (state != FTDI.FT_STATUS.FT_OK)
                    {
                        Trace.WriteLine("Failed to read device EEPROM (error " + state.ToString() + ")");
                        ftdi.Close();
                        continue;
                    }

                    ftdi.Close();

                    if (!eepromData.Manufacturer.Equals("EnOcean GmbH") && !eepromData.Manufacturer.Equals("Viessmann") && !eepromData.Manufacturer.Equals("Eltako GmbH"))
                    {
                        Trace.WriteLine(String.Format("Wrong Manufacturer {0}", eepromData.Manufacturer));
                        continue;
                    }

                    if (tmpPort != null)
                    {
                        port = tmpPort;
                        break;
                    }
                }
            }

            return(port);
        }