Exemplo n.º 1
0
 /// <summary>
 /// Checks if we are connected to an FTDI device
 /// </summary>
 /// <returns></returns>
 public bool CheckDeviceStatus()
 {
     FTDI.FT_STATUS connected = FTDIDevice.GetSerialNumber(out serialNumber);
     if (connected != FTDI.FT_STATUS.FT_OK)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
        // returns true on success
        static bool openSerialPort(TcpClient conn, string snToConnect)
        {
            NetworkStream ns = conn.GetStream();

            ns.WriteTimeout = 10;
            ns.ReadTimeout  = 10;
            byte[] msg;

            FTDI clientDevice = new FTDI();

            FTDI.FT_STATUS ftdiRet = clientDevice.OpenBySerialNumber(snToConnect);
            ftdiRet |= clientDevice.SetBaudRate(1250000);
            ftdiRet |= clientDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_2, FTDI.FT_PARITY.FT_PARITY_ODD);
            ftdiRet |= clientDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0);
            ftdiRet |= clientDevice.SetTimeouts(8000, 100);
            ftdiRet |= clientDevice.InTransferSize(65536);
            ftdiRet |= clientDevice.SetDTR(true);

            if (ftdiRet != FTDI.FT_STATUS.FT_OK)
            {
                //throw new IOException("Failed to open FTDI device");
                Console.WriteLine("ERR: Failed to open FTDI device\n");
                msg = ASCIIEncoding.ASCII.GetBytes("ERR: Failed to open FTDI device\n");
                ns.Write(msg, 0, msg.Length);
                ns.Close();
                conn.Close();
                return(false);
            }

            string desc = "X", sn = "X";

            clientDevice.GetDescription(out desc);
            clientDevice.GetSerialNumber(out sn);
            Console.WriteLine("OK: Opened FTDI device {0}-{1}", desc, sn);
            msg = ASCIIEncoding.ASCII.GetBytes("OK: Opened:" + snToConnect + "\n");
            ns.Write(msg, 0, msg.Length);


            CancellationTokenSource ct = new CancellationTokenSource();
            Thread deviceThread        = new Thread((ParameterizedThreadStart)doServer);

            deviceThreads[snToConnect] = new Tuple <Thread, CancellationTokenSource>(deviceThread, ct);
            Tuple <FTDI, TcpClient, CancellationToken> threadArg = new Tuple <FTDI, TcpClient, CancellationToken>(clientDevice, conn, ct.Token);

            deviceThread.IsBackground = true;
            deviceThread.Start(threadArg);

            return(true);
        }
