Example #1
0
        /// <summary>
        /// Activate/De-activate a specific relay
        /// </summary>
        /// <param name="relay"></param>
        /// <param name="state"></param>
        public async static void Set(RelayNumbers relay, Relaystate state)
        {
            await Task.Run(() =>
            {
                uint numBytes  = 1;
                uint relayAddr = (uint)relay;
                byte[] @out    = { 0x00 };
                byte pins      = 0x00;
                byte output    = 0x00;

                // Get current pin state.
                myFtdiDevice.GetPinStates(ref pins);

                switch (state)
                {
                case Relaystate.ON: output = (byte)(pins | relayAddr); break;

                case Relaystate.OFF: output = (byte)(pins & ~(relayAddr)); break;
                }
                myFtdiDevice.GetPinStates(ref pins);
                Pins = pins;

                @out[0] = output;
                myFtdiDevice.Write(@out, 1, ref numBytes);
            });
        }
Example #2
0
        public bool Status(int nRelay)
        {
            Monitor.Enter(ftdi);
            //Open();
            errMsg = null;
            try
            {
                byte bitModes = 0;
                ftdi.GetPinStates(ref bitModes);
                if ((bitModes & (1 << (nRelay - 1))) != 0)
                {
                    DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI close#4\n");
                    Close();
                    Monitor.Exit(ftdi);
                    return(true);
                }
            }
#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 Status failed\n" + ex.Message;
            }
            Monitor.Exit(ftdi);
            return(false);
        }
Example #3
0
        /// <summary>
        /// Read FTDI Pin State of a given Pin
        /// </summary>
        /// <param name="pin">Pin to read state from</param>
        /// <returns>Pin state (1 or 0)</returns>
        static int Get(byte pin)
        {
            byte state = 0;

            Ftdi.GetPinStates(ref state);

            return(state >> 1 & 1);
        }
 //-------------------------------------------------------------------------------------------------
 public FTDI.FT_STATUS checkPinState(ref byte pins)
 {
     ftStatus = SPI_Device.GetPinStates(ref pins);
     if (ftStatus != FTDI.FT_STATUS.FT_OK)
     {
         return(ftStatus);
     }
     return(ftStatus);
 }
Example #5
0
 public bool Status(int nRelay)
 {
     Monitor.Enter(ftdi);
     //Open();
     errMsg = null;
     try
     {
         byte bitModes = 0;
         ftdi.GetPinStates(ref bitModes);
         if ((bitModes & (1 << (nRelay - 1))) != 0)
         {
             DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI close#4\n");
             Close();
             Monitor.Exit(ftdi);
             return(true);
         }
     }
     catch (Exception ex)
     {
         errMsg = "Relay Status failed\n" + ex.Message;
     }
     Monitor.Exit(ftdi);
     return(false);
 }
        private static FTDI.FT_STATUS ftBitRead(byte signal, ref int data, FTDI myFtdiDevice)
        {
            FTDI.FT_STATUS status   = FTDI.FT_STATUS.FT_OK;
            byte           pinState = 0x00;

            byte[] rdBuf = new byte[1];

            status = myFtdiDevice.GetPinStates(ref pinState);

            if (status == FTDI.FT_STATUS.FT_OK)
            {
                data = pinState & signal;
            }

            return(status);
        }
        private static FTDI.FT_STATUS ftBitWrite(byte signal, int data, FTDI myFtdiDevice)
        {
            byte mask = (byte)~signal;

            byte[] dataBuffer = new byte[1];
            byte   pinState   = 0x00;
            uint   numByteWr  = 0;

            FTDI.FT_STATUS status = FTDI.FT_STATUS.FT_OK;

            status = myFtdiDevice.GetPinStates(ref pinState);

            dataBuffer[0] = (byte)((pinState & mask) | (data * signal));
            status        = myFtdiDevice.Write(dataBuffer, 1, ref numByteWr);
            return(status);
        }
