sharpbrick / powered-up

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

poweredup device list hangs #193

Closed mennolodder closed 4 months ago

mennolodder commented 6 months ago

As mentioned in the other thread, I think this is a separate problem, so will make it's own ticket.

The commandline tool hangs on receiving messages, for instance when running poweredup device list. It does work when I detach all my motors.

My configuration is mentioned here

Tried with the latest release, as well as the latest main.

When I enable debugging I get the following output (a few of the log messages are mine):

Scan Started. Please select the Hub (using a number keys or 'q' to terminate):
1: TechnicMediumHub (with address 90:84:2B:63:D2:B5)
1
Selected TechnicMediumHub with key 1
Discover Ports. Receiving Messages ...
info: SharpBrick.PoweredUp.Protocol.LegoWirelessProtocol[0]
      Connected to device, getting protocol information
Discovering ports.
info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Number of Ports announced: 13
info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Port 0-0
..info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Mode 0-0-0
.......info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Mode 0-0-1
.......info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Mode 0-0-2
.......info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Mode 0-0-3
.......info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Mode 0-0-4
.......info: SharpBrick.PoweredUp.Functions.DiscoverPorts[0]
      Discover Mode 0-0-5
.
mennolodder commented 6 months ago

It hangs on the message: PortModeInformationForRawMessage, for hub 0, port 0 modeIndex 5. KnowledgeManager.ApplyStaticProtocolKnowledge(await _protocol.SendMessageReceiveResultAsync(new PortModeInformationRequestMessage(port.PortId, modeIndex, PortModeInformationType.Raw) { HubId = _hubId }), knowledge);

Thats the TechnicLargeLinearMotor on port 0, if I unplug it, it does the same for the other TechnicLargeLinearMotors.

