Find the answer to your Linux question:
Results 1 to 3 of 3
hi, I am writing a program to write and read the serial port in a linux voyage. I have the linux connected with another pc through the serial port (with ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    21

    [SOLVED] problems reading serial port

    hi, I am writing a program to write and read the serial port in a linux voyage.
    I have the linux connected with another pc through the serial port (with a crossover wire).

    It works fine when I write data (i recieve it in the other pc) but when I try to read data from de serial port it does not work. It stops in the read function and doesnt continue.

    Here is the code:

    Code:
    #include <stdio.h>  /*Standar input/output definitions */
    #include <string.h> /* String functions definitions> */
    #include <unistd.h> /* UNIX standard function definitions */
    #include <fcntl.h>  /* File control definitions */
    #include <errno.h>  /* Error number definitions */
    #include <stdlib.h>
    
    
    void write_port(int, char *);
    int open_port(char *);
    
    
    int main(int argc, char **argv){
      int fd;
      char cadena[20];
      char *buffer;
      int n = 0;
      // Open port
      fd = open_port("/dev/ttyS0");
      printf("fd = %d\n", fd);
      strcpy(cadena, "This is a write test.. OK\n");
      write_port(fd, cadena);
      while(1){
        while((n = read(fd, buffer, 10)) > 0){  //<- it gets stuck here.. though I send data 
          printf("n = %d", n);
        }
        if(n == -1){
          fprintf(stderr,"Error reading COM1 in %s, line %d\n",__FILE__,__LINE__);
          exit(EXIT_FAILURE);
        }
        printf("Data readed: %s\n",buffer);
     }
     return(0);
    }
    
    int open_port(char *device){
      int fd;
      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
      if(fd == -1){
        fprintf(stderr, "Error in %s, line %d. Unable to open %s\n",__FILE__,__LINE__, device);
      }else{
        fcntl(fd, F_SETFL, 0);
        printf("Device %s opened with fd = %d\n", device, fd);
      }
      return (fd);
    }
     void write_port(int fd, char * mensaje){
      int n;
      char cadena[1024];
      strcpy(cadena,mensaje);
      n = write(fd, cadena, strlen(cadena));
      if(n < 0)
        fprintf(stderr, "Error in %s, line %d\n",__FILE__,__LINE__);
      else
        printf("Message %s sent\n",cadena);
    }
    do you know what is hapening?

    thanks!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I assume that the port is set for buffered reads, so it won't return until it has at least 10 characters to read, or an EOL has been received (carriage-return). You can set it with fcntl() to unbuffered reads and get as many characters as are available, though you need to determine when there is no more data to be read. There are a lot of subtle issues with reading serial ports, and experience is the best way to deal with it. Try different port configurations (flow-control, buffered vs. unbuffered, different characters for end-of-line, etc), and do learn how to use the select() function...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    21
    I solve the problem. I found an example and saw where my error was. I hadn't the port properly configured.

    I also found a very good explanation ot the termios.h, so I enabled the canonical input to read full lines.

    yes.. you are right I have a lot of things to study and the select function is one of the urgent ones : )

    thanks again




    (I have tried to post the url of the example and the termios.h explanation for that ones that could have this same problem but I couldnt becouse I am new here.. but you can find them with google quite easy. For the example search for "Using Linux to Send Commands to ADR Interfaces")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...