0

I am working on linux uart. In my application I can get interrupt via signal handler with above sample code.

related link --> setting serial port interruption in linux

and I used above code too. link --> http://www.tldp.org/HOWTO/text/Serial-Programming-HOWTO

  #include <termios.h>
  #include <stdio.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <sys/signal.h>
  #include <sys/types.h>

  #define BAUDRATE B38400
  #define MODEMDEVICE "/dev/ttyS1"
  #define _POSIX_SOURCE 1 /* POSIX compliant source */
  #define FALSE 0
  #define TRUE 1

  volatile int STOP=FALSE; 

  void signal_handler_IO (int status);   /* definition of signal handler */
  int wait_flag=TRUE;                    /* TRUE while no signal received */

  main()
  {
    int fd,c, res;
    struct termios oldtio,newtio;
    struct sigaction saio;           /* definition of signal action */
    char buf[255];

    /* open the device to be non-blocking (read will return immediatly) */
    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fd <0) {perror(MODEMDEVICE); exit(-1); }

    /* install the signal handler before making the device asynchronous */
    saio.sa_handler = signal_handler_IO;
    saio.sa_mask = 0;
    saio.sa_flags = 0;
    saio.sa_restorer = NULL;
    sigaction(SIGIO,&saio,NULL);

    /* allow the process to receive SIGIO */
    fcntl(fd, F_SETOWN, getpid());
    /* Make the file descriptor asynchronous (the manual page says only 
       O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
    fcntl(fd, F_SETFL, FASYNC);

    tcgetattr(fd,&oldtio); /* save current port settings */
    /* set new port settings for canonical input processing */
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | ICRNL;
    newtio.c_oflag = 0;
    newtio.c_lflag = ICANON;
    newtio.c_cc[VMIN]=1;
    newtio.c_cc[VTIME]=0;
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);

    /* loop while waiting for input. normally we would do something
       useful here */ 
    while (STOP==FALSE) {
      printf(".\n");usleep(100000);
      /* after receiving SIGIO, wait_flag = FALSE, input is available
         and can be read */
      if (wait_flag==FALSE) { 
        res = read(fd,buf,255);
        buf[res]=0;
        printf(":%s:%d\n", buf, res);
        if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
        wait_flag = TRUE;      /* wait for new input */
      }
    }
    /* restore old port settings */
    tcsetattr(fd,TCSANOW,&oldtio);
  }

  /***************************************************************************
  * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
  * characters have been received.                                           *
  ***************************************************************************/

  void signal_handler_IO (int status)
  {
    printf("received SIGIO signal.\n");
    wait_flag = FALSE;
  }

This code generate interrupt when some byte drop the my uart RX pin. But I want to generate another interrupt when I send bytes via write() function.

How can I catch a signal when send byte to UART from TX pin ? Will write() function generate signal ? if yes which signal should I catch. ?

And How should I init sigaction for that issue. Will it be same ?

Is there any example or do you have suggestion ?

Community
  • 1
  • 1
  • Do you realize that your program is not actually interacting with the UART hardware? Do you realize that the data is fully buffered between your program and the UART? – sawdust Mar 05 '16 at 01:37
  • Yes data is buffering between my program and my uart buffer. As far as understood from that behaouivor when some data reach to my uart buffer of RX pin linux publishing a signal. I am curious about when try to send data to line my write function or my TX pin will publish a signal ? – Kaan Kandemir Mar 05 '16 at 07:18
  • *"As far as understood..."* -- What you write seems to indicate that you still don't understand that the time distance from the UART RxD pin to your program is many instruction cycles and a few buffer copies apart. Asynchronous input is IMO a lot of effort for very little gain; I'd rather use threads. There is no transmit signal. *"...or my TX pin will publish a signal"* -- Again, signals are not related to the hardware. Your program only "reads" from and "writes" to system buffers, not from/to the UART or a "UART buffer". – sawdust Mar 05 '16 at 08:28
  • You can be right I am not expert in linux. I thought those signal publishing from kernel when some bytes reached to UART buffer. And I thought If I can get a signal when I have bytes in reads linux will publish signal for write too. That 's what I want to learn. I attempt to use thread on my system but which was suffred me a lot that's why I let it go :). So then Is there any way to get intrerrupt when application attemp to write byte to line or when get bytes from the line ? Light me up ! – Kaan Kandemir Mar 05 '16 at 08:38

0 Answers0