QT界面跳转的那个错误搞定了没?还没搞定的话,我贴代码你算了。。
------解决方案-------------------- 举例来说,假如一个I2C总线上有两个设备,地址为0x40和0x50。 第一步,打开总线/dev/i2c-x,此处x代表总线号,如第一根i2c总线就是i2c-0,第二根i2c总线就是i2c-1,依此类推。 int file; int adapter_nr = 2; /* probably dynamically determined */ char filename[20];
sprintf(filename,"/dev/i2c-%d",adapter_nr); if ((file = open(filename,O_RDWR)) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); }
第三步,指定I2C总线(第一步打开的总线)上的待访问设备的地址 int addr = 0x40; /* The I2C address */ if (ioctl(file,I2C_SLAVE,addr) < 0) { /* ERROR HANDLING; you can check errno to see what went wrong */ exit(1); }
第四步,对I2C总线(第一步打开的总线)进行read/write操作,也可以通过i2c-core.c中提供的API进行读写操作。如 __u8 register = 0x10; /* Device register to access */ __s32 res; char buf[10]; /* Using SMBus commands */ res = i2c_smbus_read_word_data(file,register); if (res < 0) { /* ERROR HANDLING: i2c transaction failed */ } else { /* res contains the read word */ } /* Using I2C Write, equivalent of i2c_smbus_write_word_data(file,register,0x6543) */ buf[0] = register; buf[1] = 0x43; buf[2] = 0x65; if ( write(file,buf,3) != 3) { /* ERROR HANDLING: i2c transaction failed */ }