Skip to content

System.ArgumentException: Parameters 'writeBuffer' and 'readBuffer' must have the same length #917

@olavt

Description

@olavt

I wrote a small test program and when running it on a Raspberry PI 3 with a PiFace Digital 2, I get the following exception:

Unhandled exception. System.ArgumentException: Parameters 'writeBuffer' and 'readBuffer' must have the same length.
at System.Device.Spi.UnixSpiDevice.TransferFullDuplex(ReadOnlySpan1 writeBuffer, Span1 readBuffer)
at Iot.Device.Mcp23xxx.Mcp23xxx.SpiAdapter.Read(Byte registerAddress, Span1 buffer) at Iot.Device.Mcp23xxx.Mcp23xxx.InternalRead(Register register, Span1 buffer, Port port)
at Iot.Device.Mcp23xxx.Mcp23xxx.InternalReadByte(Register register, Port port)
at Iot.Device.Mcp23xxx.Mcp23x1x.ReadByte(Register register, Port port)
at TestPiface.Program.Main(String[] args) in /home/vsts/work/1/s/Electronics/Implementations/TestPiface/Program.cs:line 31

The line causing the issue is:

        byte currentValue = mcp23s17.ReadByte(Register.GPIO, Port.PortA);

Shouldn't it be possible to read the state of the output ports like this?

This is the complete program:

using System;
using System.Threading.Tasks;
using System.Threading;
using Iot.Device.Mcp23xxx;
using System.Device.Spi;

namespace TestPiface
{
class Program
{
private static readonly int s_deviceAddress = 0x20;

    private static Mcp23s17 mcp23s17;

    static void Main(string[] args)
    {
        var spiConnectionSettings = new SpiConnectionSettings(0, 0)
        {
            ClockFrequency = 1000000,
            Mode = SpiMode.Mode0
        };

        var spiDevice = SpiDevice.Create(spiConnectionSettings);

        mcp23s17 = new Mcp23s17(spiDevice, s_deviceAddress);

        mcp23s17.WriteByte(Register.IODIR, 0b0000_0000, Port.PortA);    // set port A as an output
        mcp23s17.WriteByte(Register.IODIR, 0b1111_1111, Port.PortB);    // set port B as an input
        mcp23s17.WriteByte(Register.GPIO, 0b0000_0000, Port.PortA);     // Clear all outputs

        byte currentValue = mcp23s17.ReadByte(Register.GPIO, Port.PortA);

        mcp23s17.WriteByte(Register.GPIO, 0b0000_0010, Port.PortA);
    }

}

}

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutStatus: In PRarea-device-bindingsDevice Bindings for audio, sensor, motor, and display hardware that can used with System.Device.GpiobugSomething isn't workingup-for-grabsGood issue for external contributors to iot

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions