microsoft / microsoft/pxt-microbit

Behaviour difference with Buffer.unpack between sim and hardware

Open
#6,894 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P2 Simulator
Dominant language
TypeScript
Stars
804
Forks
721
Avg merge
2d 9h
Merged PRs (30d)
2

Description

Describe the bug

In the sim on makecode.microbit.org, Buffer.unpack() appears to pad undersized buffers and give a result. On hardware I get zero or an error code.

I originally hit the issue using radio.sendBuffer() and radio.onReceivedBuffer(). When sending an over length message it will get truncated, but the sim will still successfully unpack the last value if it is small, whereas on the microbit the truncated element unpacks to zero.

It would be good if the simulator matched the hardware behaviour (maybe always an error), because its easier to debug on the sim.

To Reproduce
Steps to reproduce the behavior:

input.onButtonPressed(Button.A, function () {
    let buf = Buffer.fromHex("ff");
    let arr = buf.unpack("B", 0);
    let out = "";
    for (let a of arr) { out += `${a}, ` };
    serial.writeLine(out);
    basic.showNumber(arr[0]);
})

input.onButtonPressed(Button.B, function () {
    let buf = Buffer.fromHex("ff");
    let arr = buf.unpack("H", 0);
    let out = "";
    for (let a of arr) { out += `${a}, ` };
    serial.writeLine(out);
    basic.showNumber(arr[0]);
})

Running this on the sim, both cases show 255

Running on a MicroBit V2.21 (the a hex built on makecode.microbit.org): Button A shows 255 and button B shows 0

Expected behavior
Same behaviour on the sim and hardware. An error would allow the user to find the issue faster, but a zero would at least be consistent

It would also be good for radio.sendBuffer() to give an error if the buffer is to big.

micro:bit version (please complete the following information):
MicroBit V2.21

Desktop (please complete the following information):

  • OS: Fedora 43
  • Browser: 1.19.13b (Firefox 150.0.3)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Button A and Button B reproduction in the simulator and on the stated micro:bit hardware, comparing Buffer.unpack("H", 0) on the one-byte buffer. Then investigate the simulator and hardware behavior for truncated buffers and oversized radio.sendBuffer() messages. Done means the simulator and hardware consistently handle undersized unpacking and the expected oversized-radio behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.