ts-7000
[Top] [All Lists]

[ts-7000] Re: No SIGIO on com1?

To:
Subject: [ts-7000] Re: No SIGIO on com1?
From: "papp_lapp" <>
Date: Sat, 29 Mar 2008 09:57:29 -0000
> Can you post the code you are trying to use? 

Sure thing. There are some unused variables left from the howto
source. I don't see a way to attach files via the yahoo interface, so
I'll paste it here:

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/time.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
char devicename[80];
long Baud_Rate = 38400;         // default Baud Rate (110 through 38400)
long BAUD;                      // derived baud rate from command line
long DATABITS;
long STOPBITS;
long PARITYON;
long PARITY;
int Data_Bits = 8;              // Number of data bits
int Stop_Bits = 1;              // Number of stop bits
int Parity = 0;                 // No Parity
int Format = 4;
FILE *input;
FILE *output;
int status;

main(int Parm_Count, char *Parms[])
{
  char message[90];
  char devicename[11] = "/dev/ttyAM0";
  int fd, tty, c, res, i, error;
  char In1, Key;
  struct termios oldtio, newtio;       //place for old and new port
settings for serial port
  struct termios oldkey, newkey;       //place tor old and new port
settings for keyboard teletype
  struct sigaction saio;               //definition of signal action
  struct sigaction query_saio;
  char buf[255];                       //buffer for where data is put

  input = fopen("/dev/tty", "r");      //open the terminal keyboard
  output = fopen("/dev/tty", "w");     //open the terminal screen

  if (!input || !output)
    {
      fprintf(stderr, "Unable to open /dev/tty\n");
      exit(1);
    }

  tty = open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK);
  //set the user console port up
  tcgetattr(tty,&oldkey); // save current port settings
  //so commands are interpreted right for this program

  // set new port settings for non-canonical input processing  //must
be NOCTTY
  newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  newkey.c_iflag = IGNPAR;
  newkey.c_oflag = 0;
  newkey.c_lflag = 0;       //ICANON;
  newkey.c_cc[VMIN]=1;
  newkey.c_cc[VTIME]=0;
  tcflush(tty, TCIFLUSH);
  tcsetattr(tty,TCSANOW,&newkey);

  BAUD = B38400;
  DATABITS = CS8;
  STOPBITS = 0;
  PARITYON = 0;
  PARITY = 0;

  //open the device(com port) to be non-blocking (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_flags = 0;
  saio.sa_restorer = NULL;
  saio.sa_sigaction = NULL;
  if(sigaction(SIGIO,&saio,NULL)<0)
    printf("Error on sigaction\n");
  sigaction(SIGIO,NULL,&query_saio);
  if(saio.sa_handler != query_saio.sa_handler)
    printf("Error setting SIGIO!\n");


  // 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, O_NONBLOCK, FASYNC);
  fcntl(fd, F_SETFL, O_NONBLOCK);

  tcgetattr(fd,&oldtio); // save current port settings
  // set new port settings for canonical input processing
  newtio.c_cflag = BAUD | DATABITS | CRTSCTS | STOPBITS | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
  newtio.c_lflag = 0;       //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. ");
      status = fread(&Key,1,1,input);

      if (status==1)  //if a key was hit
        {
          switch (Key)
            { /* branch to appropiate key handler */
            case 0x1b: /* Esc */
              STOP=TRUE;
              break;
            default:
              printf("%c",Key);
              write(fd,&Key,1);          //write 1 byte to the port
              break;
            }  //end of switch key

        }  //end if a key was hi
      // 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,255);

          if (res>0)
            {

              for (i=0; i<res; i++)  //for all chars in string
                {
                  In1 = buf[i];

                  sprintf(message,"%c",In1);
                  fputs(message,output);
                }  //end of for all chars in string
              printf("\n");
            }  //end if res>0
          wait_flag = TRUE;      /* wait for new input */
        }  //end if wait flag == FALSE
    }  //while stop==FALSE
  res = read(fd,buf,255);
          if (res>0)
            printf("%s",buf);
  // restore old port settings
  tcsetattr(fd,TCSANOW,&oldtio);
  tcsetattr(tty,TCSANOW,&oldkey);
  close(tty);
  close(fd);        //close the com port

}  //end of main

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




------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ts-7000/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ts-7000/join
    (Yahoo! ID required)

<*> To change settings via email:
     
    

<*> To unsubscribe from this group, send an email to:
    

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/

<Prev in Thread] Current Thread [Next in Thread>
Admin

Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU