Help Needed: ‘Coroutine’ Object Not Iterable in Async BACnet Device Reading /u/werner__heisenberger Python Education

Hello everyone,

I’m working on an asynchronous BACnet device scanner using the BAC0 library in Python. My goal is to retrieve all available objects and their present values from each discovered device. However, I’m running into the following error:

values = await read_device_values(bacnet, device_id, device_addr) RuntimeWarning: Enable tracemalloc to get the object allocation traceback ERROR:root:Error reading device 100: 'coroutine' object is not iterable ERROR:root:Error reading device 150: 'coroutine' object is not iterable ERROR:root:Error reading device 2537082: 'coroutine' object is not iterable ERROR:root:Error reading device 3746480: 'coroutine' object is not iterable

Code Snippet:

import asyncio

import BAC0

import logging

logging.basicConfig(level=logging.DEBUG)

async def read_device_values(bacnet, device_id, device_addr):

"""Read all available values from a BACnet device."""

try:

object_list = bacnet.read(f'{device_addr} device {device_id} objectList')

values = {}

for obj in object_list:

obj_type, obj_instance = obj

try:

value = bacnet.read(f'{device_addr} {obj_type} {obj_instance} presentValue')

try:

name = bacnet.read(f'{device_addr} {obj_type} {obj_instance} objectName')

except:

name = f"{obj_type}_{obj_instance}"

values[name] = {

'type': obj_type,

'instance': obj_instance,

'value': value

}

except Exception as e:

logging.debug(f"Error reading {obj_type} {obj_instance}: {str(e)}")

continue

return values

except Exception as e:

logging.error(f"Error reading device {device_id}: {str(e)}")

return None

async def main():

print(BAC0.version)

bbmdIP = '10.50.175.16:47808'

bbmdTTL = 900

bacnet = BAC0.connect(bbmdAddress=bbmdIP, bbmdTTL=bbmdTTL)

try:

await asyncio.sleep(2)

whois_results = list(await bacnet.who_is()) # <-- Possible Issue Here

device_values = {}

for device in whois_results:

try:

device_id = device.iAmDeviceIdentifier[1]

device_addr = device.pduSource

print(f"Reading values from Device ID: {device_id}, Address: {device_addr}")

values = await read_device_values(bacnet, device_id, device_addr)

if values:

device_values[device_id] = values

except AttributeError:

continue

print("nDevice Values Summary:")

for device_id, values in device_values.items():

print(f"nDevice {device_id}:")

for obj_name, details in values.items():

print(f" {obj_name}: {details['value']}")

except Exception as e:

print(f"Error occurred: {str(e)}")

finally:

if bacnet:

bacnet.disconnect()

if __name__ == "__main__":

asyncio.run(main())

Has anyone encountered similar issues with BAC0 when handling async operations?

Any help or insights would be greatly appreciated! 🚀

submitted by /u/werner__heisenberger
[link] [comments]

