Page 1 of 1

USB-I2C -> SRF08 on Linux

PostPosted: Tue Feb 19, 2008 11:27 am
by Bram
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:

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.

PostPosted: Sat Apr 05, 2008 7:38 pm
by diggie
Hi Bram,

Did you have any success with this device?
I'm trying to read information under Ubuntu, but no luck so far...

Any information is appreciated!

Got it working in Perl

PostPosted: Tue Apr 08, 2008 12:20 am
by diggie
Code not yet available for posting, but I as far as I can tell the problem lies in the serial port settings...

// Set 2 stop bits
options.c_cflag &= ~CSTOPB;

Shouldn't this be
Code: Select all
options.c_cflag |= CSTOPB;

Re: USB-I2C -> SRF08 on Linux

PostPosted: Wed Jun 04, 2008 3:45 pm
by Inuke
I'm experiencing the same problem, can anyone help?

Re: USB-I2C -> SRF08 on Linux

PostPosted: Wed Jul 08, 2009 8:13 pm
by andrewholt
Hi,

I wanted to verify that the usb-i2c device worked.

I have run the windows test using wine. It worked.

I took the example program from earlier in this post and discussion and simplified it to return the version number (see below) and it works.

So it's off to the races :)

HTH,
Andrew


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

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;
int output = 0;
int r = 0;

sbuf[0] = 0x5a;
sbuf[1] = 0x01;
sbuf[2] = 0x00;
sbuf[3] = 0x00;

write(fd, sbuf, 4);
usleep(750000);

r = read(fd, sbuf, 1);
if (r < 0)
printf("read() failed!!");


output = sbuf[0];
printf("output: %02x\n\n", output);


close(fd);
}

Re: USB-I2C -> SRF08 on Linux

PostPosted: Thu Jul 09, 2009 10:57 am
by andrewholt
Hi,

I know its bad form to reply to your own post but I spotted an issue.

I compiled and ran the same program on MAC OS X. I found an omission.

add this to open_port ..

cfsetospeed(&options, B19200);

it now works on macos X.

Regards,
Andrew

Re: USB-I2C -> SRF08 on Linux

PostPosted: Tue Sep 08, 2009 1:34 pm
by diggie
Just to let you know I posted the perl code:

http://blog.sog-ict.nl/2009/09/srf-08-in-perl/