1

I am trying to connect to a BACnet foreign device using the BAC0 Python library. Below is my script:

import BAC0

print(BAC0.version)

bbmdIP = '10.x.y.z:47808'  # Replace with actual IP
bbmdTTL = 900
bacnet = BAC0.lite(bbmdAddress=bbmdIP, bbmdTTL=bbmdTTL)  # Connect

print(bacnet.vendorName.strValue)
print(bacnet.modelName.strValue)

whois_results = bacnet.whois()
print("WhoIs results:", whois_results)

print(bacnet.devices)

bacnet.discover(networks='known')

However, I keep getting the following error:

2025-01-09 14:35:05,917 - INFO    | Starting Asynchronous BAC0 version 2024.09.10 (Lite)
2025-01-09 14:35:05,920 - INFO    | Using bacpypes3 version 0.0.98
2025-01-09 14:35:05,921 - INFO    | Use BAC0.log_level to adjust verbosity of the app.
2025-01-09 14:35:05,921 - INFO    | Ex. BAC0.log_level('silence') or BAC0.log_level('error')
Traceback (most recent call last):
  File "C:\Users\dmaske\Desktop\New folder\test_bacnet.py", line 7, in <module>
    bacnet = BAC0.connect(bbmdAddress=bbmdIP, bbmdTTL=bbmdTTL)  # Connect
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dmaske\AppData\Roaming\Python\Python311\site-packages\BAC0\scripts\Lite.py", line 141, in __init__
    self._ping_task.start()
  File "C:\Users\dmaske\AppData\Roaming\Python\Python311\site-packages\BAC0\tasks\TaskManager.py", line 143, in start
    self.aio_task = asyncio.create_task(self.execute(), name=f"aio{self.name}")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python311\Lib\asyncio\tasks.py", line 371, in create_task
    loop = events.get_running_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'Task.execute' was never awaited

What I have tried:

  • I tried running the script with different variations of BAC0.lite() and BAC0.connect(), but the error persists

  • I verified that my BACnet devices are accessible using Yabe software, where I can register and discover all devices successfully

  • I experimented with adding an event loop manually, but it didn't work

Why is asyncio.create_task() failing with RuntimeError: no running event loop, and how can I properly connect to a foreign BACnet device using BAC0?

1 Answer 1

0

You need to start python with asyncio

python -m asyncio to get access to a event loop if you want to play around interactively.

For a script... a simple example would be something like

import BAC0
import socket
import asyncio

async def start_bacnet_server():
    async with BAC0.start(ip="10.138.103.17/16", deviceId=1112) as device1:
        # Start BACnet on a specific IP and port
        
        print("BACnet started. Discovering devices...")

        # Discover devices (this may take some time)
        await device1._discover()
        
        # Get the discovered devices
        devices = device1.discoveredDevices
        print(f"Discovered devices: {devices}")

        print("Pretty")
        await device1.devices

if __name__ == "__main__":
    asyncio.run(start_bacnet_server())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.