public bool OpenDeviceByLocation(uint LocationID)
        {
            if (USB_Interface.IsOpen)
            {
                USB_Interface.Close();
            }

            ExtLog.AddLine("Opening device");
            var ftStatus = USB_Interface.OpenByLocation(LocationID);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                ExtLog.AddLine("Failed to open device (error " + ftStatus + ")");
                return(false);
            }

            ExtLog.AddLine("Setting default bauld rate (" + GlobalProperties.baudRate + ")");
            ftStatus = USB_Interface.SetBaudRate(GlobalProperties.baudRate);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                ExtLog.AddLine("Failed to set Baud rate (error " + ftStatus + ")");
                return(false);
            }

            ExtLog.AddLine("Setting BitMode");
            ftStatus = USB_Interface.SetBitMode(GlobalProperties.portDirectionMask, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                ExtLog.AddLine("Failed to set BitMode (error " + ftStatus + ")");
                return(false);
            }

            UpdateProgress(100, true);
            return(true);
        }
        private void CommThread()
        {
            do
            {
                Thread.Sleep(10);
                if (active == false)
                {
                    continue;
                }

                if (!Connected && !quit)
                {
                    AttemptConnect();
                }
                else
                {
                    uint bytesRead = 0;
                    lock (ftdi)
                    {
                        // read any available data
                        uint           bytesAvail = 0;
                        FTDI.FT_STATUS stat       = ftdi.GetRxBytesAvailable(ref bytesAvail);                                                   // How much data is available from the serial port?
                        if (stat == FTDI.FT_STATUS.FT_IO_ERROR)
                        {
                            // If we got an error, the port has likely been closed / unplugged - go back to waiting
                            ftdi.Close();
                            connected = false;
                            if (ConnectionEnded != null)
                            {
                                ConnectionEnded();
                            }
                            continue;
                        }

                        if (bytesAvail > 0 && !quit)
                        {
                            uint toRead = Math.Min(bytesAvail, (uint)rxBuffer.Length);
                            if (toRead > 0)
                            {
                                ftdi.Read(rxBuffer, toRead, ref bytesRead);
                            }
                        }
                    }

                    for (int i = 0; i < bytesRead; i++)
                    {
                        ProcessByte(rxBuffer[i]);
                        if (quit)
                        {
                            break;
                        }
                    }
                }
            } while(!quit);

            Disconnect();
        }
Exemplo n.º 3
0
 public void Close()
 {
     lock (sync)
     {
         if (ftdi.IsOpen)
         {
             ftdi.Close();
         }
         connection.Dispose();
         connection = null;
     }
 }
Exemplo n.º 4
0
 public void Open(uint locationId)
 {
     // Open first device in our list by serial number
     if (!mDevice.LoadLibrarySuccess)
     {
         return; // fail to load library
     }
     if (mDevice.IsOpen)
     {
         mDevice.Close();
     }
     mDevice.OpenByLocation(locationId);
 }
Exemplo n.º 5
0
        FTDI.FT_STATUS OpenRFIDDevice()
        {
            FTDI.FT_STATUS Status = FTDI.FT_STATUS.FT_OTHER_ERROR;

            Thread.Sleep(100);

            if (RFIDFtdiDevice.IsOpen)
            {
                Status = RFIDFtdiDevice.Close();
                if (Status != FTDI.FT_STATUS.FT_OK)
                {
                    return(Status);
                }
            }

            // One can modify the device descriptor to open by Description
            // Placed here for reference only
            //Status = RFIDFtdiDevice.OpenByDescription("FT232R USB UART RFID Reader");

            //Asume that we are the only FTDI device
            Status = RFIDFtdiDevice.OpenByIndex(0);
            if (Status != FTDI.FT_STATUS.FT_OK)
            {
                return(Status);
            }

            Status = RFIDFtdiDevice.SetBaudRate(2400);
            if (Status != FTDI.FT_STATUS.FT_OK)
            {
                return(Status);
            }

            Status = RFIDFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);
            if (Status != FTDI.FT_STATUS.FT_OK)
            {
                return(Status);
            }

            Status = RFIDFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x0, 00);
            if (Status != FTDI.FT_STATUS.FT_OK)
            {
                return(Status);
            }

            // Enable RFID Device
            Status = RFIDFtdiDevice.SetDTR(true);

            return(Status);
        }
