[BUG?] Attached not set until AFTER Attach event

Supporting Visual Studio on Windows
Post Reply
scsm
Phidgetly
Posts: 22
Joined: Sun Nov 20, 2022 10:28 pm
Contact:

[BUG?] Attached not set until AFTER Attach event

Post by scsm »

I'm trying to determine when all expected devices on ports are attached.

For DigitalInputs, it appears that digitalInput.Attached is not set until sometime AFTER the Attach event fires. Is this a bug?

If this is by design, does that mean need to keep track of all Attach and Detach events ourselves?
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: [BUG?] Attached not set until AFTER Attach event

Post by Patrick »

The attach event is fired during the 'attaching' phase of opening a device - the device isn't considered fully attached until after the attach event returns.

The attach event context is meant to device initialization. For example, you generally won't be able to read back device state from the attach event, but you are able to set device state.

In terms of the internal attach logic for opening a channel, this system does this:

1. Finds a matching channel based on your set matching parameters
2. Opens a connection to that device, over USB, Network, etc.
3. Sends a reset packet to reset the state of the channel
4. Sets any defaults values, as specified in the channel API
5. Fires the attach event, if you registered one - you can set your own initial values/state here
6. Sends an enable packet - now the channel is fully enabled. If it's a sensor it will start streaming back data, if it's a controller it may start driving a motor, etc.
7. The Attached property become true
8. If you called open() with a timeout, it will now wait for any initial state to come back from the device (sensor values, etc.), up to the timeout value, and then open() returns.

-Patrick
scsm
Phidgetly
Posts: 22
Joined: Sun Nov 20, 2022 10:28 pm
Contact:

Re: [BUG?] Attached not set until AFTER Attach event

Post by scsm »

Patrick wrote: Mon Dec 12, 2022 11:51 am The attach event is fired during the 'attaching' phase of opening a device - the device isn't considered fully attached until after the attach event returns.

The attach event context is meant to device initialization. For example, you generally won't be able to read back device state from the attach event, but you are able to set device state.

In terms of the internal attach logic for opening a channel, this system does this:

1. Finds a matching channel based on your set matching parameters
2. Opens a connection to that device, over USB, Network, etc.
3. Sends a reset packet to reset the state of the channel
4. Sets any defaults values, as specified in the channel API
5. Fires the attach event, if you registered one - you can set your own initial values/state here
6. Sends an enable packet - now the channel is fully enabled. If it's a sensor it will start streaming back data, if it's a controller it may start driving a motor, etc.
7. The Attached property become true
8. If you called open() with a timeout, it will now wait for any initial state to come back from the device (sensor values, etc.), up to the timeout value, and then open() returns.

-Patrick
Thanks again Patrick. We moved the Attached status check until after the Open port with timeout returns and it now updates in the UI correctly.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests