0

Communicating with BACNET device using C# BACNET MSTP, but throw Null error exception when reading the analog input value.

Hi Everyone, I'm new to the commuity and I'm not very familiar with the C# in some of structure. Please let me know if you need further information about this problem. I'm writing a script using BAC protocol library for .Net for getting the Analog input from a BACNET Device using BACNET MSTP (Serial Port). The script is based on the simple example of the library from its Github. Here's the error and the location of the error in code. The Message shows that I already have access to the BACnet Device "956", but when reading for an AnalogInput value, such as "0". It give me the NullerrorException. I thought it wias initialization problem, so I try checked each of the value for the input and output, and tried initialized the IList object. But it still throw the same error. Thanks if you could give me some suggestions on how to solve this problem and get the Analog input value.

Here's the Bug: enter image description hereenter image description here[[enter image description here](https://i.stack.imgur.com/oF9Vn.png)](https://i.stack.imgur.com/yKbPH.png)

Piece of the Code Having problem when calling the ReadScalarValue - ReadPropertyRequest:

 static void StartActivity()
        {

            // Create a BACnet client
            BacnetClient client = new BacnetClient(new BacnetMstpProtocolTransport("COM8", 9600, 8));

            

            client.Start();    // go

            // Send WhoIs in order to get back all the Iam responses :  
            client.OnIam += new BacnetClient.IamHandler(handler_OnIam);
            
            client.WhoIs();

            /* Optional Remote Registration as A Foreign Device on a BBMD at @192.168.1.1 on the default 0xBAC0 port
                           
            bacnet_client.RegisterAsForeignDevice("192.168.1.1", 60);
            Thread.Sleep(20);
            bacnet_client.RemoteWhoIs("192.168.1.1");
            */
        }

      static void ReadWriteExample()
        {

            BacnetValue Value = new BacnetValue("test");
            bool ret;
            // Read Present_Value property on the object ANALOG_INPUT:0 provided by the device 12345
            // Scalar value only
            ret = ReadScalarValue(956, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_INPUT, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, out Value);
            Console.WriteLine(Value);
            if (ret == true)
            {
                Console.WriteLine("Read value : " + Value.Value.ToString());

                // Write Present_Value property on the object ANALOG_OUTPUT:0 provided by the device 4000
                //BacnetValue newValue = new BacnetValue(Convert.ToSingle(Value.Value));   // expect it's a float
                //ret = WriteScalarValue(4000, new BacnetObjectId(BacnetObjectTypes.OBJECT_ANALOG_OUTPUT, 0), BacnetPropertyIds.PROP_PRESENT_VALUE, newValue);

                //Console.WriteLine("Write feedback : " + ret.ToString());
            }
            else
                Console.WriteLine("Error somewhere !");
        }


       public static bool ReadScalarValue(int device_id, BacnetObjectId BacnetObjet, BacnetPropertyIds Propriete, out BacnetValue Value)
        {
            
            BacnetAddress adr;

            Value = new BacnetValue("testing");
            IList<BacnetValue> NoScalarValue = new List<BacnetValue>();
            int Count = 0;
            // Looking for the device
            adr = DeviceAddr((uint)device_id);
            if (adr == null) return false;  // not found
            Console.WriteLine(adr);
            if (BacnetObjet != null)Console.WriteLine(BacnetObjet);
            if (NoScalarValue != null) { Console.WriteLine(NoScalarValue); }
            if (Propriete != null) { Console.WriteLine(Propriete); }

            tryin:

            try
            {
                if (client.ReadPropertyRequest(adr, BacnetObjet, Propriete, out NoScalarValue) == false)
                {
                    return false;
                }

            }
            catch (Exception err)
            {
                Count++;
                if (Count < 10) { Thread.Sleep(2000); goto tryin; } else { return false; }
            }
                //return false;

                Value = NoScalarValue[0];
            return true;
        }
Leo Ye
  • 9
  • 3
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ian Kemp May 21 '23 at 19:38
  • I tried initialize the IList, but it is not working. – Leo Ye May 24 '23 at 15:22

2 Answers2

0

Are you sure your device has an AI:0 object?

Can you see it if you scan your device using 'YABE' (- I'm guessing that you're probably using the BACnet library that was forked from the API that is used within 'YABE')?

I think your best bet is to look at the full exception call-stack, if not also consider using the VS Debug 'Exception Settings' Window, to stop upon all "Common Language Runtime Exceptions" (- i.e. even if the exception in question is handled).

(If you can advertise a download link for the/a test VS solution, I might be able to take a quick look - with as few changes as possible.)

DennisVM-D2i
  • 416
  • 3
  • 8
  • Hi, I used software BatnetScan and it shows that the device has AI:0 object, and it was mapped to its serial number. Yes, I used the library that is used with in YABE. – Leo Ye May 22 '23 at 01:22
  • @LeoYe I'd probably have to see the source code. – DennisVM-D2i May 24 '23 at 09:18
  • Thanks, I have updated my code in the question. – Leo Ye May 24 '23 at 15:21
  • Just FYI, I tried Yabe, and i found when it tried to read the property input value, it logs a few Exception at the beginning, but after serveral trial, it can read the analog input value. So I tried request for multiple times, but it was not working. – Leo Ye May 24 '23 at 15:24
  • @LeoYe I'm not sure if I do understand; but if YABE is not working, then the problem might not be with your software, or at least not only with your software. – DennisVM-D2i May 24 '23 at 16:20
  • @LeoYe Where is your 'client' - has it been constructed/created (?). – DennisVM-D2i May 24 '23 at 18:01
  • Sry, I think I may confuse you. The YABE is working, the "thing" not working is that I tried send repeating request for response in my software. And Yes my client is created, and the log shows it can be reached in screenshot. Updated the client code. – Leo Ye May 25 '23 at 05:05
0

Problem Solved.

It seems that I created the client object in public, but was creating and initializing another new client object in the StartActivity() function.

So the client was called in reading function was an empty (non initialized) client object, which caused the NullReferenceException.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Leo Ye
  • 9
  • 3