Működő példaprogram, nyers - azaz nem terminál emulációs - adatforgalomra.

A program kiír egy byte-ot a portra majd olvas egyet onnan. Ha nem érkezik adat, akkor blokkolva marad, ez esetben CTRL-C-vel szakítható meg a futása.

Vissza


/*
 * RS232C mintaprogram 
 * 
 * fordítás: gcc -Wall -o rs232_skeleton rs232_skeleton.c
 * 
 * használat: ./rs232_skeleton port_neve
 * például    ./rs232_skeleton /dev/ttyS0
 * 
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define EINTR_RETRY(expression) \
    ({ \
        long int _result; \
        do _result = (long int) (expression); \
        while (_result == -1L && errno == EINTR); \
        _result; \
    })

int
open_port (char *port_name)
{
  int fd;

  fd = EINTR_RETRY (open (port_name, O_RDWR | O_NOCTTY | O_NDELAY));
  if (fd == -1)
    {
      perror ("open_port: Unable to open port");
    }
  else
    fcntl (fd, F_SETFL, 0);

  return (fd);
}

int
close_port (int fd)
{
  int ret_val;

  ret_val = EINTR_RETRY (close (fd));

  return (ret_val);
}

int
write_byte (int fd, unsigned char out_byte)
{
  int ret_val;
  int bytes_out;

  bytes_out = EINTR_RETRY (write (fd, &out_byte, 1));

  ret_val = 0;
  if (bytes_out != 1)
    ret_val = -1;

  return (ret_val);
}

int
read_byte (int fd, unsigned char *in_byte)
{
  int ret_val;
  int bytes_in;

  bytes_in = EINTR_RETRY (read (fd, in_byte, 1));

  ret_val = 0;
  if (bytes_in != 1)
    ret_val = -1;

  return (ret_val);
}

int
main (int argc, char *argv[])
{
  int fd;
  int ret_val;
  struct termios my_options;
  unsigned char byte0;


  if (argc < 2)
    {
      fprintf (stderr, "Error: missing device name argument!\n");
      exit (EXIT_FAILURE);
    }

  printf ("Port: %s\n", argv[1]);

  fd = open_port (argv[1]);
  if (fd < 0)
    {
      fprintf (stderr, "Error: opening device %s!\n", argv[1]);
      exit (EXIT_FAILURE);
    }

  tcgetattr (fd, &my_options);

  // 9600 Baud
  cfsetispeed (&my_options, B9600);
  cfsetospeed (&my_options, B9600);

  // 8 bit, paritás letiltva (8N1)
  my_options.c_cflag &= ~PARENB;
  my_options.c_cflag &= ~CSTOPB;
  my_options.c_cflag &= ~CSIZE;
  my_options.c_cflag |= CS8;

  // Hardware handshake letiltás
  my_options.c_cflag &= ~CRTSCTS;

  // Olvasás engedélyezés
  my_options.c_cflag |= (CLOCAL | CREAD);

  // Nyers adatfolyam kiválasztása
  my_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

  // Nyers adatfolyam kiválasztása
  my_options.c_oflag &= ~OPOST;

  tcsetattr (fd, TCSANOW, &my_options);


  tcflush (fd, TCIOFLUSH);

  ret_val = write_byte (fd, '\x00');
  if (ret_val < 0)
    {
      fprintf (stderr, "Error: writing device %s!\n", argv[1]);
      exit (EXIT_FAILURE);
    }

  ret_val = read_byte (fd, &byte0);
  if (ret_val < 0)
    {
      fprintf (stderr, "Error: reading device %s!\n", argv[1]);
      exit (EXIT_FAILURE);
    }

  ret_val = close_port (fd);
  if (ret_val < 0)
    {
      fprintf (stderr, "Error: closing device %s!\n", argv[1]);
      exit (EXIT_FAILURE);
    }

  return (0);
}