bacnet._discover error messages from bacpypes3
Hello, I'm looking for finding the physical address (like network:MAC or IP address) to be passed to bacnet.read and write commands.
On the legacy BAC0 I was using bacnet.whois call with success (reduced to just the one device I need), but on the current BAC0 it seems there is no way to release just a Who-Is message, but I have to launch _discover. (Or -- is there any simple way?)
So, I'm trying code like this:
import BAC0
import asyncio
async def main():
# Create a BACnet object
bacnet = BAC0.start(ip='10.10.82.221/24', ping=False)
await bacnet._discover(limits=(21100, 21100), global_broadcast=True)
devs = bacnet.discoveredDevices
print(f"Discovered devices: {devs}")
bacnet.disconnect
asyncio.run(main())
It WORKS -- it releases Who-Is and puts just device 21100 (Loytec L-DALI gateway in this case) to the discoveredDevices object. However, it LIKELY everytime (but not at all) outputs these errors:
2025-09-30 22:36:30,937 - INFO | Starting Asynchronous BAC0 version 2025.09.15 (Lite)
2025-09-30 22:36:30,938 - INFO | Using bacpypes3 version 0.0.102
2025-09-30 22:36:30,938 - INFO | Use BAC0.log_level to adjust verbosity of the app.
2025-09-30 22:36:30,938 - INFO | Ex. BAC0.log_level('silence') or BAC0.log_level('error')
2025-09-30 22:36:31,086 - INFO | Using ip : 10.10.82.221/24 on port 47808 | broadcast : 10.10.82.255
2025-09-30 22:36:37,094 - INFO | Using default JSON configuration file
2025-09-30 22:36:37,145 - INFO | Registered as BACnet/IP App | mode normal
2025-09-30 22:36:37,152 - INFO | Device instance (id) : 3056934
2025-09-30 22:36:37,153 - INFO | Installing recurring task Cleanup Tasks List (id:2498468121168)
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<IPv4DatagramServer.confirmation() done, defined at c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\ipv4\__init__.py:253> exception=InvalidStateError('invalid state')>
Traceback (most recent call last):
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\ipv4\__init__.py", line 264, in confirmation
await self.response(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\comm.py", line 98, in response
await self.serverPeer.confirmation(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\ipv4\service.py", line 115, in confirmation
await self.annexJ.response(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\comm.py", line 98, in response
await self.serverPeer.confirmation(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\ipv4\bvll.py", line 252, in confirmation
await self.response(lpdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\comm.py", line 98, in response
await self.serverPeer.confirmation(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\ipv4\service.py", line 347, in confirmation
await self.response(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\comm.py", line 98, in response
await self.serverPeer.confirmation(pdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\netservice.py", line 485, in confirmation
await self.adapterSAP.process_npdu(self, npdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\netservice.py", line 1029, in process_npdu
await self.sap_request(adapter, npdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\comm.py", line 179, in sap_request
await self.serviceElement.indication(*args)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\netservice.py", line 1504, in indication
response = getattr(self, fn)(adapter, npdu)
File "c:\Users\tomas\Documents\BacnEx\.venv\Lib\site-packages\bacpypes3\netservice.py", line 2219, in NetworkNumberIs
future.set_result(npdu.nniNet)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
asyncio.exceptions.InvalidStateError: invalid state
2025-09-30 22:36:39,595 - INFO | Found those networks : {1701, 1702, 40, 1641, 1711, 1712, 1103, 1661}
2025-09-30 22:36:39,596 - INFO | Global braodacast required, attempting a simple whois using provided device instances limits (21100 - 21100)
2025-09-30 22:36:39,596 - WARNING | Issuing a global Broadcast whois can create network flood. Use with care.
2025-09-30 22:36:39,932 - INFO | Discovery done. Found 1 devices on 8 BACnet networks.
Discovered devices: {'device,21100': {'object_instance': (<ObjectType: device>, 21100), 'address': <IPv4Address 10.10.82.111>, 'network_number': {40}, 'vendor_id': 178, 'vendor_name': 'unknown'}}
(in facts, it is used in a more complicated script with dynamic parameter composition, but the issue is the same).
What's wrong with it?
Thank you in advance!
Task exception was never retrieved
This is telling us that something went wrong and the script ended up before getting all the answers.
I highly suggest working inside a asyn with loop
For example :
import asyncio
import BAC0
import logging
async def main():
BAC0.log_level(stdout=logging.DEBUG)
async with BAC0.start() as bacnet:
await bacnet._discover()
await bacnet.devices
while True:
await asyncio.sleep(2)
print(bacnet.discoveredDevices)
if __name__ == '__main__':
asyncio.run(main())
Hello, I've tried your code (just adding my IP address) several times with several networks, trying every time both versions (yours and mine).
Basically, the results of both codes are the same. The error message I described before is exactly the same with both -- but I found the error message appears just ONLY when I'm using BACnet/IP over Layer 2 VPN connection (to say the truth, just on SOME of my VPN connections, although there is the same VPN software (SoftEther).
When used locally (on a real Ethernet port) the error message never appeared.
So, it seems to me it could have something to do with timings (VPN over relatively slow channel). And, it is probably more bacpypes3 issue then BAC0 issue. I'll try some specific dependences using several VPNs, and eventually I'll open an issue on bacpypes project.
However, thank you for your analysis!
Another option would be an async function somewhere is called without await... the trace only shows bacpypes things.... wonder if it's in BAC0 or bacpypes....
Haaaa ! I knew I had something...
Can you try develop branch ? It's something I think I fixed a few weeks ago. A missing await....
Hmm. I'm on BAC0 version 2025.9.15 (the last one on pypi); in this release that await is already there.
I guess this stopAllTasks() is called in the end of the session -- but that error appears just when calling await bacnet._discover(). I've tried to insert await asyncio.sleep(15):
bacnet = BAC0.start(ip='10.10.82.223/24', ping=False)
print ("Waiting...")
await asyncio.sleep(15)
print ("Starting discovery...")
await bacnet._discover(limits=(21100, 21100), global_broadcast=True)
and the error appears just after that 15 seconds, when bacnet._discover is called. Not before, not after. And not after NEXT calls for bacnet._discover (I'm calling it for every single device I need to contact, as the network is very large). But, even though this error appears, the discovery in facts do work fine.
Zut...