​r/learnpython Hello everyone, I’m working on an asynchronous BACnet device scanner using the BAC0 library in Python. My goal is to retrieve all available objects and their present values from each discovered device. However, I’m running into the following error: values = await read_device_values(bacnet, device_id, device_addr) RuntimeWarning: Enable tracemalloc to get the object allocation traceback ERROR:root:Error reading device 100: ‘coroutine’ object is not iterable ERROR:root:Error reading device 150: ‘coroutine’ object is not iterable ERROR:root:Error reading device 2537082: ‘coroutine’ object is not iterable ERROR:root:Error reading device 3746480: ‘coroutine’ object is not iterable Code Snippet: import asyncio import BAC0 import logging logging.basicConfig(level=logging.DEBUG) async def read_device_values(bacnet, device_id, device_addr): “””Read all available values from a BACnet device.””” try: object_list = bacnet.read(f'{device_addr} device {device_id} objectList’) values = {} for obj in object_list: obj_type, obj_instance = obj try: value = bacnet.read(f'{device_addr} {obj_type} {obj_instance} presentValue’) try: name = bacnet.read(f'{device_addr} {obj_type} {obj_instance} objectName’) except: name = f”{obj_type}_{obj_instance}” values[name] = { ‘type’: obj_type, ‘instance’: obj_instance, ‘value’: value } except Exception as e: logging.debug(f”Error reading {obj_type} {obj_instance}: {str(e)}”) continue return values except Exception as e: logging.error(f”Error reading device {device_id}: {str(e)}”) return None async def main(): print(BAC0.version) bbmdIP = ‘10.50.175.16:47808’ bbmdTTL = 900 bacnet = BAC0.connect(bbmdAddress=bbmdIP, bbmdTTL=bbmdTTL) try: await asyncio.sleep(2) whois_results = list(await bacnet.who_is()) # <– Possible Issue Here device_values = {} for device in whois_results: try: device_id = device.iAmDeviceIdentifier[1] device_addr = device.pduSource print(f”Reading values from Device ID: {device_id}, Address: {device_addr}”) values = await read_device_values(bacnet, device_id, device_addr) if values: device_values[device_id] = values except AttributeError: continue print(“nDevice Values Summary:”) for device_id, values in device_values.items(): print(f”nDevice {device_id}:”) for obj_name, details in values.items(): print(f” {obj_name}: {details[‘value’]}”) except Exception as e: print(f”Error occurred: {str(e)}”) finally: if bacnet: bacnet.disconnect() if __name__ == “__main__”: asyncio.run(main()) Has anyone encountered similar issues with BAC0 when handling async operations? Any help or insights would be greatly appreciated! 🚀 submitted by /u/werner__heisenberger [link] [comments] 

Hello everyone,

I’m working on an asynchronous BACnet device scanner using the BAC0 library in Python. My goal is to retrieve all available objects and their present values from each discovered device. However, I’m running into the following error:

values = await read_device_values(bacnet, device_id, device_addr) RuntimeWarning: Enable tracemalloc to get the object allocation traceback ERROR:root:Error reading device 100: 'coroutine' object is not iterable ERROR:root:Error reading device 150: 'coroutine' object is not iterable ERROR:root:Error reading device 2537082: 'coroutine' object is not iterable ERROR:root:Error reading device 3746480: 'coroutine' object is not iterable

Code Snippet:

import asyncio

import BAC0

import logging

logging.basicConfig(level=logging.DEBUG)

async def read_device_values(bacnet, device_id, device_addr):

"""Read all available values from a BACnet device."""

try:

object_list = bacnet.read(f'{device_addr} device {device_id} objectList')

values = {}

for obj in object_list:

obj_type, obj_instance = obj

try:

value = bacnet.read(f'{device_addr} {obj_type} {obj_instance} presentValue')

try:

name = bacnet.read(f'{device_addr} {obj_type} {obj_instance} objectName')

except:

name = f"{obj_type}_{obj_instance}"

values[name] = {

'type': obj_type,

'instance': obj_instance,

'value': value

}

except Exception as e:

logging.debug(f"Error reading {obj_type} {obj_instance}: {str(e)}")

continue

return values

except Exception as e:

logging.error(f"Error reading device {device_id}: {str(e)}")

return None

async def main():

print(BAC0.version)

bbmdIP = '10.50.175.16:47808'

bbmdTTL = 900

bacnet = BAC0.connect(bbmdAddress=bbmdIP, bbmdTTL=bbmdTTL)

try:

await asyncio.sleep(2)

whois_results = list(await bacnet.who_is()) # <-- Possible Issue Here

device_values = {}

for device in whois_results:

try:

device_id = device.iAmDeviceIdentifier[1]

device_addr = device.pduSource

print(f"Reading values from Device ID: {device_id}, Address: {device_addr}")

values = await read_device_values(bacnet, device_id, device_addr)

if values:

device_values[device_id] = values

except AttributeError:

continue

print("nDevice Values Summary:")

for device_id, values in device_values.items():

print(f"nDevice {device_id}:")

for obj_name, details in values.items():

print(f" {obj_name}: {details['value']}")

except Exception as e:

print(f"Error occurred: {str(e)}")

finally:

if bacnet:

bacnet.disconnect()

if __name__ == "__main__":

asyncio.run(main())

Has anyone encountered similar issues with BAC0 when handling async operations?

Any help or insights would be greatly appreciated! 🚀

submitted by /u/werner__heisenberger
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *