sharpbrick / powered-up

.NET implementation of the LEGO PoweredUp Protocol
MIT License
94 stars 19 forks source link

System.ObjectDisposedException: The object has been closed. after a few seconds #189

Open thetestgame opened 1 year ago

thetestgame commented 1 year ago

After a few seconds and one command being sent to a Lego hub it abruptly closes the connection. Is there a heartbeat step that needs to occur? This is currently guaranteed to happen every time I try with my current codebase.

System.ObjectDisposedException: The object has been closed. (0x80000013)
   at SharpBrick.PoweredUp.WinRT.WinRTPoweredUpBluetoothCharacteristic.WriteValueAsync(Byte[] data)
   at SharpBrick.PoweredUp.Bluetooth.BluetoothKernel.SendBytesAsync(Byte[] data)
   at SharpBrick.PoweredUp.Protocol.LegoWirelessProtocol.SendMessageAsync(LegoWirelessMessage message)
   at SharpBrick.PoweredUp.Protocol.ILegoWirelessProtocolExtensions.SendMessageReceiveResultAsync[TResultMessage](ILegoWirelessProtocol self, LegoWirelessMessage message, Func`2 filter)
   at SharpBrick.PoweredUp.Protocol.ILegoWirelessProtocolExtensions.SendPortOutputCommandAsync(ILegoWirelessProtocol self, PortOutputCommandMessage message)
   at SharpBrick.PoweredUp.BasicMotor.StartPowerAsync(SByte power)
tthiery commented 1 year ago

In the protocol there is no concept of heartbeat. The hubs have a very strange initial connection mode but aside of that it is a very simple protocol on the frame layer.

tthiery commented 1 year ago

This is a bit strange considering that StartPower is one of the most basic commands.

tthiery commented 1 year ago

I guess this is the hub shutting down. Question is why:

thetestgame commented 1 year ago

This is repeatable after sending only one command after the initial connect call. Also the Hubs I've tested with work fine otherwise with the controller or the Lego app. Far as I know I have a standard BT setup. Its stock with my DELL machine if its worth mentioning.

tthiery commented 1 year ago

Have you had the chance to test it on another device? I have no trains, it worked last time I used it (tm) with a technic hub and motor and right now I am in an actual train so hard time supporting you 😂

tthiery commented 1 year ago

Creating a trace file would be helpful. I could verify the correct encoding there and check the exact place the thing fails

thetestgame commented 1 year ago

Have you had the chance to test it on another device? I have no trains, it worked last time I used it (tm) with a technic hub and motor and right now I am in an actual train so hard time supporting you 😂

I have not. I'll try that later today.

Creating a trace file would be helpful. I could verify the correct encoding there and check the exact place the thing fails

Do you have a preferred tool for trace files?

thetestgame commented 1 year ago

Additional bit of information. It seems to disconnect/stop on its own regardless if I send any new commands. Also sometimes it keeps working flawlessly but its rare. When the abrupt stop happens the light on the hub is still on like its connected but the object has been disposed from .NET.

tthiery commented 1 year ago

Which Hub ist it exactly? The train one with the motors or the default Lego two port hub?

thetestgame commented 1 year ago

I'm using the Lego two port hubs from the RC trains.

tthiery commented 1 year ago

I do not have any and I do not remember special behavior for them. They are a late product like the technic hubs.