Example #1
0
        public bool GetInputGpioValues()
        {
            // this is just for debugging
            //FileStream fileStream = new FileStream("C:\\GpioIntervalLog.txt", FileMode.Append);
            //StreamWriter sw = new StreamWriter(fileStream);

            bool          success = false;
            StringBuilder command = new StringBuilder("");

            //build the command to read INPUTS
            if (wiznetGpioCfg.GetGpioModeString(wiznetGpioCfg.GpioA).Equals("INPUT"))
            {
                if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioA).Equals("NORMAL"))
                {
                    command.Append("GA\r\n");
                }
                else if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioA).Equals("PULSE"))
                {
                    command.Append("TA\r\n");
                }
            }
            if (wiznetGpioCfg.GetGpioModeString(wiznetGpioCfg.GpioB).Equals("INPUT"))
            {
                if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioB).Equals("NORMAL"))
                {
                    command.Append("GB\r\n");
                }
                else if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioB).Equals("PULSE"))
                {
                    command.Append("TB\r\n");
                }
            }
            if (wiznetGpioCfg.GetGpioModeString(wiznetGpioCfg.GpioC).Equals("INPUT"))
            {
                if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioC).Equals("NORMAL"))
                {
                    command.Append("GC\r\n");
                }
                else if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioC).Equals("PULSE"))
                {
                    command.Append("TC\r\n");
                }
            }
            if (wiznetGpioCfg.GetGpioModeString(wiznetGpioCfg.GpioD).Equals("INPUT"))
            {
                if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioD).Equals("NORMAL"))
                {
                    command.Append("GD\r\n");
                }
                else if (wiznetGpioCfg.GetGpioDigModeString(wiznetGpioCfg.GpioD).Equals("PULSE"))
                {
                    command.Append("TD\r\n");
                }
            }
            if (command.ToString().Equals(System.String.Empty))
            {
                //sw.WriteLine("GetInputGpioValues NO COMMAND - ALL OUTPUTS?");
                //sw.Close();
                return(false);
            }
            // build the complete network command
            string logText;

            byte[] cmd_hex   = { 0x4d, 0x41, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0d, 0x0a, 0x50, 0x57, 0x20, 0x0d, 0x0a };
            byte[] cmdBytes  = Encoding.ASCII.GetBytes(command.ToString());
            int    final_len = cmd_hex.Length + cmdBytes.Length;

            // reserve the final command buffer
            byte[] final_cmd = new byte[final_len + 1];
            // combine the network header and command
            Array.ConstrainedCopy(cmd_hex, 0, final_cmd, 0, cmd_hex.Length);
            Array.ConstrainedCopy(cmdBytes, 0, final_cmd, cmd_hex.Length, cmdBytes.Length);
            // Send the command
            Connection.Write(final_cmd, 0, final_len, CommUtils.ProtocolLogFormats.Hex, out logText);
            byte[] par     = new byte[100];
            int    partial = Connection.Read(par, 0, 15, 1000);

            if (partial == 0)
            {
                // Timeout
                //sw.WriteLine("GetInputGpioValues Timeout");
                //sw.Close();
                return(false);
            }

            string tmp = System.String.Empty;

            while (true)
            {
                tmp = Connection.ReadLine(100, out logText);
                if (tmp == null)
                {
                    // Timeout
                    break;
                }
                else
                {
                    try
                    {
                        if (!wiznetGpioCfg.DecodeCommand(tmp.Trim()))
                        {
                            DiscardConnectionData();
                            //sw.WriteLine("GetInputGpioValues Fail decode");
                            //sw.Close();
                            return(false);
                        }
                        else
                        {
                            success = true;
                        }
                    }
                    catch //(Exception e)
                    {
                        //sw.WriteLine("GetInputGpioValues Exception: " + e);
                        //sw.Close();
                        DiscardConnectionData();
                        return(false);
                    }
                    continue;
                }
            }
            return(success);
        }