使用C#的System.IO.Bacnet库写入BACnet设备BinaryOutput对象报错求助
解决System.IO.Bacnet写入OBJECT_BINARY_OUTPUT时的INVALID_TAG错误
问题根源
触发INVALID_TAG错误的核心原因是写入的数据类型与BACnet设备要求不匹配:OBJECT_BINARY_OUTPUT的PROP_PRESENT_VALUE属性要求的数据类型是BacnetBinaryPV枚举(对应Active/Inactive状态),而你传入的是uint类型数值,设备无法识别该数据标签,因此拒绝写入请求。
修复代码
将写入值从uint替换为BacnetBinaryPV枚举的对应成员,修改后的关键代码如下:
// 选择要切换的目标状态:ACTIVE对应启动,INACTIVE对应关闭 BacnetBinaryPV targetValue = BacnetBinaryPV.BINARY_ACTIVE; IList<BacnetValue> valtowrite = new BacnetValue[1] { new BacnetValue(targetValue) };
完整修复后的代码示例:
bacnetClient.Start(); BacnetAddress deviceAddress = new BacnetAddress(BacnetAddressTypes.IP, "10.20.30.250:47808"); BacnetObjectId objectId = new BacnetObjectId(BacnetObjectTypes.OBJECT_BINARY_OUTPUT, 2); IList<BacnetValue> values; bool success = bacnetClient.ReadPropertyRequest(deviceAddress, objectId, BacnetPropertyIds.PROP_PRESENT_VALUE, out values); if (success) { string presentValue = values[0].Value.ToString(); Console.WriteLine($"Present Value: {presentValue}"); } else { Console.WriteLine("Error occurred while reading from the device."); } // 替换原uint值为BacnetBinaryPV枚举 BacnetBinaryPV targetValue = BacnetBinaryPV.BINARY_ACTIVE; IList<BacnetValue> valtowrite = new BacnetValue[1] { new BacnetValue(targetValue) }; bool successWrite = bacnetClient.WritePropertyRequest(deviceAddress, objectId, BacnetPropertyIds.PROP_PRESENT_VALUE, valtowrite ); if (successWrite) { Console.WriteLine($"\nSuccessfully updated the status for OBJECT_BINARY_OUTPUT 2"); bool successRead = bacnetClient.ReadPropertyRequest(deviceAddress, objectId, BacnetPropertyIds.PROP_PRESENT_VALUE, out values); if (successRead) { Console.WriteLine($"\nUpdated Present Value: {values[0].Value.ToString()}"); } else { Console.WriteLine("\n!! Error occurred when reading from BACnet device. !!"); } } else { Console.WriteLine("\n !! Error occurred while writing to the bacnet device. !!"); }
额外注意事项
- 如果修改后仍然写入失败,需要确认该OBJECT_BINARY_OUTPUT实例的
PROP_PRESENT_VALUE属性是否允许写入(部分设备可能将该属性设为只读,或需要特定权限)。 - 读取时返回的字符串(如"Active")本质是
BacnetBinaryPV枚举的ToString()结果,写入时必须使用枚举类型才能匹配设备的标签要求。
内容的提问来源于stack exchange,提问作者Hansi Thennakoon
相关产品推荐
相关产品推荐

