Qt 无法正确读取i2c设备

Qt 无法正确读取i2c设备,qt,embedded,embedded-linux,ioctl,i2c,Qt,Embedded,Embedded Linux,Ioctl,I2c,我正在尝试编写一个嵌入式qt应用程序来读取特定的i2c rtc设备。以下是我初始化i2c的代码: int addr = 0x68; // The I2C address of the RTC sprintf(filename,I2C_FILE_NAME); if ((file = open(filename,O_RDWR)) < 0) { qDebug()<<"Failed to open the bus."; return; } if (io

我正在尝试编写一个嵌入式qt应用程序来读取特定的i2c rtc设备。以下是我初始化i2c的代码:

int addr = 0x68;        // The I2C address of the RTC

sprintf(filename,I2C_FILE_NAME);
if ((file = open(filename,O_RDWR)) < 0)
{
    qDebug()<<"Failed to open the bus.";
    return;
}

if (ioctl(file,I2C_SLAVE_FORCE,addr) < 0)
{
    qDebug()<<"Failed to acquire bus access and/or talk to slave.\n";
    return;
}
int addr=0x68;//RTC的I2C地址
sprintf(文件名,I2C文件名);
如果((文件=打开(文件名,O_RDWR))<0)
{

qDebug()问题在于寄存器不包含秒数的简单计数。高位保存数十秒,低位保存单位秒

使用
(inbuf[0]>>4)
可以获得数十秒,使用
inbuf[0]&0xf
可以获得单位秒

而不是尝试使用
qDebug()4)&0x7)+(inbuf[0]&0xf)打印秒数;

Wow,使用qDebug()而不是printf()在一个非qt的打印代码中……这真是一个奇怪的想法。:-)这是哪个i2c设备?这是一个RTC,ds1337。这是数据表,你怎么知道这一点而不知道这个设备?@MartinJames:OP实际上并没有发布这个设备,所以我不确定你会读什么手册……也许OP和Krueger之前有过讨论,所以t上下文是已知的,但对于像我这样的新人来说,它不是。这就是为什么要求澄清…该设备是一个RTC,ds1337。这里是数据表datasheets.maximpintegrated.com/en/ds/ds1337-DS1337C.pdfit工作,但我不明白在不知道该设备的情况下,您如何理解?所有RTC设备都有相同的标准吗?使用二进制代码时钟值的d十进制不是一个标准,但它也很常见。如果您将问题中的示例计数转换为十六进制表示,则模式很容易看到。
unsigned char addr = 0x68;
unsigned char reg = 0x00;
unsigned char inbuf[2], outbuf;

struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];

/*
* In order to read a register, we first do a "dummy write" by writing
* 0 bytes to the register we want to read from.  This is similar to
* the packet in set_i2c_register, except it's 1 byte rather than 2.
*/
outbuf = reg;
messages[0].addr  = addr;
messages[0].flags = 0;
messages[0].len   = sizeof(outbuf);
messages[0].buf   = &outbuf;
/* The data will get returned in this structure */
messages[1].addr  = addr;
messages[1].flags = I2C_M_RD/* | I2C_M_NOSTART*/;
messages[1].len   = 2;
messages[1].buf   = inbuf;
/* Send the request to the kernel and get the result back */
packets.msgs      = messages;
packets.nmsgs     = 2;
if(ioctl(file, I2C_RDWR, &packets) < 0)
{
    qDebug()<<"Unable to send data";
    return;
}
qDebug() << inbuf[0];
qDebug() << (10 * ((inbuf[0] >> 4) & 0x7) + (inbuf[0] & 0xf));