예제 #1
0
        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();
        }
예제 #2
0
        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;
        }
예제 #3
0
        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;
        }
예제 #4
0
 private static int GetVolume()
 {
     Package pkg = new Package("MVLQSTN");
     return Int32.Parse(receiver.ExecuteQuestion(pkg, true));
 }
예제 #5
0
 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);
 }