Search code examples
c#list.net-corebacnet

Getting a NullReference Exception when trying to read Analog Input from a BACNET Device using BACNET MSTP with C# - any suggestions?


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.sstatic.net/oF9Vn.png)](https://i.sstatic.net/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;
        }

Solution

  • 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.