Example #8
0
        /// <summary>
        /// Activate/De-activate a specific relay
        /// </summary>
        /// <param name="Rnum"></param>
        /// <param name="state"></param>
        public void RelaySwitch(Relaynum Rnum, Relaystate state)
        {
            uint numBytes = 1;
            int  relay    = 0x00;

            byte[] Out    = { 0x00 };
            byte   pins   = 0x00;
            byte   output = 0x00;

            // Find which relays are ON/OFF
            myFtdiDevice.GetPinStates(ref pins);

            switch (Rnum)
            {
            case Relaynum.ONE:
                relay = 0x01;
                break;

            case Relaynum.TWO:
                relay = 0x02;
                break;

            case Relaynum.THREE:
                relay = 0x04;
                break;

            case Relaynum.FOUR:
                relay = 0x08;
                break;
            }

            switch (state)
            {
            case Relaystate.ON:
                output = (byte)(pins | relay);
                break;

            case Relaystate.OFF:
                output = (byte)(pins & ~(relay));
                break;
            }

            Out[0] = output;
            myFtdiDevice.Write(Out, 1, ref numBytes);
        }
        /// <summary>
        /// Activate/De-activate a specific relay
        /// </summary>
        /// <param name="relayCode">The relay's code.</param>
        /// <param name="state">If set to <c>true</c> switch on, otherwise, switch off.</param>
        public void RelaySwitch(byte relayCode, bool state)
        {
            uint numBytes = 1;

            byte[] datas = startup;
            byte   pins  = 0x00;

            // Find which relays are ON/OFF
            ftdiDevice.GetPinStates(ref pins);

            // Permut
            datas[0] = state ?
                       (byte)(pins | relayCode) :
                       (byte)(pins & ~relayCode);

            // Set state
            ftdiDevice.Write(datas, 1, ref numBytes);
        }
Example #10
0
        public bool connect(string s_description, uint u32_baudrate, bool flowCtrl)
        {
            logFile  = new System.IO.StreamWriter(@".\log.txt", false);
            ftStatus = FtdiDevice.OpenByDescription(s_description);
            if (ftStatus == FTDI.FT_STATUS.FT_DEVICE_NOT_FOUND || ftStatus == FTDI.FT_STATUS.FT_INVALID_HANDLE || ftStatus == FTDI.FT_STATUS.FT_DEVICE_NOT_OPENED)
            {
                isValid = false;
                logFile.Close();
                return(false);
            }

            ftStatus = FtdiDevice.SetBaudRate(u32_baudrate);
            ftStatus = FtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);
            if (flowCtrl)
            {
                ftStatus = FtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0, 0);
            }
            else
            {
                ftStatus = FtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0, 0);
            }

            ftStatus = FtdiDevice.SetTimeouts(5000, 5000);

            byte oldBitMode = 0;

            ftStatus = FtdiDevice.GetPinStates(ref oldBitMode);

            receivingThread = new Thread(new ThreadStart(USBReceivingRoutine));
            receivingThread.IsBackground = true;

            processingThread = new Thread(new ThreadStart(processIncomingData));
            processingThread.IsBackground = true;
            receivingThread.Start();
            processingThread.Start();

            waitForNewSerialData = new AutoResetEvent(false);

            return(true);
        }
