Newbie has rough time with ready to use code

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 1389 Views - Last Post: 25 October 2017 - 12:45 PM Rate Topic: -----

#16 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 06:58 AM

I DID IT.

The array clear command would not work. I had to search in the API pages and find a particular command.

I started with this piece of code.
pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10300000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
Console.ReadLine();

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
Console.ReadLine();


I copied the commands of lines 1-5 to lines 7-11. In line #1 the integer is 10300000 while in line #7 is 10600000. The device I am «playing» with (a professional satellite receiver) changed to 10300000 and then to 10600000. However, I got the following messages.

Posted Image

The lines #3 and #9 display the same string, the display the message of the first SET command. In both lines #4 and #10 we see the pdu[0] thing.

I changed the code as follows.
pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10300000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
Console.ReadLine();

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[1].Oid.ToString(), response.Pdu[1].Value.ToString()));
Console.ReadLine();



The console messages became as following.
Posted Image
The first message says 10300000, the second says 10600000.

Let's see the reset command.

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10300000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
Console.ReadLine();

pduMRD4400.Reset();

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
Console.ReadLine();



I put a command in the middle of the groups of commands, line #07, and I changed the pdu[1] to pdu[0]. The satellite receiver did what I expected and I got the following messages.

Posted Image
It looks like the second screenshot.

One last thing that was pending was to execute pdu[1] with the reset command. The code got changed as follows.

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10300000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
Console.ReadLine();

pduMRD4400.Reset();

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
Console.WriteLine(String.Format("Agent response {0}: {1}",
         response.Pdu[1].Oid.ToString(), response.Pdu[1].Value.ToString()));
Console.ReadLine();


The commands got executed properly but there was something wrong with the console messages. I knew it would happen, I only had to see with my own eyes what would happen and how.

Posted Image

The message of Visual Studio said: Index was out of range.

Well, of course it was. I had reset the pdu array. This message made 100% that the array structured thing did get reset.
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6383
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 06:59 AM

Referring to your earlier posts, I still think it worth pointing out (as it seems to have been skipped) that there is a difference between IPAddress and IpAddress. C# is case sensitive and these are two different types. Compare SnmpSharpNet and System.Net.IPAddress namespaces.
Was This Post Helpful? 0
  • +
  • -

#18 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,902
  • Joined: 12-June 08

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 07:06 AM

Quote

For example, let's say that we need an integer variable named integer1. The syntax (depending on the language) should look like the following:

int integer1 or
Dim integer1 as int

UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));


target is of type UDPtarget and the word UDPtarget has to be written twice. I suppose there is a reason in this, a reason I still do not know.


Yes there is a reason. The second is the constructor method/function of the object.

<datatype> <var name> = new <constructor name>(<any or no params>);


<datatype> <var name> = new <constructor name>(<any or no params>);
Int32 foo = new Int32();


You can do the same in VB.NET as well, but the language helps you cut this corner.
Example:
Dim foo As Int32 = New Int32()



https://docs.microso...ts/constructors
Was This Post Helpful? 0
  • +
  • -

#19 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 09:08 AM

Now it's time to start building my application. The objective of the SNMP thing is to control a professional satellite receiver.


This is my first try.

            //Declare variables
            Int32 int32Var=10600;
            Char CommandSelection;
            string ConsoleInput;


            //Initialise SNMP
            UdpTarget MRD4400IP = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));
            Pdu pduMRD4400 = new Pdu(PduType.Set);
            SnmpV2Packet response;
            AgentParameters MRD4400param = new AgentParameters(SnmpVersion.Ver2, new OctetString("private"));


            //Here starts the application
            StartOfApp:
            Console.WriteLine("Choose CommandSelection.");
            Console.WriteLine("Press L for LNB frequency");
            Console.WriteLine("Press S for Satellite frequency");
            Console.WriteLine("Press Q to exit");
            Console.WriteLine("");
            CommandSelection = Console.ReadKey().KeyChar;
            Console.WriteLine("");
            Console.WriteLine("You pressed the {0} key.", CommandSelection);
            Console.ReadLine();

            switch (CommandSelection)
            { 
                case 'L':
                case 'l':
                    goto SetLNBfreq;
                    break;
                case 'Q':
                case 'q':
                    goto Telos;
                    break;
                default:
                    goto StartOfApp;
                    break;
            }


        SetLNBfreq:
        Console.WriteLine("Give LNB frequency in MHz (just numbers, no comma or dot).");
        ConsoleInput = Console.ReadLine();
        int32Var = Convert.ToInt32(ConsoleInput);
        Console.WriteLine("The LNB frequency is: ,{0} MHz", int32Var);
        pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(int32Var*1000));
        response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
        goto StartOfApp;
            Telos:
            Console.WriteLine("Bye bye.");
            Console.ReadLine();




A screenshot from the above code.
Posted Image


I have much work to do.
Was This Post Helpful? 0
  • +
  • -

#20 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 09:14 AM

The first bug has just appeared.

Take a look at the arrows. The second time I had to give the number twice. Otherwise an error would terminate my application.

Posted Image

I am not complaining, a calm sea never made a good sailor.

:chinese: :chinese: :chinese:
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 09:48 AM

Looks like user error to me... You seem to have forgotten about your extra ReadLine() on line 24. It's interesting that your screen shot, above the yellow arrow, you remembered to press the enter key in response to the "You pressed the l key" prompt, but on the first red arrow, you entered a frequency instead of pressing the enter key like you did previously.
Was This Post Helpful? 0
  • +
  • -

#22 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 09:49 AM

Well, it was not actually a bug. Actually, it was the Console.Readline() at line 24 (in the last code I uploaded). I rushed to enter the number. It was so simple! :bananaman:/> :bananaman:/>