Exemplo n.º 6
0
        public FTDIInfo FindFTDI()
        {
            var ft = new FTDI();

            uint deviceCount = 0;
            uint deviceID    = 0;

            var stat = ft.GetNumberOfDevices(ref deviceCount);

            FTDI.FT_DEVICE_INFO_NODE[] devices = new FTDI.FT_DEVICE_INFO_NODE[deviceCount];
            stat = ft.GetDeviceList(devices);

            foreach (var dev in devices)
            {
                try
                {
                    stat = ft.OpenByLocation(dev.LocId);
                    if (stat == FTDI.FT_STATUS.FT_OK)
                    {
                        ft.GetDeviceID(ref deviceID);
                        if (deviceID != 67330049)
                        {
                            ft.GetCOMPort(out var portName);
                            ft.Close();
                            return(new FTDIInfo(portName, deviceID));
                        }
                    }
                }
                catch
                {
                    try
                    {
                        if (ft.IsOpen)
                        {
                            ft.Close();
                        }
                    }
                    finally
                    {
                        if (ft.IsOpen)
                        {
                            ft.Close();
                        }
                    }
                }
            }
            return(null);
        }
Exemplo n.º 7
0
 public void connect(String serialNo)
 {
     if (!isOpen())
     {
         ftStatus  = _modbusMaster.OpenBySerialNumber(serialNo);
         ftStatus |= _modbusMaster.SetBaudRate(Constants.modbus_baudrate);
         ftStatus |= _modbusMaster.SetDataCharacteristics(Constants.modbus_databit, Constants.modbus_stopbit, Constants.modbus_parity);
         ftStatus |= _modbusMaster.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0);
         ftStatus |= _modbusMaster.SetTimeouts(1000, 1000);
         if (ftStatus != FTDI.FT_STATUS.FT_OK)
         {
             _modbusMaster.Close();
             throw new System.Exception("Could not open ModBus device with serial number " + Constants.modbus_serial);
         }
     }
 }
Exemplo n.º 8
0
 public void Dispose()
 {
     if (_ftdi != null)
     {
         _ftdi.Close();
     }
 }
Exemplo n.º 9
0
        static void showOne()
        {
            FTDI ftdi_handle = new FTDI();

            FTDI.FT_STATUS ft_status;
            uint           devcount = 0;

            ft_status = ftdi_handle.GetNumberOfDevices(ref devcount);
            if (ft_status != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("Error getting number of devices.");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("FTDI devices connected: {0}", devcount);
            if (devcount == 0)
            {
                return;
            }
            FTDI.FT_DEVICE_INFO_NODE[] ft_dev_list = new FTDI.FT_DEVICE_INFO_NODE[devcount];
            ft_status = ftdi_handle.GetDeviceList(ft_dev_list);
            var serial_numbers = ft_dev_list.Select(h => h.SerialNumber);

            foreach (string st in serial_numbers)
            {
                if (st != null && st.CompareTo("") != 0)
                {
                    Console.WriteLine("Serial: {0}", st);
                }
            }

            string def_serial = serial_numbers.FirstOrDefault();

            if (def_serial == null || def_serial.CompareTo("") == 0)
            {
                Console.WriteLine("Error getting device serial");
                Console.ReadKey();
                return;
            }
            ft_status = ftdi_handle.OpenBySerialNumber(def_serial);

            ft_status = ftdi_handle.SetBaudRate(1);
            ft_status = ftdi_handle.SetBitMode(0x07, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);

            uint buf = 0;

            int numberOfBytes;

            byte[] outb = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };

            int idx = 0;

            do
            {
                idx = (int)Char.GetNumericValue(Console.ReadKey().KeyChar);
                ftdi_handle.Write(outb, idx + 1, ref buf);
            } while (idx < 8);

            ftdi_handle.Close();
        }
Exemplo n.º 10
0
 public void Dispose()
 {
     if (ftdi.IsOpen)
     {
         ftdi.Close();
     }
 }