Example #11
0
        /// <summary>
        /// Attempts to open the serial port, reset the device, and set the configuration
        /// </summary>
        /// <param name="serial"></param>
        /// <returns></returns>
        private bool InitializeDevice(string serial = "")
        {
            FTDI.FT_STATUS deviceStatus;
            if (serial != "")
            {
                // Open the device based on serial number
                deviceStatus = FTDIDevice.OpenBySerialNumber(serial);
                if (deviceStatus != FTDI.FT_STATUS.FT_OK)
                {
                    return(false);
                }
            }
            else
            {
                // Grab the first FTDI device found
                deviceStatus = FTDIDevice.OpenByIndex(0);
                if (deviceStatus != FTDI.FT_STATUS.FT_OK)
                {
                    return(false);
                }
            }

            // Reset the device and check status again
            deviceStatus = FTDIDevice.ResetDevice();
            if (deviceStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            // Set Baud Rate
            deviceStatus = FTDIDevice.SetBaudRate(921600);
            if (deviceStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            // Set Bit Bang
            deviceStatus = FTDIDevice.SetBitMode(255, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG);
            if (deviceStatus != FTDI.FT_STATUS.FT_OK)
            {
                return(false);
            }

            //Read Relays Status
            deviceStatus = FTDIDevice.GetPinStates(ref sentBytes[0]);
            if ((sentBytes[0] & 2) == 0)
            {
                currentRelayStates[0] = true;
            }
            if ((sentBytes[0] & 8) == 0)
            {
                currentRelayStates[1] = true;
            }
            if ((sentBytes[0] & 32) == 0)
            {
                currentRelayStates[2] = true;
            }
            if ((sentBytes[0] & 128) == 0)
            {
                currentRelayStates[3] = true;
            }

            return(true);
        }
Example #12
0
        internal DeviceConnectionState Connect(bool Emulation, out string Message)
        {
            m_Emulation = Emulation;
            DeviceConnectionState state;

            Message = "";
            m_Stop  = false;

            if (m_Emulation)
            {
                IsConnected = true;
                state       = DeviceConnectionState.ConnectionSuccess;

                return(state);
            }

            if (!m_MyFtdiDevice.IsOpen)
            {
                IsConnected = false;
                m_FtStatus  = m_MyFtdiDevice.OpenByDescription(DESCRIPTION_NAME);

                if (m_FtStatus == FTDI.FT_STATUS.FT_OK)
                {
                    m_FtStatus = m_MyFtdiDevice.SetBaudRate(115200);
                }
                if (m_FtStatus == FTDI.FT_STATUS.FT_OK)
                {
                    m_FtStatus = m_MyFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);
                }
                if (m_FtStatus == FTDI.FT_STATUS.FT_OK)
                {
                    m_FtStatus = m_MyFtdiDevice.SetBitMode(DIR_MASK, FTDI.FT_BIT_MODES.FT_BIT_MODE_MPSSE);
                }

                if (m_FtStatus == FTDI.FT_STATUS.FT_OK)
                {
                    LedRedSwitch(false);
                    LedGreenSwitch(false);
                    IsConnected = true;

                    m_FtStatus = m_MyFtdiDevice.GetPinStates(ref m_BitMode);

                    if (m_FtStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        if ((m_BitMode & B2_MASK) == 0 && !m_Buttons[1])
                        {
                            m_Buttons[1]        = true;
                            IsStopButtonPressed = m_Buttons[1];
                            Cache.Net.CallbackManager.GatewayButtonPressHandler(ComplexButtons.ButtonStopFTDI, m_Buttons[1]);
                        }
                        else if ((m_BitMode & B2_MASK) != 0 && m_Buttons[1])
                        {
                            m_Buttons[1]        = false;
                            IsStopButtonPressed = m_Buttons[1];
                            Cache.Net.CallbackManager.GatewayButtonPressHandler(ComplexButtons.ButtonStopFTDI, m_Buttons[1]);
                        }
                    }
                    state = DeviceConnectionState.ConnectionSuccess;

                    ThreadPool.QueueUserWorkItem(PoolingRoutine);
                }
                else
                {
                    m_MyFtdiDevice.Close();
                    Message = "FT status " + m_FtStatus;
                    state   = DeviceConnectionState.ConnectionFailed;
                }
            }
            else
            {
                state = DeviceConnectionState.ConnectionSuccess;
            }

            return(state);
        }
Example #13
0
        private bool ConnectToRelayBoard()
        {
            //Get serial number of device with index 0
            if (myFtdiDevice == null)
            {
                return(false);
            }
            ftStatus = myFtdiDevice.OpenByIndex(0);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                log.Error("OpenByIndex returned: " + ftStatus.ToString());
                return(false);
            }
            log.Debug("OpenByIndex returned: " + ftStatus.ToString());

            //Reset device
            ftStatus = myFtdiDevice.ResetDevice();
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                log.Error("ResetDevice returned: " + ftStatus.ToString());
                return(false);
            }
            log.Debug("ResetDevice returned: " + ftStatus.ToString());

            //Set Baud Rate
            ftStatus = myFtdiDevice.SetBaudRate(921600);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                log.Error("SetBaudRate(921600) returned: " + ftStatus.ToString());
                return(false);
            }
            log.Debug("ResetDevice returned: " + ftStatus.ToString());

            //Set Bit Bang
            ftStatus = myFtdiDevice.SetBitMode(255, FTD2XX_NET.FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                log.Error("SetBitMode(255, FTD2XX_NET.FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG) returned: " + ftStatus.ToString());
                return(false);
            }
            log.Error("SetBitMode(255, FTD2XX_NET.FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG) returned: " + ftStatus.ToString());

            //Read Relays Status
            log.Debug("--------------------------------Start reading devide pin states--------------------------");
            ftStatus = myFtdiDevice.GetPinStates(ref sentBytes[0]);

            log.Debug("GetPingStates returned " + ftStatus.ToString());

            if ((sentBytes[0] & 1) == 0)        //5 min light
            {
                Relay1Status = false;
                log.Debug("sentBytes[0] & 1 returned 0  ---Relay 1 Status OFF");
            }
            else
            {
                Relay1Status = true;
                log.Debug("sentBytes[0] & 1 returned " + (sentBytes[0] & 1) + "  --- Relay 1 Status ON");
            }

            if ((sentBytes[0] & 2) == 0)       //4 min light
            {
                log.Debug("sentBytes[0] & 2 returned 0  --- Relay 2 Status OFF");

                Relay2Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 2 returned " + (sentBytes[0] & 2) + "  --- Relay 2 Status ON");


                Relay2Status = true;
            }
            if ((sentBytes[0] & 4) == 0)        //1 min light
            {
                log.Debug("sentBytes[0] & 4 returned 0  --- Relay 3 Status OFF");

                Relay3Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 4 returned " + (sentBytes[0] & 4) + "  --- Relay 3 Status ON");

                Relay3Status = true;
            }
            if ((sentBytes[0] & 8) == 0)        //general recall light
            {
                log.Debug("sentBytes[0] & 8 returned 0  --- Relay 4 Status OFF");

                Relay4Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 8 returned " + (sentBytes[0] & 8) + "  --- Relay 4 Status ON");

                Relay4Status = true;
            }
            if ((sentBytes[0] & 16) == 0)       //horne connected
            {
                log.Debug("sentBytes[0] & 16 returned 0  --- Relay 5 Status OFF");

                Relay5Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 16 returned " + (sentBytes[0] & 16) + "  --- Relay 5 Status ON");

                Relay5Status = true;
            }
            if ((sentBytes[0] & 32) == 0)
            {
                log.Debug("sentBytes[0] & 32 returned 0  --- Relay 6 Status OFF");

                Relay6Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 32 returned " + (sentBytes[0] & 32) + "  --- Relay 6 Status ON");
                Relay6Status = true;
            }
            if ((sentBytes[0] & 64) == 0)
            {
                log.Debug("sentBytes[0] & 64 returned 0  --- Relay 7 Status OFF");

                Relay7Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 64 returned " + (sentBytes[0] & 64) + "  --- Relay 7 Status ON");

                Relay7Status = true;
            }
            if ((sentBytes[0] & 128) == 0)
            {
                log.Debug("sentBytes[0] & 128 returned 0  --- Relay 8 Status OFF");

                Relay8Status = false;
            }
            else
            {
                log.Debug("sentBytes[0] & 128 returned " + (sentBytes[0] & 128) + "  --- Relay 8 Status ON");
                Relay8Status = true;
            }
            return(true);
        }