USB-I2C -> SRF08 on Linux
I am trying to use a SRF08 sensor with a USB-I2C converter on my linux box, the writing to the port seems to work fine (the red LED on the SRF08 flashes when I set it to ranging mode) however the reading doesn't work. I am not getting back any data.
first I write the byte sequence to set it to ranging mode, then I send the sequence for reading the light sensor data and then I read the data from the port.
all I am getting as output is "read() Failed!!" and "output: 85", so the read() function doest work at all and sbuf[0] is still 0x55 (or 85 in decimal)
the is the code I am using:
I hope you guys can give me some good tips on how to fix this.
first I write the byte sequence to set it to ranging mode, then I send the sequence for reading the light sensor data and then I read the data from the port.
all I am getting as output is "read() Failed!!" and "output: 85", so the read() function doest work at all and sbuf[0] is still 0x55 (or 85 in decimal)
the is the code I am using:
- Code: Select all
#include <stdio>
#include <string>
#include <unistd>
#include <fcntl>
#include <errno>
#include <termios>
struct termios options;
int open_port(void)
{
int fd; // File descriptor for the port
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyUSB0 - "); // Could not open the port.
}
else
{
fcntl(fd, F_SETFL, 0);
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 19200...
cfsetispeed(&options, B19200);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
// Set no parity bit
options.c_cflag &= ~PARENB;
// Set 2 stop bits
options.c_cflag &= ~CSTOPB;
// Set the character size
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
fcntl(fd, F_SETFL, FNDELAY);
}
return (fd);
}
int main()
{
int fd = open_port();
unsigned char sbuf[64];
int x = 0;
while(x < 4)
{
// set ranging mode in cm
sbuf[0] = 0x55;
sbuf[1] = 0xE0;
sbuf[2] = 0x00;
sbuf[3] = 0x01;
sbuf[4] = 0x51;
write(fd, sbuf, 5);
usleep(750000);
// read light sensor
sbuf[0] = 0x55;
sbuf[1] = 0xE1;
sbuf[2] = 0x01;
sbuf[3] = 0x01;
write(fd, sbuf, 4);
usleep(750000);
int r = read(fd, sbuf, 1);
if(r < 0)
printf("read() failed!!");
int output = sbuf[0];
printf("output: %d\n\n", output);
x++;
}
close(fd);
}
I hope you guys can give me some good tips on how to fix this.