I2C read mit C-Programm Raspberry PI

OP #4842078
Lesenswert?

Hallo, ich möchte ein I2C Touch mit einem C-Programm auslesen (mit 
i2cdump -y 1 0x24 i erhalte ich Daten) aber mit folgenden Programm Code 
siehe unten erhalte ich nichts, d.h:
Data read:
Data read:
Data read:

Erwarten tue ich aber mindestens Hex-Werte wie 0x00 oder 00... aber gar 
nichts?
Ist die Variable "buffer" vom falschen Typ?


Hier mein C-Code:
**************************
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

int file_i2c;
int length;
int i;
unsigned char buffer[2] = {0};


//----- OPEN THE I2C BUS -----
int readi2cbus()
{
        char *filename = (char*)"/dev/i2c-1";
        if ((file_i2c = open(filename, O_RDWR)) < 0)
        {
                //ERROR HANDLING: you can check errno to see what went 
wrong
                printf("Failed to open the i2c bus");
                return;
        }


        int addr = 0x24;          //<<<<<The I2C address of the slave
        if (ioctl(file_i2c, I2C_SLAVE, addr) < 0)
        {
                printf("Failed to acquire bus access and/or talk to 
slave.\n");
                //ERROR HANDLING; you can check errno to see what went 
wrong
                return;
        }


        //----- READ BYTES -----
        length = sizeof(buffer);                        //<<< Number of 
bytes to read
        if (read(file_i2c, buffer, length) != length)           //read() 
returns the number of bytes actually read, if it doesn't match t$
        {
                //ERROR HANDLING: i2c transaction failed
                printf("Failed to read from the i2c bus.\n");
        }
        else
        {
                for(i=0;i<=length;i++)
                {
                printf("Data read: %c\n", buffer[i]);
                }
        }
}

//----- WRITE BYTES -----
int writei2cbus()
{
        buffer[0] = 0x00;
        buffer[1] = 0x00;
        length = 2;                     //<<< Number of bytes to write
        if (write(file_i2c, buffer, length) != length) 
//write()      returns the number of bytes actually written, if it 
doesn't mat$
        {
                /* ERROR HANDLING: i2c transaction failed */
                printf("Failed to write to the i2c bus.\n");
        }
}



int main()
{

        writei2cbus();
        readi2cbus();

//      return 0;
}
Gast #4844101
Lesenswert?

Ich schlage vor, du spaltest eine openi2cbus()-Funktion von deiner 
readi2cbus() Funktion ab und rufst diese in main() auf. Dann wirst du 
draufkommen, dass du derzeit in writei2cbus() auf den i2cbus schreibst, 
und ihn aber erst später in readi2cbus() öffnest.
OP #4844827
Lesenswert?

Rolf M. schrieb:
> aus. Wenn du es als Zahl haben willst, wäre %d richtig, bzw. %x für
> hexadezimale Darstellung.

vielen Dank, das war die Lösung.

tictactoe schrieb:
> Ich schlage vor, du spaltest eine openi2cbus()-Funktion von deiner
> readi2cbus() Funktion ab und rufst diese in main() auf. Dann wirst du
> draufkommen, dass du derzeit in writei2cbus() auf den i2cbus schreibst,
> und ihn aber erst später in readi2cbus() öffnest.

stimmt, ganz anfangs hatte ich aber keine Funktionen und es hatte 
trotzdem nicht funktioniert, also sicher das mit %x die Lösung, aber das 
mit den Funktionen ein weiterer Fehler. Danke :-)

hier der funktionierende Code:
*********************************

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

int file_i2c = -1;
int length;
int i;
unsigned char buffer[40] = {0};


//----- OPEN THE I2C BUS -----
int openi2cbus()
{
        char *filename = (char*)"/dev/i2c-1";
        if ((file_i2c = open(filename, O_RDWR)) < 0)
        {
                //ERROR HANDLING: you can check errno to see what went 
wrong
                printf("Failed to open the i2c bus");
                return;
        }
}

int readi2cbus()
{
        int addr = 0x24;          //<<<<<The I2C address of the slave
        if (ioctl(file_i2c, I2C_SLAVE, addr) < 0)
        {
                printf("Failed to acquire bus access and/or talk to 
slave.\n");
                //ERROR HANDLING; you can check errno to see what went 
wrong
                return;
        }


        //----- READ BYTES -----
        length = sizeof(buffer);                        //<<< Number of 
bytes to read
        if (read(file_i2c, buffer, length) != length)           //read() 
returns the number of bytes actually read, if it doesn't match then an 
error oc$
        {
                //ERROR HANDLING: i2c transaction failed
                printf("Failed to read from the i2c bus.\n");
        }
        else
        {
                for(i=0;i<=length;i++)
                {
                printf("Data read: %x\n", buffer[i]);
                }
        }
}

//----- WRITE BYTES -----
int writei2cbus()
{
        buffer[0] = 0x00;
        buffer[1] = 0x00;
        length = 2;                     //<<< Number of bytes to write
        if (write(file_i2c, buffer, length) != length) 
//write() returns the number of bytes actually written, if it doesn't 
match then an erro$
        {
                /* ERROR HANDLING: i2c transaction failed */
                printf("Failed to write to the i2c bus.\n");
        }
}



int main()
{
        openi2cbus();
        writei2cbus();
        readi2cbus();

//      printf("Hello, World! \n");
        return 0;
}
*****************************************

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren