Okay thanks, that was very useful. I found on the USB-I2C Communications Module link a string of bytes that is supposed to read from the compass. If you want to look at it, its about 40% down on the page under "Reading from I2C devices with a 1 byte internal address register". However, the program still gets stuck when I try to read. My code looks like this now -
Code:
//55 for command to read for 1 byte addressed devices
//C1 for device i2c addr with r/w bit high
//bearing register
//number to read
unsigned char cmd[4] = {0x55, 0xC1, 0x02, 0x02};
unsigned char receive[2];
int num_sent;
int num_read;
while(1) {
num_sent = write(fd, cmd, 4);
if(num_sent < 0) {
printf("\nError sending: %m", errno);
exit(1);
}
printf("\nbefore read, num_sent: %i\n", num_sent);
num_read = read(fd, receive, sizeof(unsigned char)*2);
printf("\n%i %i\n", (int)receive[0], (int)receive[1]);
}
It appears to send the command okay...num_sent is always 4. But it can't read anything...it just hangs even when I tell it to only read one byte. It says -
"The USB-I2C will perform the read operation on the I2C bus and send two bytes back to the PC - high byte first. The PC should wait for both bytes to be returned (timing out after 500mS) before proceeding with the next transaction."
So it happens as soon as I write? I feel like this a dumb question, but is there another way to get the bytes sent back without calling read? Also, 500 milliseconds seems like a really fast timeout time...maybe it times out before I can read it? The link also says that "The USB-I2C module takes care of all the I2C bus requirements such as start/restart/stop sequencing and handles the acknowledge cycles". Is there a way to check that this is actually happening?