Exemplo n.º 3
0
        private void IdentifyDevice()
        {
            FTDI.FT_STATUS status;
            uint           devcount = 0;

            ftHandle.GetNumberOfDevices(ref devcount);
            if (devcount > 0)
            {
                status = ftHandle.GetDeviceList(deviceInfos);
                if (status == FTDI.FT_STATUS.FT_OK)
                {
                    for (int i = 0; i < devcount; i++)
                    {
                        deviveComboBox.Items.Add(deviceInfos[i].Description);
                    }
                }
                status = ftHandle.GetSerialNumber(out serialNumber);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method detects and configures Sainsmart 8ch USB outputs automatically (possibly FT245RBitbangController in general?).
        /// </summary>
        /// <param name="Cabinet">The cabinet object to which the automatically detected IOutputController objects are added if necessary.</param>
        public void AutoConfig(Cabinet Cabinet)
        {
            //Log.Write("FT245RBitbangControllerAutoConfigurator.AutoConfig started");

            FTDI              dummyFTDI     = new FTDI();
            uint              amountDevices = 0;
            string            callResult    = "";
            List <DeviceInfo> devicelist    = new List <DeviceInfo>();

            //fetch amount of devices, then kill instance
            callResult = dummyFTDI.GetNumberOfDevices(ref amountDevices).ToString();
            //Log.Write("FT245RBitbangControllerAutoConfigurator.AutoConfig: amount of devices detected=" + amountDevices + ", callresult=" + callResult);
            dummyFTDI.Close();
            dummyFTDI = null;

            //connect to each device using ftdi directly, snag the index and serial, then close and gc
            //do this seperate from adding instances in case multiple instances of ftdi would cause issues, as doing this too aggressive will cause locked exe / dll on exit
            for (uint i = 0; i < amountDevices; i++)
            {
                using (FTDI connectFTDI = new FTDI())
                {
                    string deviceSerial = "";
                    string deviceDesc   = "";
                    connectFTDI.OpenByIndex(i);
                    connectFTDI.GetSerialNumber(out deviceSerial);
                    connectFTDI.GetDescription(out deviceDesc);
                    devicelist.Add(new DeviceInfo(deviceSerial, deviceDesc));
                    //Log.Write("i=" + i + ", serial device=" + deviceSerial);
                    connectFTDI.Close();
                }
            }

            //next add instances of the controller to output, and all controller outputs
            for (int deviceIndex = 0; deviceIndex < devicelist.Count; deviceIndex++)
            {
                FT245RBitbangController FTDevice = new FT245RBitbangController();
                FTDevice.Name         = "FT245RBitbangController {0}".Build(deviceIndex);
                FTDevice.SerialNumber = devicelist[deviceIndex].serial;
                FTDevice.Description  = devicelist[deviceIndex].desc;
                FTDevice.Id           = deviceIndex;

                Log.Write("FT245RBitbangControllerAutoConfigurator.AutoConfig.. Detected FT245RBitbangController" + "["
                          + deviceIndex + "], name=" + FTDevice.Name
                          + ", description: " + FTDevice.Description
                          + ", serial #" + FTDevice.SerialNumber);

                if (!Cabinet.OutputControllers.Contains(FTDevice.Name))
                {
                    Cabinet.OutputControllers.Add(FTDevice);

                    Log.Write("Detected and added FT245RBitbangController Id {0} with name {1}".Build(deviceIndex, FTDevice.Name));

                    //+40 used to define start of directoutputconfig[40...].xml
                    if (!Cabinet.Toys.Any(T => T is LedWizEquivalent && ((LedWizEquivalent)T).LedWizNumber == deviceIndex - 0 + 40))
                    {
                        LedWizEquivalent LWE = new LedWizEquivalent();
                        LWE.LedWizNumber = deviceIndex - 0 + 40;
                        LWE.Name         = "{0} Equivalent 1".Build(FTDevice.Name);
                        for (int outputIndex = 1; outputIndex <= 8; outputIndex++)
                        {
                            LedWizEquivalentOutput LWEO = new LedWizEquivalentOutput()
                            {
                                OutputName = "{0}\\{0}.{1:00}".Build(FTDevice.Name, outputIndex), LedWizEquivalentOutputNumber = outputIndex
                            };
                            LWE.Outputs.Add(LWEO);
                        }
                        if (!Cabinet.Toys.Contains(LWE.Name))
                        {
                            Cabinet.Toys.Add(LWE);
                            Log.Write("Added LedwizEquivalent Nr. {0} with name {1} for PacUIO with Id {2}".Build(LWE.LedWizNumber, LWE.Name, deviceIndex));
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        static void doServer(object argObj)
        {
            var               arg          = (Tuple <FTDI, TcpClient, CancellationToken>)argObj;
            FTDI              clientDevice = arg.Item1;
            TcpClient         clientConn   = arg.Item2;
            NetworkStream     clientStream = clientConn.GetStream();
            CancellationToken ct           = arg.Item3;

            string ftdiSN = "";

            clientDevice.GetSerialNumber(out ftdiSN);
            if (ftdiSN != "")
            {
                ftdiSN = "(" + ftdiSN + ")";
            }

            uint bytesInDevice = 0;
            uint bytesDone     = 0;
            int  bytePos       = 0;
            int  bytesToWrite  = 0;

            byte[] buf  = new byte[1024 * 1024];
            byte[] buf2 = new byte[1024 * 1024]; // used when writing to FTDI, to start in the middle in case a write doesn't send all data

            while (true)
            {
                // Check if network connection has closed
                if (clientConn.Client.Poll(0, SelectMode.SelectRead) && !clientStream.DataAvailable)
                {
                    Console.WriteLine("Connection closed");
                    clientStream.Close();
                    clientConn.Close();
                    clientDevice.Close();
                    break;
                }

                // Check if we have been asked to end
                if (ct.IsCancellationRequested)
                {
                    Console.WriteLine("Program ending");
                    clientStream.Close();
                    clientConn.Close();
                    clientDevice.Close();
                    break;
                }

                // FTDI -> Network
                if (clientDevice.GetRxBytesAvailable(ref bytesInDevice) != FTDI.FT_STATUS.FT_OK)
                {
                    throw new IOException("Failed to get number of RX bytes available in FTDI device.");
                }

                if (bytesInDevice > 0)
                {
                    Console.WriteLine("FTDI{0} -> Network", ftdiSN);
                    Console.WriteLine("  {0} bytes from FTDI", bytesInDevice);
                    if (bytesInDevice > buf.Length)
                    {
                        bytesInDevice = (uint)buf.Length;
                    }

                    clientDevice.Read(buf, bytesInDevice, ref bytesDone);
                    clientStream.Write(buf, 0, (int)bytesDone);
                    Console.WriteLine("  Wrote {0} bytes to network", bytesDone);
                }

                // FTDI <- Network
                if (clientStream.DataAvailable)
                {
                    Console.WriteLine("FTDI{0} <- Network", ftdiSN);

                    bytePos      = 0;
                    bytesToWrite = clientStream.Read(buf, 0, buf.Length);

                    Console.WriteLine("  {0} bytes from network", bytesToWrite);


                    clientDevice.Write(buf, bytesToWrite, ref bytesDone);
                    Console.WriteLine("  Wrote {0} bytes to FTDI", bytesDone);

                    /*
                     * do
                     * {
                     *  Array.Copy(buf, bytePos, buf2, 0, bytesToWrite);
                     *  clientDevice.Write(buf2, bytesToWrite, ref bytesDone);
                     *  Console.WriteLine("  Wrote {0} bytes to FTDI", bytesDone);
                     *
                     *  bytesToWrite -= (int)bytesDone;
                     *  bytePos += (int)bytesDone;
                     * } while (bytesToWrite > 0);
                     */
                }

                //Thread.Yield();
            }
        }