home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / ultrix / 6926 < prev    next >
Encoding:
Text File  |  1992-09-15  |  5.1 KB  |  149 lines

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