From: gdrew@cs.umd.edu (Greg Drew)
Newsgroups: comp.unix.questions
Subject: Serial port on a DECstation
Date: 14 Sep 92 20:24:09 GMT
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742

I have a program which has to use the serial port on a DECstation to pass
data in and out.  It has to run at 9600baud, and unfortunatly, I cannot
manage to open the port at faster than 300baud, which is the system default.
I've included the code I use to open the port and set the parameters.  To
get the data, i use the read call, and read a single character at a time.
Could someone tell me what I am doing wrong, and how to get the parameters
to take effect.  Thanks (If this is a FAQ, could someone send me a pointer
to the FAQ?).
					-- GDD

/************************************************************************/
/************************************************************************/
/* this should be set to read from the serial port.  The default is to  */
/*     read input from the console.                                     */
#ifndef TTYINPUT
#define SERIALINPUT
#endif
/* Specify the /dev/tty name of the serial port to use.                 */
#define PORT "/dev/tty00"
/* Set this for verbose command output.  It is very slow!!! */
/*#define VERBOSE*/

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/file.h>
#include <limits.h>

struct termios *original,*new;
int inpt;

/************************************************************************/
/* InitIO determins the fd of stdin, saves the current terminal 	*/
/*    settings, and then sets the terminal to do unbuffered IO.		*/
/* Greg Drew                                                            */
/* 9/3/91								*/
/************************************************************************/
InitIO()
{
#ifdef SERIALINPUT
  /* Open the serial port */
  inpt=open(PORT,O_RDWR|O_NOCTTY|O_NONBLOCK);
#else
  /* find the fd of the terminal */
  inpt=fileno(stdin);
#endif
  printf("the input file designator %d\n",inpt);
  /* allocate space for 2 terminal invironments */
  original = (struct termios *)malloc(sizeof(struct termios));
  new = (struct termios *)malloc(sizeof(struct termios));
  /* save the current environment */
  /* ioctl(inpt,TCGETS,original);*/
  tcgetattr(inpt,original);
  /* get a copy of the current environment to modify */
  /*ioctl(inpt,TCGETS,new);*/
  printf("getattr returns %d \n",tcgetattr(inpt,new));
  /* turn canonical (line) mode input processing off */
  new->c_lflag=new->c_lflag & ~ICANON | CLOCAL;
  /* set the minimum number of characters to read to 0 */
  new->c_cc[VMIN]=0;
  /* set no time to wait for a character */
  new->c_cc[VTIME]=0;
  /* set the baud rate on the line */
  printf("cfsetispeed returns %d\n",cfsetispeed(new,(unsigned long)B9600));
  printf("cfsetospeed returns %d\n",cfsetospeed(new,(unsigned long)B9600));
  /* enable the new termio values */
  /*ioctl(inpt,TCSETS,new);*/
  printf("setattr returns %d \n",tcsetattr(inpt,TCSANOW,new));
  printf("cfgetispeed returns %lu\n",cfgetospeed(new));
}/* end of InitIO */



void ResetIO()
/************************************************************************/
/* ResetIO returns the terminal to the parameters that were in force    */
/*    before the last call to InitIO.					*/
/* Greg Drew                                                            */
/* 9/3/91								*/
/************************************************************************/
{
  /* reset the stored terminal parameters */
  /*ioctl(inpt,TCSETS,original);*/
  tcsetattr(inpt,TCSANOW,original);
}/* end of ResetIO */



/************************************************************************/
/* PrintInfo: used to print messages to the screen.  It is used instead */
/*    of printf so that this output can be easliy turned off		*/
/* Greg Drew								*/
/* 9/25/91								*/
/************************************************************************/
PrintInfo(str)
char *str; /* the string to print */
{
#ifdef VERBOSE
  printf("%s\n",str);
#endif
}


main()
{
 char ch;
 int numRead = 0;

 InitIO();
 numRead = read(inpt,&ch,1);
 while (1)
  {/* a charactar was read */
   if(numRead)
    {printf("read: %d  Char: %c \n",numRead,ch);
    }/*endif*/
   numRead = read(inpt,&ch,1);
  }
  ResetIO();
}
-- 
----------------------------------------------------------------------------
Greg Drew                 | N3MXX 
gdrew@trellis.cs.umd.edu  |
----------------------------------------------------------------------------
Newsgroups: comp.unix.questions