home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 11057 < prev    next >
Encoding:
Text File  |  1992-09-14  |  4.5 KB  |  135 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!ra!mimsy!gdrew
  2. From: gdrew@cs.umd.edu (Greg Drew)
  3. Newsgroups: comp.unix.questions
  4. Subject: Serial port on a DECstation
  5. Message-ID: <60307@mimsy.umd.edu>
  6. Date: 14 Sep 92 20:24:09 GMT
  7. Sender: news@mimsy.umd.edu
  8. Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
  9. Lines: 124
  10.  
  11. I have a program which has to use the serial port on a DECstation to pass
  12. data in and out.  It has to run at 9600baud, and unfortunatly, I cannot
  13. manage to open the port at faster than 300baud, which is the system default.
  14. I've included the code I use to open the port and set the parameters.  To
  15. get the data, i use the read call, and read a single character at a time.
  16. Could someone tell me what I am doing wrong, and how to get the parameters
  17. to take effect.  Thanks (If this is a FAQ, could someone send me a pointer
  18. to the FAQ?).
  19.                     -- GDD
  20.  
  21. /************************************************************************/
  22. /************************************************************************/
  23. /* this should be set to read from the serial port.  The default is to  */
  24. /*     read input from the console.                                     */
  25. #ifndef TTYINPUT
  26. #define SERIALINPUT
  27. #endif
  28. /* Specify the /dev/tty name of the serial port to use.                 */
  29. #define PORT "/dev/tty00"
  30. /* Set this for verbose command output.  It is very slow!!! */
  31. /*#define VERBOSE*/
  32.  
  33. #include <stdio.h>
  34. #include <termios.h>
  35. #include <unistd.h>
  36. #include <sys/file.h>
  37. #include <limits.h>
  38.  
  39. struct termios *original,*new;
  40. int inpt;
  41.  
  42. /************************************************************************/
  43. /* InitIO determins the fd of stdin, saves the current terminal     */
  44. /*    settings, and then sets the terminal to do unbuffered IO.        */
  45. /* Greg Drew                                                            */
  46. /* 9/3/91                                */
  47. /************************************************************************/
  48. InitIO()
  49. {
  50. #ifdef SERIALINPUT
  51.   /* Open the serial port */
  52.   inpt=open(PORT,O_RDWR|O_NOCTTY|O_NONBLOCK);
  53. #else
  54.   /* find the fd of the terminal */
  55.   inpt=fileno(stdin);
  56. #endif
  57.   printf("the input file designator %d\n",inpt);
  58.   /* allocate space for 2 terminal invironments */
  59.   original = (struct termios *)malloc(sizeof(struct termios));
  60.   new = (struct termios *)malloc(sizeof(struct termios));
  61.   /* save the current environment */
  62.   /* ioctl(inpt,TCGETS,original);*/
  63.   tcgetattr(inpt,original);
  64.   /* get a copy of the current environment to modify */
  65.   /*ioctl(inpt,TCGETS,new);*/
  66.   printf("getattr returns %d \n",tcgetattr(inpt,new));
  67.   /* turn canonical (line) mode input processing off */
  68.   new->c_lflag=new->c_lflag & ~ICANON | CLOCAL;
  69.   /* set the minimum number of characters to read to 0 */
  70.   new->c_cc[VMIN]=0;
  71.   /* set no time to wait for a character */
  72.   new->c_cc[VTIME]=0;
  73.   /* set the baud rate on the line */
  74.   printf("cfsetispeed returns %d\n",cfsetispeed(new,(unsigned long)B9600));
  75.   printf("cfsetospeed returns %d\n",cfsetospeed(new,(unsigned long)B9600));
  76.   /* enable the new termio values */
  77.   /*ioctl(inpt,TCSETS,new);*/
  78.   printf("setattr returns %d \n",tcsetattr(inpt,TCSANOW,new));
  79.   printf("cfgetispeed returns %lu\n",cfgetospeed(new));
  80. }/* end of InitIO */
  81.  
  82.  
  83.  
  84. void ResetIO()
  85. /************************************************************************/
  86. /* ResetIO returns the terminal to the parameters that were in force    */
  87. /*    before the last call to InitIO.                    */
  88. /* Greg Drew                                                            */
  89. /* 9/3/91                                */
  90. /************************************************************************/
  91. {
  92.   /* reset the stored terminal parameters */
  93.   /*ioctl(inpt,TCSETS,original);*/
  94.   tcsetattr(inpt,TCSANOW,original);
  95. }/* end of ResetIO */
  96.  
  97.  
  98.  
  99. /************************************************************************/
  100. /* PrintInfo: used to print messages to the screen.  It is used instead */
  101. /*    of printf so that this output can be easliy turned off        */
  102. /* Greg Drew                                */
  103. /* 9/25/91                                */
  104. /************************************************************************/
  105. PrintInfo(str)
  106. char *str; /* the string to print */
  107. {
  108. #ifdef VERBOSE
  109.   printf("%s\n",str);
  110. #endif
  111. }
  112.  
  113.  
  114. main()
  115. {
  116.  char ch;
  117.  int numRead = 0;
  118.  
  119.  InitIO();
  120.  numRead = read(inpt,&ch,1);
  121.  while (1)
  122.   {/* a charactar was read */
  123.    if(numRead)
  124.     {printf("read: %d  Char: %c \n",numRead,ch);
  125.     }/*endif*/
  126.    numRead = read(inpt,&ch,1);
  127.   }
  128.   ResetIO();
  129. }
  130. -- 
  131. ----------------------------------------------------------------------------
  132. Greg Drew                 | N3MXX 
  133. gdrew@trellis.cs.umd.edu  |
  134. ----------------------------------------------------------------------------
  135.