USB-I2C -> SRF08 on Linux

General & specific discussions on our Ultrasonic Rangers

Moderator: chris

USB-I2C -> SRF08 on Linux

Postby Bram » Tue Feb 19, 2008 11:27 am

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.
Bram
 
Posts: 2
Joined: Tue Feb 19, 2008 11:19 am

Postby diggie » Sat Apr 05, 2008 7:38 pm

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!
diggie
 
Posts: 3
Joined: Sat Apr 05, 2008 7:29 pm

Got it working in Perl

Postby diggie » Tue Apr 08, 2008 12:20 am

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;
diggie
 
Posts: 3
Joined: Sat Apr 05, 2008 7:29 pm

Re: USB-I2C -> SRF08 on Linux

Postby Inuke » Wed Jun 04, 2008 3:45 pm

I'm experiencing the same problem, can anyone help?
Inuke
 
Posts: 1
Joined: Wed Jun 04, 2008 3:42 pm

Re: USB-I2C -> SRF08 on Linux

Postby andrewholt » Wed Jul 08, 2009 8:13 pm

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);
}
andrewholt
 
Posts: 2
Joined: Wed Jul 08, 2009 8:01 pm

Re: USB-I2C -> SRF08 on Linux

Postby andrewholt » Thu Jul 09, 2009 10:57 am

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
andrewholt
 
Posts: 2
Joined: Wed Jul 08, 2009 8:01 pm

Re: USB-I2C -> SRF08 on Linux

Postby diggie » Tue Sep 08, 2009 1:34 pm

Just to let you know I posted the perl code:

http://blog.sog-ict.nl/2009/09/srf-08-in-perl/
diggie
 
Posts: 3
Joined: Sat Apr 05, 2008 7:29 pm


Return to Ultrasonic Sensors

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests