wiringpi-dotnet icon indicating copy to clipboard operation
wiringpi-dotnet copied to clipboard

II2CDevice.Read(int length) doesn't seem to work

Open crazycga opened this issue 3 years ago • 1 comments

Please bear with me.

I'm trying to use the II2CDevice.Read(int length) method to bring back a six-byte array off the bus. The behavior, though, is that it is bringing back six copies of the FIRST byte of the bus. I've tried this many times, many ways, and all I get back is six of the same byte. Note also, this is a purchased I2C device (a Thunderborg) so I am unable to change its behavior. I've seen in a similar bug elsewhere that the solution was to change a connected Arduino's code to work in a specific way. I've also seen that I should be able to access the underlying WiringPi.Read(int fd, int length) type function, but that is beyond my ken at the moment.

To Reproduce Steps to reproduce the behavior:

for (int kb = 0; kb < splash.Length; kb++)
{
     log.WriteLog("Found: " + splash[kb].ToString("X2");
}

Expected behavior This SHOULD produce 0x99/0x15/0x00/0x00/0x00/0x00

Screenshots It produces:

16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99
16/03/2021 22:09:06: Found: 99

Desktop (please complete the following information):

  • Raspberry Pi 3
  • .NET IoT 3.1

Additional context Any help would be appreciated.

crazycga avatar Mar 17 '21 04:03 crazycga

PS: In PYTHON, it works:

>>> TB.RawRead(0x99, 6)
[153, 21, 0, 0, 0, 0]

I've replicated the functionality of the Python code TB.RawRead as best I can, but there's a missing piece in the C# commands, in that I can't actually get it to respond properly.

Python code in question:

def RawRead(self, command, length, retryCount = 3):
        """
RawRead(command, length, [retryCount])

Reads data back from the ThunderBorg after sending a GET command
Command codes can be found at the top of ThunderBorg.py, length is the number of bytes to read back

The function checks that the first byte read back matches the requested command
If it does not it will retry the request until retryCount is exhausted (default is 3 times)

Under most circumstances you should use the appropriate function instead of RawRead
        """
        while retryCount > 0:
            self.RawWrite(command, [])
            rawReply = self.i2cRead.read(length)
            reply = []
            for singleByte in rawReply:
                reply.append(singleByte)
            if command == reply[0]:
                break
            else:
                retryCount -= 1
        if retryCount > 0:
            return reply
        else:
            raise IOError('I2C read for command %d failed' % (command))

crazycga avatar Mar 17 '21 04:03 crazycga