The solution to that was to change from Console.Readline() to Console.ReadKey(). Even if I start giving the number prematurely the Console accepts only one digit and then proceeds to the next command.

You can see that in the following screenshot (yellow arrows).

Posted Image

However, i did not like the ones that appeared. So I changed the Console.ReadKey() to Console.ReadKey(true).

Now it looks better.
Posted Image


PS: andrewsw, modi123_1 thanks for your answers.
Was This Post Helpful? 0
  • +
  • -

#23 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 09:58 AM

I know that you are claiming to be a newbie, but I highly recommend learning to use the language the right way. The use of goto's is really a poor programming practice because it leads to spaghetti code if you get in the habit of using it all the time.
Was This Post Helpful? 0
  • +
  • -

#24 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 10:09 AM

View PostSkydiver, on 23 October 2017 - 07:48 PM, said:

Looks like user error to me... You seem to have forgotten about your extra ReadLine() on line 24. It's interesting that your screen shot, above the yellow arrow, you remembered to press the enter key in response to the "You pressed the l key" prompt, but on the first red arrow, you entered a frequency instead of pressing the enter key like you did previously.
It seems that we uploaded our posts simultaneously. :bananaman: :bananaman:



View PostSkydiver, on 23 October 2017 - 07:58 PM, said:

The use of goto's is really a poor programming practice because it leads to spaghetti code if you get in the habit of using it all the time.
Interesting. What's the alternative?
Was This Post Helpful? 0
  • +
  • -

#25 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 10:10 AM

Loops. For your use case, a do-while loop maybe a better fit than a while loop.
Was This Post Helpful? 0
  • +
  • -

#26 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 10:49 AM

View PostSkydiver, on 23 October 2017 - 08:10 PM, said:

Loops. For your use case, a do-while loop maybe a better fit than a while loop.
I will do it.

Meanwhile, I did something else. I focused on the error handling of the console input. My code asks for an integer number and I had to make sure that the Console input contains only digits.

        static void Main(string[] args)
        {
            Console.Title = "MRD4400 remote control via SNMP";

            //Declare variables
            Int32 int32Var = 10600;
            Char CommandSelection;
            string ConsoleInput;
            bool InputIsInt32;


            //Initialise SNMP
            UdpTarget MRD4400IP = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));
            Pdu pduMRD4400 = new Pdu(PduType.Set);
            SnmpV2Packet response;
            AgentParameters MRD4400param = new AgentParameters(SnmpVersion.Ver2, new OctetString("private"));


            //Here starts the application
        StartOfApp:
            Console.WriteLine("Choose CommandSelection.");
            Console.WriteLine("Press L for LNB frequency");
            Console.WriteLine("Press S for Satellite frequency");
            Console.WriteLine("Press Q to exit");
            Console.WriteLine("");
            CommandSelection = Console.ReadKey().KeyChar;
            Console.WriteLine("");
            Console.WriteLine("You pressed the {0} key.", CommandSelection);
            Console.ReadKey(true);

            switch (CommandSelection)
            {
                case 'L':
                case 'l':
                    goto SetLNBfreq;
                    break;
                case 'Q':
                case 'q':
                    goto Telos;
                    break;
                default:
                    goto StartOfApp;
                    break;
            }


        SetLNBfreq:
            do
            {
                Console.WriteLine("Give LNB frequency in MHz (just numbers, no comma or dot).");
                ConsoleInput = Console.ReadLine();
                InputIsInt32 = int.TryParse(ConsoleInput, out int32Var);
            } while (InputIsInt32==false);
            
            int32Var = Convert.ToInt32(ConsoleInput);
            Console.WriteLine("The LNB frequency is: ,{0} MHz", int32Var);
            pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(int32Var * 1000));
            response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
            goto StartOfApp;
        Telos:
            Console.WriteLine("Bye bye.");
            Console.ReadLine();

        }



The new stuff starts from line 48.
Was This Post Helpful? 0
  • +
  • -

#27 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6383
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 12:09 PM

Is there a question?

Which tutorial is encouraging the use of goto? (Most decent tutorials will tuck it to the side or an appendix.)

Ugh, please don't name variables stuff like int32Var?!!
Was This Post Helpful? 0
  • +
  • -

#28 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 597
  • View blog
  • Posts: 2,494
  • Joined: 13-June 14

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 12:20 PM

View PostArchangelos, on 23 October 2017 - 01:47 PM, said:

UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));

target is of type UDPtarget and the word UDPtarget has to be written twice. I suppose there is a reason in this, a reason I still do not know.


I haven't read the rest of the posts in this thread, but I can talk about this (note that I'm not a C# programmer, but these things are common to many languages).

C# is a statically typed language, meaning that you have to tell the compiler what kind of thing you're going to store in it (and you can't change the kind of thing later). That accounts for the one on the left. On the right hand side, you're creating an instance of the class and to do so, you use a constructor. passing any arguments that the constructor takes. Constructors for a class use the class' name, so that accounts for the one on the right.

I suppose this probably doesn't need too much explanation, if you read the words and think about what's going on: the left hand side is saying "make some space to store a UDPTarget" and the right hand side says "create a new UDPTarget" and of course we're assigning that new object to the variable on the left so we can use it later.
Was This Post Helpful? 0
  • +
  • -

#29 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6383
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 12:29 PM

Consider if you split the statement into two:

UdpTarget target;

You are declaring a variable of type UdpTarget. It can, but does not currently, refer to such an object.

target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));

You are assigning the variable to a new instance of UdpTarget.
Was This Post Helpful? 0
  • +
  • -

#30 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 12:53 PM

And as I've stated before, the cast is pointless because all it is doing is casting to the same type.
target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));


should be:
target = new UdpTarget(new IpAddress("192.168.192.21"));


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3