(debug - code attached) Read /dev/ttyUSB0 from a C program


hi,
i exchange messages arduino linux c program. first step read serial port, wrote conde on arduino diecimila send me sequence 0 1 2 3 , on.
i print on video sequence.
the problem using debugger (in particular ddd) able read characters arduino send me without debugger not able print them.
i read correctly sequence using linux command

tail -f < /dev/ttyusb0

follows arduino , c codes. c code comes
http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
in particular
http://todbot.com/arduino/host/arduino-serial/arduino-serial.c

thanks in advance
marco

================
arduino code
================


#define init_loop_delay 1000
#define init_serial_baud_rate 9600

int loop_delay;
int i=0;

void setup() {
   loop_delay=init_loop_delay;
   serial.begin(init_serial_baud_rate);
   serial.flush();
}


void loop() {
   if (loop_delay>0) delay(loop_delay);
   serial.println(string(i));
   serial.flush();
   i++;
   

}



+++++++++++++++++++++++++
================
c code
================

#include <stdio.h>    /* standard input/output definitions */
#include <stdlib.h>
#include <stdint.h>   /* standard types */
#include <string.h>   /* string function definitions */
#include <unistd.h>   /* unix standard function definitions */
#include <fcntl.h>    /* file control definitions */
#include <errno.h>    /* error number definitions */
#include <termios.h>  /* posix terminal control definitions */
#include <sys/ioctl.h>
//#include <getopt.h>

#define baud_rate b9600
#define string_serial_port_length 256
#define string_buf_length 256

#define serial_port_device "/dev/ttyusb0"


int serialport_read(int fd, char* c)
{
  int n;
    {
        n = read(fd, c, 1);  // read char @ time
        if( n==-1) return -1;    // couldn't read
        if( n==0 ) {
            usleep( 10 * 1000 ); // wait 10 msec try again
            continue;
        }
   if ((*c=='\n') || (*c=='\r')) n=0;
    } while( n<=0 );
    return 0;
}

// takes string name of serial port (e.g. "/dev/tty.usbserial","com1")
// , baud rate (bps) , connects port @ speed , 8n1.
// opens port in raw mode can send binary data.
// returns valid fd, or -1 on error
int serialport_init(const char* serialport, int baud)
{
    struct termios toptions;
    int fd;
   
    //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
    //        serialport,baud);

    fd = open(serialport, o_rdwr | o_noctty | o_ndelay);
    if (fd == -1)  {
        perror("init_serialport: unable open port ");
        return -1;
    }
   
    if (tcgetattr(fd, &toptions) < 0) {
        perror("init_serialport: couldn't term attributes");
        return -1;
    }
    speed_t brate = baud; // let override switch below if needed
    switch(baud) {
    case 4800:   brate=b4800;   break;
    case 9600:   brate=b9600;   break;
#ifdef b14400
    case 14400:  brate=b14400;  break;
#endif
    case 19200:  brate=b19200;  break;
#ifdef b28800
    case 28800:  brate=b28800;  break;
#endif
    case 38400:  brate=b38400;  break;
    case 57600:  brate=b57600;  break;
    case 115200: brate=b115200; break;
    }
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);

    // 8n1
    toptions.c_cflag &= ~parenb;
    toptions.c_cflag &= ~cstopb;
    toptions.c_cflag &= ~csize;
    toptions.c_cflag |= cs8;
    // no flow control
    toptions.c_cflag &= ~crtscts;

    toptions.c_cflag |= cread | clocal;  // turn on read & ignore ctrl lines
    toptions.c_iflag &= ~(ixon | ixoff | ixany); // turn off s/w flow ctrl

    toptions.c_lflag &= ~(icanon | echo | echoe | isig); // make raw
    toptions.c_oflag &= ~opost; // make raw

    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
    toptions.c_cc[vmin]  = 0;
    toptions.c_cc[vtime] = 20;
   
    if( tcsetattr(fd, tcsanow, &toptions) < 0) {
        perror("init_serialport: couldn't set term attributes");
        return -1;
    }

    return fd;
}



int main(int argc, char *argv[])
{
    int fd = 0;
    char serial_port[string_serial_port_length];
    int baudrate = baud_rate;  // default
    char c='*';
    int rc,n;

    (n=0;n<string_serial_port_length;n++) serial_port[n]='0';

 
    strcpy(serial_port,serial_port_device);
    fd = serialport_init(serial_port, baudrate);
    if(fd==-1) {printf("unable open device"); return -1;}
   
    while (1) {
    serialport_read(fd, &c);
            printf("%c",c);
       fflush(null);
    }
} // end main
   



Arduino Forum > Using Arduino > Interfacing w/ Software on the Computer > (debug - code attached) Read /dev/ttyUSB0 from a C program


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial