public void ExecutePackage(Package package)
        {
            _client = new TcpClient();
            _client.Connect(_endPoint);

            if (_client.Connected)
            {
                NetworkStream stream = _client.GetStream();
                stream.Write(package.ByteValue(), 0, package.Length());
            }
            _client.Close();
        }
        public string ExecuteQuestion(Package package, bool returnDecimal)
        {
            string response = "";
            _client = new TcpClient();
            _client.Connect(_endPoint);

            if (_client.Connected)
            {
                NetworkStream stream = _client.GetStream();
                stream.Write(package.ByteValue(), 0, package.Length());

                byte[] responseBuffer = new byte[128];
                stream.Read(responseBuffer, 0, responseBuffer.Length);

                int startPosition = findOccurence(responseBuffer, 0x21); // EOF byte
                int endPosition = findOccurence(responseBuffer, 0x1A); // EOF byte

                if(startPosition < endPosition)
                {
                    if (returnDecimal)
                    {
                        startPosition = endPosition - 2;
                    }

                    byte[] data = new byte[endPosition - startPosition];
                    Buffer.BlockCopy(responseBuffer, startPosition, data, 0, data.Length);
                    response = Encoding.ASCII.GetString(data);

                    if(returnDecimal)
                    {
                        response = Convert.ToInt32(response, 16).ToString();
                    }
                }

            }
            _client.Close();

            return response;
        }
        static int Main(string[] args)
        {
            programArguments = new Arguments(args);

            if( ! programArguments.HasArgument("noConsole"))
            {
                AttachConsole(-1);
            }

            if (CheckCommandListHelpParams()) return 0; // List was requested and given, so exit without errorcode

            if( ! RequiredParametersAvailable())
            {
                ShowParameterHelp();
                return 1;
            }

            IPAddress ipAddress = ParseIPAddressParameter();
            if (ipAddress == null) return 2;

            var pkg = new Package(programArguments.GetArgument("command"));

            receiver = new Receiver(ipAddress);

            try
            {
                if (ExecuteVolumeCommands())
                {
                    return 0;
                }
            }catch(Exception ex)
            {
                HelpPrinter.PrintCopyright();
                HelpPrinter.ExceptionOccured(ex);
                return 3;
            }

            try
            {
                if (pkg.IsQuestion())
                {
                    string response = receiver.ExecuteQuestion(pkg, programArguments.HasArgument("outputDecimal"));
                    Console.Write(response);
                }
                else
                {
                    HelpPrinter.PrintCopyright();
                    receiver.ExecutePackage(pkg);
                }

            }catch(Exception ex)
            {
                HelpPrinter.PrintCopyright();
                HelpPrinter.ExceptionOccured(ex);
                return 3;
            }

            return 0;
        }
 private static int GetVolume()
 {
     Package pkg = new Package("MVLQSTN");
     return Int32.Parse(receiver.ExecuteQuestion(pkg, true));
 }
 private static void SetVolume(int volume)
 {
     if (volume < 0)
     {
         volume = 0;
     }else if(volume > 80)
     {
         volume = 80;
     }
     Package pkg = new Package("MVL" + volume.ToString("X").PadLeft(2, '0'));
     receiver.ExecutePackage(pkg);
 }