For the WeDo 2 it gives the following error (I added the raw data of the sending and receiving, this is the Name message (PortModeInformationType.Name), both in request and reply.:

  Sending 06:00:22:02:00:00
  Received 11:00:44:02:00:00:4C:50:46:32:2D:4D:4D:4F:54:4F:52

  Exception in LegoWirelessProtocol Decode/Knowledge
  System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
     at SharpBrick.PoweredUp.Protocol.Formatter.PortModeInformationEncoder.Decode(Byte hubId, Span`1& data) in C:\Repos\powered-up\src\SharpBrick.PoweredUp\Protocol\Formatter\PortModeInformationEncoder.cs:line 21
     at SharpBrick.PoweredUp.Protocol.Formatter.PortModeInformationEncoder.SharpBrick.PoweredUp.Protocol.Formatter.IMessageContentEncoder.Decode(Byte hubId, Span`1& data)
     at SharpBrick.PoweredUp.Protocol.Formatter.MessageEncoder.Decode(Span`1& data, ProtocolKnowledge knowledge) in C:\Repos\powered-up\src\SharpBrick.PoweredUp\Protocol\Formatter\MessageEncoder.cs:line 86
     at SharpBrick.PoweredUp.Protocol.LegoWirelessProtocol.<ConnectAsync>b__15_0(Byte[] data) in C:\Repos\powered-up\src\SharpBrick.PoweredUp\Protocol\LegoWirelessProtocol.cs:line 55

Edit: Here the line fails (PortModeInformationEncoder line 21) PortModeInformationForNameMessage(portId, mode, Encoding.ASCII.GetString(payload.Slice(0, payload.IndexOf<byte>(0x00)))), The call payload.IndexOf<byte>(0x00) returns -1 , causing the Slice to fail.

mennolodder commented 6 months ago

So I dug into the decode error, it seems that some motors don't follow the specs:

image

My Large TechnicLineairMotor all zero fill these messages, instead of taking variable length (these messages match your dumps): Example: 11:00:44:00:04:00:4C:4F:41:44:00:00:00:00:00:00:00

For the wedo 2.0 motor the name is 'LPF2-MMOTOR' which fills all characters so doesn't have a 0x00 to terminate on.

I'll see if I can make a fix for it and make a PR.

mennolodder commented 6 months ago

Edit: create #195 for this.

I've been able to debug this:

It goes wrong here:

dbug: SharpBrick.PoweredUp.Bluetooth.BluetoothKernel[0]
      > 06-00-22-00-05-00
dbug: SharpBrick.PoweredUp.Bluetooth.BluetoothKernel[0]
      < 05-00-05-22-06
info: SharpBrick.PoweredUp.Protocol.LegoWirelessProtocol[0]
      Received 05:00:05:22:06
fail: SharpBrick.PoweredUp.Functions.TraceMessages[0]
      Error - InvalidUse from PortModeInformationRequest

The reply translates as: Error - Port Mode Information Request - Port 0 - Invalid use (e.g. parameter error(s)

Which makes sense, because it is requesting mode 5 (so the 6th mode) where earlier the device announced 5 modes (( checked, the bold number is 'Total Mode Count'_:

dbug: SharpBrick.PoweredUp.Bluetooth.BluetoothKernel[0]
      > 05-00-21-00-01
dbug: SharpBrick.PoweredUp.Bluetooth.BluetoothKernel[0]
      < 0B-00-43-00-01-0F-**05**-1E-00-1F-00
info: SharpBrick.PoweredUp.Functions.TraceMessages[0]
      Port Information - Port 0/0 Total Modes 5 / Capabilities Output:True, Input:True, LogicalCombinable:True, LogicalSynchronizable:True / InputModes: 1E, OutputModes: 1E

I didn't doublecheck this, but this probably is because the TechnicLargeLinearMotor.GetStaticPortInfoMessages announces 6 modes: 0B-00-43-00-01-0F-06-1E-00-1F-00

For my motor it outputs this version info. could this be two versions?

     < 0F-00-04-00-01-2E-00-00-10-00-00-00-10-00-00
info: SharpBrick.PoweredUp.Functions.TraceMessages[0]
      Attached IO - Port 0/0 of device type TechnicLargeLinearMotor (HW: 0.0.0.1000 / SW: 0.0.0.1000)

If I remove the static port info messages it works and I get:

0B-00-43-00-01-0F-05-1E-00-1F-00
07-00-43-00-02-0E-00
11-00-44-00-00-00-50-4F-57-45-52-00-00-00-00-00-00
0E-00-44-00-00-01-00-00-C8-C2-00-00-C8-42
0E-00-44-00-00-02-00-00-C8-C2-00-00-C8-42
0E-00-44-00-00-03-00-00-C8-C2-00-00-C8-42
0A-00-44-00-00-04-50-43-54-00
08-00-44-00-00-05-00-10
0A-00-44-00-00-80-01-00-01-00
11-00-44-00-01-00-53-50-45-45-44-00-00-00-00-00-00
0E-00-44-00-01-01-00-00-C8-C2-00-00-C8-42
0E-00-44-00-01-02-00-00-C8-C2-00-00-C8-42
0E-00-44-00-01-03-00-00-C8-C2-00-00-C8-42
0A-00-44-00-01-04-50-43-54-00
08-00-44-00-01-05-10-10
0A-00-44-00-01-80-01-00-04-00
11-00-44-00-02-00-50-4F-53-00-00-00-00-00-00-00-00
0E-00-44-00-02-01-00-00-B4-C3-00-00-B4-43
0E-00-44-00-02-02-00-00-C8-C2-00-00-C8-42
0E-00-44-00-02-03-00-00-B4-C3-00-00-B4-43
0A-00-44-00-02-04-44-45-47-00
08-00-44-00-02-05-08-08
0A-00-44-00-02-80-01-02-04-00
11-00-44-00-03-00-41-50-4F-53-00-00-00-00-00-00-00
0E-00-44-00-03-01-00-00-34-C3-00-00-33-43
0E-00-44-00-03-02-00-00-48-C3-00-00-48-43
0E-00-44-00-03-03-00-00-34-C3-00-00-33-43
0A-00-44-00-03-04-44-45-47-00
08-00-44-00-03-05-08-08
0A-00-44-00-03-80-01-01-03-00
11-00-44-00-04-00-4C-4F-41-44-00-00-00-00-00-00-00
0E-00-44-00-04-01-00-00-00-00-00-00-FE-42
0E-00-44-00-04-02-00-00-00-00-00-00-C8-42
0E-00-44-00-04-03-00-00-00-00-00-00-FE-42
0A-00-44-00-04-04-50-43-54-00
08-00-44-00-04-05-08-08
0A-00-44-00-04-80-01-00-01-00

Here are the differences with what is checked in: image

image

mennolodder commented 5 months ago

Update, just because I wondered why this wasn't closed.

This issue was twofold:

  1. closed in #194 to support messages that don't seem to follow specs
  2. still open #197 to update the technic large motor.