SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By sharana
#22669
Hey Guys

I was wondering if any of you were able to hardwire your IMU to the serial port through a RS232 to TTL converter (Shifter SMD). I've been trying to do that with my IMU but it just doesn't connect. I have a 5V power output coming from a power supply unit which I input into the IMU and the "RS232 to TTL" converter and then I connect the RX of IMU to TX of converter and TX of IMU to RX of converter. After doing this I've tried using the Mixer and the Hyperterminal but there is no streaming of data.

Can you please let me know if you've been able to hardwire your IMU and can you please give me your connection details?

Thanks,
Sharan
By geekything
#22674
Hi Sharan,

The Shifter requires that you share a common ground between it, the device you're connecting to, and the host. (Or at least between it and the device; can't quite remember).

Without that, it won't work. It's to do with the funky transistor tricks it does to achieve level conversion.

Hope that helps.

-marc
By sharana
#22680
Thanks Marc...

The connection seems to be functional no.w. The IMU device is streaming data. I'm using minicom and it works perfectly. But I'm having difficulties interpreting the data when I'm communicating with the device using a C program. The data stream looks like this...

" /2529-48x|-121+)Dv[!(-20-12625|{p-88Y*!z-86-1-87&21D^HrY*13Fp-25(vP8.-16-73-82ih-50{|i-21Pa)(8e25-6-9k-60v7`tQ*5|Y*9-91-12 -94 -88PN126lQhYkP*5-121-P%-93RRDP-101|i-21\-88P-117-1426p(126-18A-88917"8!P-30-111-88"11-31qkYhY-21X a&721Dr-63(FY*!z-86-1-87&21D^HrY*13Fp-25(vP8.p-73.i-24N-5|i-21Pa)(8e25z-9kDv7`-12Q*5|Y*9%t "
(P-50126lQ-24Yk-48*5-121-P-91-93R-46DP-101|i-21\(P-117r26-16-88126-18A-88917"8!

Any idea why this is happening?? Below is my C code!

Thanks Again,
sharan
Code: Select all
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
                                                                                
#define BAUDRATE B19200
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1
                                                                                
FILE *input;
FILE *output;
                                                                                
int open_port(void)
{
  int fd; /* File descriptor for the port */
  fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd == -1)
    perror("open_port: Unable to open /dev/ttyS0 - "); //Could not open the port  else
    fcntl(fd, F_SETFL, 0);
  return (fd);
}
                                                                                
main()
{
  int fd, c, n, i, In1;
  char str[255], message[90];
  struct termios options;
                                                                                
  output = fopen("/dev/tty", "w");     //open the terminal screen
  input = fopen("/dev/tty", "r");     //open the terminal screen
  if (!output || !input)
  {
    fprintf(stderr, "Unable to open /dev/tty\n");
    exit(1);
  }
                                                                                
  //fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
  fd = open_port();
  //fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
                                                                                
  if (fd <0) {perror(MODEMDEVICE); exit(-1); }
  else { fcntl(fd, F_SETFL, 0);}
                                                                                
  options.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  options.c_cc[VMIN]=1;
  options.c_cc[VTIME]=0;
                                                                                
  /* Write something */
  n = write(fd, "^g", 2);
  //fcntl(fd, F_SETFL, 0);
  if (n < 0)
    fputs("write() of 4 bytes failed!", stderr);
  else
    printf("\nWrote Successfully\n");
                                                                                
  /* Make read() return immediately */
  //fcntl(fd, F_SETOWN, getpid());
  //fcntl(fd, F_SETFL, FASYNC);
  fcntl(fd, F_SETFL, FNDELAY);
                                                                                
  /* Read something until 'Q' recieved */
  //while(str[0] != 'Q')
  //{
 sleep(5);
    int retN = read(fd, str, 255);
    printf("\n\nThe read string is... %s \n\n", str);
    //fcntl(fd, F_SETFL, 0);
    if(retN > 0)
      for (i=0; i<retN; i++)
      {
        In1 = str[i];
        if((In1<32) || (In1>125))
        {
          sprintf(message, "%d", In1);
          fputs(message, output);
        }
        else fputc ((int) In1, output);
      }
        //printf("\nThe # of characters returned is %d\n", retN);
        //printf("%d\n", str);
        //fputs(str, output);
    else
    {
        perror(MODEMDEVICE);
        fputs("read() of X bytes failed!\n\n", stderr);
        //printf("\n# of charaters %d\n", retN);
    }
    close(fd);
    fclose(output);
        //puts("read() of 4 bytes failed!");
        //printf("\nread error\n");
  //}
}