Pages: [1]   Go Down
Author Topic: (debug - code attached) Read /dev/ttyUSB0 from a C program  (Read 1696 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I would exchange messages from Arduino to linux with a C program. The first step is read the serial port, so I wrote a conde on Arduino Diecimila that send me a sequence 0 1 2 3 and so on.
I would like print on video the sequence.
The problem is that using a debugger (in particular DDD) I am able to read the characters that arduino send to me but without a debugger I am not able to print them.
I read correctly the sequence using the linux command

tail -f < /dev/ttyUSB0

Follows Arduino and C codes. The C code comes from
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;
    do {
        n = read(fd, c, 1);  // read a char at a 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 the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
// and a baud rate (bps) and connects to that port at that speed and 8N1.
// opens the port in fully raw mode so you 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 to open port ");
        return -1;
    }
   
    if (tcgetattr(fd, &toptions) < 0) {
        perror("init_serialport: Couldn't get term attributes");
        return -1;
    }
    speed_t brate = baud; // let you 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;

    for (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 to open the device"); return -1;}
   
    while (1) {
    serialport_read(fd, &c);
            printf("%c",c);
       fflush(NULL);
    }
} // end main
   

* arduino_send_i.ino (0.3 KB - downloaded 14 times.)
* test_01_receive.c (3.41 KB - downloaded 26 times.)
Logged

Pages: [1]   Go Up
Jump to: