英文:
Getting a NullReference Exception when trying to read Analog Input from a BACNET Device using BACNET MSTP with C# - any suggestions?
问题
使用C# BACNET MSTP与BACNET设备通信,但在读取模拟输入值时抛出Null错误异常。
大家好,我是社区的新成员,对C#的某些结构不太熟悉。请告诉我是否需要关于这个问题的更多信息。我正在使用.Net的BAC协议库编写脚本,以通过BACNET MSTP(串口)从BACNET设备获取模拟输入。该脚本基于库的GitHub上的简单示例。这里是代码中错误的位置和错误。消息显示我已经访问了BACnet设备“956”,但在读取模拟输入值(如“0”)时,它给了我NullerrorException。我以为是初始化问题,所以我尝试检查每个输入和输出的值,并尝试初始化IList对象。但它仍然抛出相同的错误。如果您能提供一些建议来解决这个问题并获取模拟输入值,我将不胜感激。
Bug如下:
[enter image description here](https://i.stack.imgur.com/c6Mja.png)[enter image description here](https://i.stack.imgur.com/VFvj1.png)[[[enter image description here](https://i.stack.imgur.com/Ch4tZ.png)](https://i.stack.imgur.com/oF9Vn.png)](https://i.stack.imgur.com/yKbPH.png)
代码片段出现问题,调用ReadScalarValue - ReadPropertyRequest时:
```csharp
static void StartActivity()
{
// 创建BACnet客户端
BacnetClient client = new BacnetClient(new BacnetMstpProtocolTransport("COM8", 9600, 8));
client.Start(); // 启动
// 发送WhoIs以获取所有Iam响应:
client.OnIam += new BacnetClient.IamHandler(handler_OnIam);
client.WhoIs();
}
static void ReadWriteExample()
{
BacnetValue Value = new BacnetValue("test");
bool ret;
// 读取由设备12345提供的对象ANALOG_INPUT:0上的Present_Value属性
// 仅标量值
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());
// 在由设备4000提供的对象ANALOG_OUTPUT:0上写入Present_Value属性
//BacnetValue newValue = new BacnetValue(Convert.ToSingle(Value.Value)); // 期望是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;
// 寻找设备
adr = DeviceAddr((uint)device_id);
if (adr == null) return false; // 未找到
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; }
}
Value = NoScalarValue[0];
return true;
}
英文:
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;
}
答案1
得分: 0
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).
如果您可以提供一个测试的VS解决方案的下载链接,我可能可以尽量少做修改地快速查看一下。
英文:
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.)
答案2
得分: 0
问题已解决。
似乎我在公共部分创建了客户端对象,但在StartActivity()
函数中又创建和初始化了另一个新的客户端对象。
因此,在读取函数中调用的客户端是一个空的(未初始化的)客户端对象,导致了NullReferenceException
。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论