Exemplo n.º 11
0
        public void Close()
        {
            if (cts != null)
            {
                cts.Cancel();
                try
                {
                    if (txTsk != null)
                    {
                        txTsk.Wait();
                    }
                }
                catch (Exception)
                {
                }
                try
                {
                    if (rxTsk != null)
                    {
                        rxTsk.Wait();
                    }
                }
                catch (Exception)
                {
                }
                cts = null;
            }

            if (myFtdiDevice != null)
            {
                myFtdiDevice.Close();
            }
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            FTDI ftdi = new FTDI();

            FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[2];
            FTDI.FT_STATUS             ftStatus   = ftdi.GetDeviceList(devicelist);
            Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK);
            ftStatus = ftdi.OpenByIndex(1);
            Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK);
            if (ftdi.IsOpen)
            {
                FTDI.FT_DEVICE type = FTDI.FT_DEVICE.FT_DEVICE_UNKNOWN;
                ftStatus = ftdi.GetDeviceType(ref type);
                Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK);
                Debug.Assert(type == FTDI.FT_DEVICE.FT_DEVICE_2232H);
                //ftdi.
                FTD2XX_NET.FTDI.FT2232H_EEPROM_STRUCTURE ee2232h = new FTDI.FT2232H_EEPROM_STRUCTURE();
                ftStatus = ftdi.ReadFT2232HEEPROM(ee2232h);
                Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK);
            }
            else
            {
                throw new Exception();
            }
            ftStatus = ftdi.Close();
            Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK);
        }
        private void Disconnect()
        {
            lock (FTDILocker)
            {
                if (FTDI != null)
                {
                    try
                    {
                        SendUpdate(0);
                    }
                    catch { }

                    if (FTDI.IsOpen)
                    {
                        try
                        {
                            FTDI.ErrorHandler(FTDI.Close());
                        }
                        catch (Exception)
                        {
                            Log.Exception("A exception occured when closing the FTDI chip {0}.".Build(SerialNumber));
                        }
                    }
                    FTDI = null;
                    Log.Write("Connection to FTDI chip {0} closed.".Build(SerialNumber));
                }
            }
        }
        public override void Close()
        {
            isInitialized = false;
            if (ftdi != null && ftdi.IsOpen)
            {
                // バッファをクリア
                ClearBuffer();

                // ポートを閉じる
                FT_STATUS ret = ftdi.ResetPort();
                if (ret == FT_STATUS.FT_OK)
                {
                    ret = ftdi.Close();
                    if (ret != FT_STATUS.FT_OK)
                    {
                        throw new InvalidOperationException("close failure.");
                    }
                }
                else
                {
                    throw new InvalidOperationException("reset failure.");
                }
                ftdi = null;
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Закрыть соединение с устройством.
        /// </summary>
        public void Close()
        {
            _device.Close();
            IsDeviceOpen = _device.IsOpen;
#if DEBUG
            Console.WriteLine($"Device[{DeviceIndex}] Open: {_device.IsOpen}");
#endif
        }
Exemplo n.º 16
0
 /// <summary>
 /// open the FTDI device, set frequency, then close the device
 /// </summary>
 public static void FrequencySetStable(uint ftdiDevice = 0, int freqTarget = 10_000_000)
 {
     ft_status = ftdi.OpenByIndex(ftdiDevice);
     ft_status = ftdi.SetBaudRate(9600);
     ft_status = ftdi.SetBitMode(255, 1);
     FrequencySet(freqTarget);
     ftdi.Close(); // detach cleanly;
 }
Exemplo n.º 17
0
 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (exchanger != null)
     {
         exchanger.Abort();
     }
     //FTDI.FT_STATUS ftStatus = ftdi.SetBitMode(0xFF, 0x00); //reset mode
     ftdi.Close();
 }
Exemplo n.º 18
0
 public FlowMeter()
 {
     _flowmeter = new FTDI();
     if (_flowmeter.IsOpen)
     {
         _flowmeter.Close();
     }
     _calibrationMode = false;
 }
Exemplo n.º 19
0
        public virtual void Dispose()
        {
            if (!ftdi.IsOpen)
            {
                return;
            }

            ftdi.Close();
        }
Exemplo n.º 20
0
 static void CloseDevice()
 {
     FTDI.FT_STATUS status = FTDI.FT_STATUS.FT_OK;
     status = _device.Close();
     if (status != FTDI.FT_STATUS.FT_OK)
     {
         throw new Exception("Couldn't close device.");
     }
 }
Exemplo n.º 21
0
        public void Open(string comPortNew, bool close = false)
        {
            errMsg = null;
            if (ftdi == null)
            {
                ftdi = new FTDI();
            }
            else
            {
                DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI close#1\n");
                ftdi.Close();
            }
            try
            {
                int index = comList.IndexOf(comPortNew);
                if (index < 0)
                {
                    index = -1;
                    return;
                }
                ftdi.OpenByIndex(comIndex[index]);
                ftdi.SetBitMode(0xff, 0x01);
                comPort      = comPortNew;
                relayNum     = (int)index + 1; // index is 0-based, our relayNum is 1-based for the GUI
                serialNumber = serialNums[index];
                if (close)
                {
                    DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI close#2\n");
                    ftdi.Close();
                    ftdi = null;
                }
                //AllOff();
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                errMsg = "Relay Open failed\n" + ex.Message + "\n";
                DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.ERR, errMsg);
                return;
            }
            // this msg doesn't display...why??
            //DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.ERR, "Relay#"+relayNum+" opened\n");
        }
Exemplo n.º 22
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("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;
        }
Exemplo n.º 23
0
 public void Dispose()
 {
     if (_pinDmd1Device != null)
     {
         Ftdi.SetBitMode(0x00, 0x0);
         Ftdi.Close();
         _pinDmd1Device = null;
         IsAvailable    = false;
     }
 }
