dotnet / dotnet/iot

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

Open
#917 14 comments 1 reaction 2 assignees Claimed by @krwq View on GitHub
area-device-bindings bug Priority:1 Status: In PR up-for-grabs
Dominant language
C#
Stars
2.4k
Forks
630
Avg merge
11d 3h
Merged PRs (30d)
2

Description

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(ReadOnlySpan`1 writeBuffer, Span`1 readBuffer)
at Iot.Device.Mcp23xxx.Mcp23xxx.SpiAdapter.Read(Byte registerAddress, Span`1 buffer)
at Iot.Device.Mcp23xxx.Mcp23xxx.InternalRead(Register register, Span`1 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);
}

}
}

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.