Receive serial data, does not work first

0

I'm using the code below to access the serial port of a raspberry Pi and receive the data sent by the UART, running ALMOST perfectly.

When Linux starts and runs the software it stops and does not receive anything, but if it quits and starts again it starts to receive.

If you are receiving the data I finalize the software, restart Linux and start the software again it does not receive, finalizing and starting the same normally receives, what can it be?

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

#define BAUDRATE B110
#define FALSE 0
#define TRUE 1
#define BUFFER_SIZE 9

//
volatile int STOP = FALSE;
void signal_handler_IO (int status);
int wait_flag = TRUE;
char devicename[80] = "/dev/ttyAMA0", ch;
int status;
//
int
main (int argc, char *argv[])
{
  int fd, res, i;
  struct termios newtio;
  struct sigaction saio;
  char buf[BUFFER_SIZE];
//
//open the device in non-blocking way (read will return immediately)
  fd = open (devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd < 0)
    {
      perror (devicename);
      exit (1);
    }
//
//install the serial handler before making the device asynchronous
  saio.sa_handler = signal_handler_IO;
  sigemptyset (&saio.sa_mask);  //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
  fcntl (fd, F_SETFL, FASYNC);
//
// set new port settings for canonical input processing
  newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
  newtio.c_lflag = 0;
  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)
    {
//
// read characters typed by user in non-blocking way
      ch = getchar_unlocked ();
      if (ch > 0)
//  write (fd, &ch, 1);
//
// after receiving SIGIO, wait_flag = FALSE, input is available and can be read */
      if (wait_flag == FALSE)   //if input is available
    {

      res = read (fd, buf, BUFFER_SIZE);
          sleep(1);

    if (res > 0)
        {
              printf("\n %d bytes recebidos\n", res);

          for (i = 0; i < res; i++) //for all chars in string
        {
          printf ("0x%02x\t", buf[i]);
        }

              printf("\n");
        }
      wait_flag = TRUE; /* wait for new input */
    }
    }
  close (fd);
}

//
/***************************************************************************
* 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;

}
    
asked by anonymous 17.04.2018 / 15:53

0 answers