PCA9555 на Raspberry Pi C # Mono - PullRequest
       8

PCA9555 на Raspberry Pi C # Mono

0 голосов
/ 13 ноября 2018

Я написал программу для чтения регистров из расширителя портов PCA9555 с помощью Mono.Как я могу установить порт 0 и порт 1 в качестве входов?Поэтому, когда я использую I2c-обнаружение, я вижу чип по адресу 0x18:

enter image description here] 1

Код на C #:

/// <summary>
/// MODE_AUTO   0
/// MODE_QUICK  1
/// MODE_READ   2 - 
/// MODE_FUNC   3 - 'V': version = 1; 'y': yes = 1; 'l': device list = 1; 
/// </summary>
class Program
{
    private const int AutoMode = 0;
    private const int QuickMode = 1;
    private const int OpenReadAndWriteMode = 2;
    private const int I2cToolsFunctionMode = 3;
    //private static int DeviceAddress = 0x0703;
    private static int DeviceAddress = 0x20;

    [DllImport("libc.so.6", EntryPoint = "open")]
    public static extern int Open(string fileName, int mode);

    [DllImport("libc.so.6", EntryPoint = "ioctl", SetLastError = true)]
    private extern static int Ioctl(int fd, int request, int data);

    [DllImport("libc.so.6", EntryPoint = "read", SetLastError = true)]
    internal static extern int Read(int handle, byte[] data, int length);

    static void Main(string[] args)
    {
        var logger = NLog.LogManager.GetCurrentClassLogger();

        Console.WriteLine("################ I2C test application ################\n");

        // read from I2C device bus 1
        try
        {
            var i2cBushandle = Open("/dev/i2c-2", OpenReadAndWriteMode);
            // open the slave device at address 0x48 for communication

            int registerAddress = 0x00;
            var deviceReturnCode = Ioctl(i2cBushandle, DeviceAddress, registerAddress);

            // read the first two bytes from the device into an array
            var deviceDataInMemory = new byte[2];
            Read(i2cBushandle, deviceDataInMemory, deviceDataInMemory.Length);

            for (int i = 0; i < deviceDataInMemory.Length; i++)
            {
                Console.Write($"{deviceDataInMemory[i]}  ");
            }

            Console.WriteLine();
            Console.WriteLine("Done!" + "Device code" + deviceReturnCode);
            Console.WriteLine($"Most significant byte = {deviceDataInMemory[0]}");
            Console.WriteLine($"Least significant byte = {deviceDataInMemory[1]}");
        }
        catch (Exception exception)
        {
            Console.WriteLine($"Error! {exception.Message}");
            logger.Debug($"Error! {exception.Message}");
            logger.Info($"Error! {exception.Message}");
        }

        Console.ReadKey();
    }
}

Когда я запускаю приложение на RPi, я получаю только 255 значений:

enter image description here

Но на экране байты отличаются.Я не знаю, как мне настроить чип и прочитать его.Буду признателен за любую помощь.

...