Exemplo n.º 24
0
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     myFtdiDevice.Close();
     if (sw != null)
     {
         sw.Flush();
         sw.Close();
     }
     System.Environment.Exit(0);
 }
Exemplo n.º 25
0
        public static List <FDTIPort> FindFdtiUsbDevices()
        {
            ///////////////////////
            // Requires
            // FTD2XX_NET.dll
            ///////////////////////

            List <FDTIPort> ports = new List <FDTIPort>();

            FTDI _ftdi = new FTDI();


            UInt32 count = 0;

            FTDI.FT_STATUS status = _ftdi.GetNumberOfDevices(ref count);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("log.Warn: Unable to access FTDI");
                return(ports);
            }

            FTDI.FT_DEVICE_INFO_NODE[] list = new FTDI.FT_DEVICE_INFO_NODE[count];
            status = _ftdi.GetDeviceList(list);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("log.Warn: Unable to access FTDI");
                return(ports);
            }


            foreach (FTDI.FT_DEVICE_INFO_NODE node in list)
            {
                if ((status = _ftdi.OpenByLocation(node.LocId)) == FTDI.FT_STATUS.FT_OK)
                {
                    try
                    {
                        string comport;
                        _ftdi.GetCOMPort(out comport);

                        if (comport != null && comport.Length > 0)
                        {
                            //Console.WriteLine(node.Type);
                            ports.Add(new FDTIPort(comport, node.Description.ToString(), node.SerialNumber.ToString()));
                        }
                    }
                    finally
                    {
                        _ftdi.Close();
                    }
                }
            }

            //_ftdi.Dispose();
            return(ports);
        }
Exemplo n.º 26
0
 public void Close()
 {
     Console.WriteLine("Close Port");
     // don't throw any exception
     try
     {
         _Ftdi.Close();
     }
     catch (Exception)
     { }
 }
Exemplo n.º 27
0
        public override IObservable <Mat> Generate()
        {
            return(Observable.Create <Mat>((observer, cancellationToken) =>
            {
                return Task.Factory.StartNew(() =>
                {
                    var source = new FTDI();
                    var status = source.OpenByLocation((uint)LocationId);
                    if (status != FTDI.FT_STATUS.FT_OK)
                    {
                        throw new InvalidOperationException("Unable to open the FTDI device at the specified serial port.");
                    }

                    status = source.SetTimeouts(100, 100);
                    if (status != FTDI.FT_STATUS.FT_OK)
                    {
                        throw new InvalidOperationException("Unable to set timeouts on the FTDI device.");
                    }

                    Write(source, StartCommand);
                    var dataFrame = new short[ChannelCount, 1];
                    var packet = new byte[FrameSize];
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        Read(source, packet);
                        for (int i = 0; i < packet.Length; i += 20)
                        {
                            var channelOffset = (i / 20) * 2;

                            // Check if device data is valid
                            if (packet[i + 18] != 0x3F)
                            {
                                dataFrame[channelOffset + 0, 0] = (Int16)(((packet[i + 1] << 8) | packet[i + 2]) >> 4);
                                dataFrame[channelOffset + 1, 0] = (Int16)(((packet[i + 3] << 8) | packet[i + 4]) >> 4);
                            }
                            else
                            {
                                dataFrame[channelOffset + 0, 0] = -1;
                                dataFrame[channelOffset + 1, 0] = -1;
                            }
                        }

                        var dataOutput = Mat.FromArray(dataFrame);
                        observer.OnNext(dataOutput);
                    }

                    Write(source, StopCommand);
                    source.Close();
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default);
            }));
        }
Exemplo n.º 28
0
        // Protected implementation of Dispose pattern.
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                // Free any other managed objects here.
                //
                DisposeThread();
                myFtdiDevice.Close();
                myFtdiDevice = null;
            }

            // Free any unmanaged objects here.
            //
            disposed = true;
        }
Exemplo n.º 29
0
        private void StopThreads()
        {
            _shouldBeRunning = false;
            _getDeviceThread.Join();
            _writeDataThread.Join();

            FTDI.FT_STATUS status = _ftdi.Close();
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                throw new Exception("Unable to close the FTDI device.");
            }
        }
Exemplo n.º 30
0
 private void btnClose_Click(object sender, EventArgs e)
 {
     status = esp.Close();
     Console.WriteLine(status);
     if (status == FTDI.FT_STATUS.FT_OK)
     {
         log.AppendText("Closing port!\r\n");
     }
     else
     {
         log.AppendText("Error closing port: " + status + "\r\n");
     }
 }