コード例 #1
0
ファイル: Relay.cs プロジェクト: mdblack98/TunerUtil
        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);
        }
コード例 #2
0
ファイル: DebugMsg.cs プロジェクト: mdblack98/TunerUtil
        public static void DebugAddMsg(DebugEnum level, string msg)
        {
            DebugMsg msgItem = new DebugMsg
            {
                Text  = Form1.MyTime() + " " + msg,
                Level = level
            };

            DebugAddMsg(msgItem);
        }
コード例 #3
0
 public void SetText(DebugEnum debugLevel, string v)
 {
     if (debugLevel <= DebugLevel || debugLevel == DebugEnum.LOG || Tuning)
     {
         DebugMsg myMsg = new DebugMsg
         {
             Text  = MyTime() + " " + v,
             Level = debugLevel
         };
         //msgQueue.Enqueue(myMsg);
         DebugAddMsg(myMsg);
     }
 }
コード例 #4
0
ファイル: Relay.cs プロジェクト: mdblack98/TunerUtil
 public void Close()
 {
     // Put some delays in here as the 8-channel relay was turning on some relays
     // I had 4-channel as Relay1 and 8-channel as Relay2
     // The delays seem to have cured that problem
     if (ftdi != null && ftdi.IsOpen)
     {
         //AllOff();
         DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI close#3\n");
         ftdi.Close();
         Thread.Sleep(200);
     }
     ftdi = null;
     Thread.Sleep(200);
 }
コード例 #5
0
ファイル: Relay.cs プロジェクト: mdblack98/TunerUtil
        public byte Status()
        {
            relayError = false;
            if (ftdi == null)
            {
                //Open(false);
                DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI is null??\n");
                relayError = true;
                return(0x0);
            }
            Monitor.Enter(ftdi);
            bool local = false;

            if (ftdi != null && !ftdi.IsOpen) // then we'll open and close the device inside here
            {
                Open();
                local = true;
            }
            errMsg = null;
            byte bitModes = 0x00;

            try
            {
                FTD2XX_NET.FTDI.FT_STATUS status = ftdi.GetPinStates(ref bitModes);
                if (status != FT_STATUS.FT_OK)
                {
                    DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "FTDI status != FT_STATUS_.FT_OK, " + status + " != " + FT_STATUS.FT_OK + "\n");
                    errMsg = "Oops!!";
                    ftdi.CyclePort();
                    Monitor.Exit(ftdi);
                    relayError = true;
                    return(0x0);
                }
            }
            catch (Exception ex)
            {
                errMsg = "Relay Status failed\n" + ex.Message;
            }
            if (local)
            {
                Close();
            }
            if (ftdi != null)
            {
                Monitor.Exit(ftdi);
            }
            return(bitModes);
        }
コード例 #6
0
ファイル: Relay.cs プロジェクト: mdblack98/TunerUtil
        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");
        }
コード例 #7
0
ファイル: Relay.cs プロジェクト: mdblack98/TunerUtil
 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);
 }
コード例 #8
0
ファイル: DebugMsg.cs プロジェクト: mdblack98/TunerUtil
 public static void DebugAddMsg(DebugMsg msg)
 {
     msgQueue.Enqueue(msg);
 }
コード例 #9
0
ファイル: Relay.cs プロジェクト: mdblack98/TunerUtil
        public void Set(int nRelay, byte status)
        {
            if (ftdi == null)
            {
                Open(false);
            }
            if (nRelay == 0)
            {
                return;
            }
            Monitor.Enter(ftdi);
            errMsg = null;
            try
            {
                // Get status
                byte[] data     = { 0xff, 0xff, 0x00 };
                uint   nWritten = 0;
                byte   flags;
                byte   bitModes = 0x00;

                ftdi.GetPinStates(ref bitModes);

                if (status != 0)
                {
                    flags = (byte)(bitModes | (1u << (nRelay - 1)));
                }
                else
                {
                    flags = (byte)(bitModes & (~(1u << (nRelay - 1))));
                }
                DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "Relay #" + nRelay + " set=" + status + " bits=" + bitModes + " newBits=" + flags + "\n");
                data[2] = flags;
                ftdi.Write(data, data.Length, ref nWritten);
                if (nWritten == 0)
                {
                    Close();
                    errMsg = "Unable to write to relay...disconnected?";
                    DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.ERR, errMsg);
                    Monitor.Exit(ftdi);
                    return;
                    //throw new Exception("Unable to write to relay...disconnected?");
                }
                Thread.Sleep(100);
                byte bitModes2 = 0x00;
                ftdi.GetPinStates(ref bitModes2);
                DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.LOG, "Relay #" + nRelay + " statusbits=" + bitModes2 + "\n");
                if (status != 0) // check we set it
                {
                    if ((bitModes2 & (1u << (nRelay - 1))) == 0)
                    {
                        errMsg = "Relay did not get set!";
                        DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.ERR, errMsg);
                        Close();
                        Monitor.Exit(ftdi);
                        return;
                    }
                }
                //Status();
            }
            catch (Exception ex)
            {
                errMsg = "Relay Set failed\n" + ex.Message;
                DebugMsg.DebugAddMsg(DebugMsg.DebugEnum.ERR, errMsg);
            }
            Monitor.Exit(ftdi);
        }