home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / sun / apps / 2896 < prev    next >
Encoding:
Text File  |  1993-01-06  |  6.8 KB  |  195 lines

  1. Xref: sparky comp.sys.sun.apps:2896 comp.sys.sun.hardware:6655 comp.sys.sun.misc:6144 comp.sys.sun.admin:9971
  2. Newsgroups: comp.sys.sun.apps,comp.sys.sun.hardware,comp.sys.sun.misc,comp.sys.sun.admin
  3. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!sdd.hp.com!network.ucsd.edu!scubed!scatter@scubed.com
  4. From: scatter@scubed.com (McLaughlin)
  5. Subject: ioctl() on the SPARC 
  6. Message-ID: <1993Jan6.021151.7243@scubed.com>
  7. Followup-To: comp.sys.sun.misc
  8. Keywords: ioctl termio 
  9. Sender: usenet@scubed.com (USENET News System)
  10. Nntp-Posting-Host: vulcan
  11. Reply-To: scatter@scubed.com
  12. Organization: S-CUBED Division of Maxwell Laboratories
  13. Date: Wed, 6 Jan 1993 02:11:51 GMT
  14. Lines: 179
  15.  
  16. I am controling a device over one of an RS-232 line on an IPC.
  17. I am having difficulties.
  18.  
  19. My problem is that ioctl() does not seem to be doing what the man
  20. page(s) advertise.  Of coarse, it is a good possibility that I am 
  21. screwed up and the problem is pilot error on my part.  If there is
  22. some lore and wisdom out there in netland, I would gladly like to
  23. find out now before I spend more time beating my head against the
  24. wall.  I have a version of the code working on an SGI, so I know that
  25. the "Non-Canonical Mode Input Processing" works on that machine.
  26.  
  27. 1) Am I using ioctl() incorrectly?
  28. 2) Is ioctl() broken under SUNOS?
  29. 3) Will my code be broken again when we go to SOLARIS?
  30. 4) Was termio(4) and ioctl invented for some programmer's job security?
  31. 5) Am I going to have to write my own interupts?
  32.  
  33. The root of my problem is that according to the termio(4) man page I should be
  34. able to use "Non-Canonical Mode Input Processing" and set MIN and TIME.
  35. I am interested in Case C: MIN = 0, TIME > 0 and  Case D: MIN = 0, TIME = 0.
  36. The termio(4) man page says:
  37.  
  38. ".....
  39.   Case C: MIN = 0, TIME > 0
  40.      In this case, since MIN = 0, TIME no  longer  represents  an
  41.      intercharacter timer.  It now serves as a read timer that is
  42.      activated as soon as a read() is done.  A read is  satisfied
  43.      as  soon as a single character is received or the read timer
  44.      expires.  Note: in this case if the timer expires, no  char-
  45.      acter  will  be returned.  If the timer does not expire, the
  46.      only way the read can be satisfied  is  if  a  character  is
  47.      received.  In this case the read will not block indefinitely
  48.      waiting for a character - if no character is received within
  49.      TIME*.10  seconds after the read is initiated, the read will
  50.      return with zero characters.
  51.  
  52.   Case D: MIN = 0, TIME = 0
  53.      In this case return is immediate.  The minimum of either the
  54.      number  of  characters requested or the number of characters
  55.      currently available will be  returned  without  waiting  for
  56.      more characters to be input.
  57. ....."
  58.  
  59. My problem is that no matter how long I wait, whether MIN=0 or 1, my read() 
  60. never returns if the external device has not transmitted a character.
  61. It does not matter whether TIME = 0 or 1.  It is working as if TIME = INFINITY.
  62.  
  63. My routine, lvr_init(), for opening the port is listed below.  
  64.  
  65. /* a whole bunch of includes */
  66. #include <stdio.h>
  67. #include <string.h>
  68. #include <termios.h>
  69. #include <sgtty.h>
  70. #include <fcntl.h>
  71. #include <errno.h>
  72. /* Global Static Variables */
  73. static struct termios new_port_d2;
  74. static struct termios old_port_d2;
  75. static struct sgttyb old_port, new_port;
  76. static struct tchars new_tchars, old_tchars;
  77. static struct ltchars new_lchars, old_lchars;
  78. /* the pointer to the LVR tyy port */
  79. static int fdes;
  80. /* termio and sgtty stuff */
  81. int ldisc = NTTYDISC;
  82. int local_mode = 0;
  83.  
  84. int lvr_init (tty)  /* initialize communications with lvr */
  85.      char * tty;    /* device name such as /dev/ttyb */
  86. {
  87.   extern int errno;
  88.   int i;
  89.   int ioctl_err;
  90.  
  91.   /* opening port */
  92.   if ((fdes = open (tty, O_RDWR | O_NDELAY | O_NOCTTY )) == -1) {
  93.       fprintf (stderr, "lvr_init error: cannot open tty port %s.\n", tty);
  94.       return (-1);
  95.     }
  96.  
  97.   /* get termio struct for device */
  98.   if ((ioctl_err = ioctl (fdes, TCGETS, &old_port_d2)) == -1) {
  99.       fprintf (stderr, "lvr_init error: old termio struct. fdes =%d\n", fdes);
  100.       close (fdes);
  101.       return (-1);
  102.     }
  103.  
  104.   /* initialize new termio struct */
  105.   new_port_d2.c_iflag = IGNPAR;    /* input flag */
  106.   new_port_d2.c_oflag = 0;    /* output flag */
  107.   new_port_d2.c_cflag = B9600 | CS8 | CLOCAL;    /* 9600 Baud and 8 bits */
  108.   new_port_d2.c_lflag = 0;    /* local modes */
  109.   new_port_d2.c_line = old_port_d2.c_line;
  110.   for (i = 0; i < NCCS; i++)
  111.     new_port_d2.c_cc[i] = old_port_d2.c_cc[i];
  112.  
  113.   /* required for non-cononical input mode processing */
  114.   /* VMIN is minimum number of characters for the read to satisfy */
  115.   /* VTIME is in units of 0.10 sec */
  116.   /* setting VEOF or VMIN */
  117.   new_port_d2.c_cc[4] = 0;
  118.   /* setting VEOL or VTIME */
  119.   /* this does not work,...... */
  120.   new_port_d2.c_cc[5] = 1;
  121.   /* this sort of works but is not what I want........ */
  122.   new_port_d2.c_cc[5] = 0;
  123.  
  124.   if ((ioctl_err = ioctl (fdes, TCSETS, &new_port_d2)) == -1) {
  125.       fprintf (stderr, "lvr_init: new termio struct. fdes =%d\n", fdes);
  126.       close (fdes);
  127.       return (-1);
  128.     }
  129.  
  130.   /* Get current SGTTYB struct for /dev/tty* */
  131.   if ((ioctl_err = ioctl (fdes, TIOCGETP, &old_port)) == -1) {
  132.       fprintf (stderr, "LVR_IOCTL_SGTTY\n");
  133.       return (-1);
  134.     }
  135.  
  136.   /* Get current TCHARS struct for /dev/tty* */
  137.   if ((ioctl_err = ioctl (fdes, TIOCGETC, &old_tchars)) == -1) {
  138.       fprintf (stderr, "LVR_IOCTL_TCHARS\n");
  139.       return (-1);
  140.     }
  141.  
  142.   /* Initialize new SGTTYB struct */
  143.   new_port.sg_ispeed = B9600;    /* 9600 baud input */
  144.   new_port.sg_ospeed = B9600;    /* 9600 baud output */
  145.   new_port.sg_flags = RAW;    /* Raw mode */
  146.  
  147.   if ((ioctl_err = ioctl (fdes, TIOCSETP, &new_port)) == -1) {
  148.       fprintf(stderr, "lvr_init: setting sgtty\n");
  149.       close (fdes);
  150.       return (-1);
  151.     }
  152.  
  153.   /* Initialize new line discipline */
  154.   if ((ioctl_err = ioctl (fdes, TIOCSETD, &ldisc)) == -1) {
  155.       fprintf(stderr, "lvr_init: ldisc\n");
  156.       close (fdes);
  157.       return (-1);
  158.     }
  159.  
  160.   /* Set special characters */
  161.   if ((ioctl_err = ioctl (fdes, TIOCSETC, &new_tchars)) == -1) {
  162.       fprintf(stderr, "lvr_init: spec chars\n");
  163.       close (fdes);
  164.       return (-1);
  165.     }
  166.  
  167.   /* Set local modes */
  168.   if ((ioctl_err = ioctl (fdes, TIOCLBIS, &local_mode)) == -1) {
  169.       fprintf(stderr, "lvr_init: local mode\n");
  170.       close (fdes);
  171.       return (-1);
  172.     }
  173.  
  174.   if ((ioctl_err = ioctl (fdes, TIOCSLTC, &new_lchars)) == -1) {
  175.       fprintf(stderr, "lvr_init: local spec\n");
  176.       close (fdes);
  177.       return (-1);
  178.     }
  179.  
  180.   /* Flush input and output buffers just for drill */
  181.   if ((ioctl_err = ioctl (fdes, TIOCFLUSH, 0)) == -1) {
  182.       fprintf(stderr, "lvr_init: FLUSH_PORT\n");
  183.       return (-1);
  184.     }
  185.  
  186.   return (0);
  187. }                /* end lvr_init */
  188.  
  189.  
  190. -- 
  191. Keith L. McLaughlin (scatter)
  192. e-mail: scatter@vulcan.scubed.com, scatter@seismo.CSS.GOV
  193. phone: 619-587-8436, 619-453-0060, FAX 619-755-0474
  194. snail-mail: S-CUBED, Div. Maxwell Laboratories, POB 1620, La Jolla, CA 92038
  195.