home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / LOCAL / sio.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  5KB  |  227 lines

  1. /*------------------------------------------------------------------*/
  2. /*    Sio.c:  Serial I/O for Amiga Version of PCMail            */
  3. /*    Created: 27Jun87 by J.A. Lydiatt                */
  4. /*                                    */
  5. /* Exports the following routines required by PCMail:            */
  6. /*  SIOInit():        Initialize Serial Port                */
  7. /*  SIOClose():        Close and free Serial Port resources        */
  8. /*  SIOSpeed():        Set bps                        */
  9. /*  SIORead():        read characters from serial port        */
  10. /*  SIOWrite():        write characters to serial port            */
  11. /*  SIOInBuffer():    Set Buffer size for read & writes.        */
  12. /*  SIOOutBuffer():    (Has no effect in Amiga Version)        */
  13. /*------------------------------------------------------------------*/
  14.  
  15. #include <stdio.h>
  16. #ifndef TRUE
  17. #define TRUE -1
  18. #define FALSE 0
  19. #endif
  20.  
  21. /*--- Timer Function Declarations --- */
  22.  
  23. extern void CloseTimer();    /* Return Timer's resources to the System */
  24. extern int  OpenTimer();     /* Returns True if Timer Device Opened OK */ 
  25. extern void StartTimer();    /* (long)seconds, (long)microseconds      */
  26. extern int  TimerExpired();     /* Returns True if Timer interval expired */
  27.  
  28. /*--- SerialIO Function Declarations --- */
  29.  
  30. extern struct EOExtSer *InitSerialIO(); /* problems? return NULL      */
  31. extern void CloseSerialIO();        /* Return SerialIO resources      */
  32. extern int  CheckSerIO();        /* TRUE if character available    */
  33. extern char SerIORead();        /* Returns a Char from serial port*/
  34. extern void SerIOWrite();         /* Writes one byte to serial port */ 
  35. extern void SetSerBaud();        /* (int)baud - set baud rate      */
  36.  
  37. /*--------------------------------------------------------------*/
  38. /*     getspeed/getbaud: check for valid baud parameters    */
  39. /*--------------------------------------------------------------*/
  40.  
  41. static struct {
  42.     unsigned baudr;
  43.     int speedcode;
  44.  
  45. } speeds[] = {
  46.     300,    300,
  47.     1200,    1200,
  48.     2400,    2400,
  49.     4800,    4800,
  50.     9600,    9600,
  51.     19200, 19200,
  52.     0,
  53. };
  54.  
  55. static unsigned getspeed(code)
  56. {
  57.     register n;
  58.  
  59.     for (n=0; speeds[n].baudr; ++n)
  60.         if (speeds[n].speedcode == code)
  61.             return speeds[n].baudr;
  62.     return 0;
  63. }
  64.  
  65.  
  66. static unsigned getbaud(code)
  67. char *code;
  68. {
  69.     register n;
  70.     register int Baudrate;
  71.     
  72.     Baudrate = atoi(code);
  73.     
  74.     for (n=0; speeds[n].baudr; ++n)
  75.         if (speeds[n].baudr == Baudrate)
  76.             return speeds[n].speedcode;
  77.     return 0;
  78. }
  79.  
  80. /*--------------------------------------------------------------*/
  81. /*    SIOInit: initialize serial I/O                */
  82. /*--------------------------------------------------------------*/
  83.  
  84.  
  85. SIOInit ( whichport, speed )
  86. char * whichport;
  87. char * speed;
  88. {
  89.     unsigned int baud;
  90.     extern void SetXonMode();
  91.  
  92.     /*  fprintf( stderr, "sioinit %s %s\n", whichport, speed ); /* */
  93.  
  94.     if ( InitSerialIO() == NULL ) {
  95.         fprintf( stderr, "Can't open Serial Port\n" );
  96.         return( -1 );
  97.     }
  98.  
  99.      if ( baud = getbaud( speed ) )
  100.     SetSerBaud( baud );
  101.  
  102.      if ( !OpenTimer() )
  103.     {
  104.        CloseSerialIO();
  105.        fprintf( stderr, "Can't open the Timer Device\n" );
  106.        return -1;
  107.         }
  108.  
  109.      SetXonMode( (int)FALSE );
  110.      return( 0 );
  111. }
  112.  
  113. /*--------------------------------------------------------------*/
  114. /*    SIOSpeed:  set the serial Baud rate             */
  115. /*--------------------------------------------------------------*/
  116.  
  117. SIOSpeed( speed )
  118. char *speed;
  119. {
  120.     unsigned int baud;
  121.  
  122.     if ( baud = getbaud( speed ) )
  123.     SetSerBaud( baud );
  124. }
  125.  
  126. /*--------------------------------------------------------------*/
  127. /*    SIOInBuffer/ SIOOutBuffer:    Set Buffer Size.    */
  128. /*--------------------------------------------------------------*/
  129.  
  130. SIOInBuffer ( buf, size )
  131. char * buf;
  132. int size;
  133. {
  134. }
  135.  
  136. SIOOutBuffer ( buf, size )
  137. char * buf;
  138. int size;
  139. {
  140. }
  141.  
  142. /*--------------------------------------------------------------*/
  143. /*    SIOClose:    Close Serial port.            */
  144. /*--------------------------------------------------------------*/
  145.  
  146. SIOClose ( dtr )
  147. {
  148.    CloseSerialIO();
  149.    CloseTimer();
  150. }
  151.  
  152. /*--------------------------------------------------------------*/
  153. /*    SIOWrite: send count characters to serial port        */
  154. /*--------------------------------------------------------------*/
  155.  
  156. SIOWrite ( buf, count )
  157. register char * buf;
  158. int count;
  159. {
  160.     register int nsent;
  161.  
  162.     nsent = count;
  163.     while ( nsent-- )
  164.        SerIOWrite( *buf++ );
  165.  
  166.     return count;
  167. }
  168.  
  169. /*--------------------------------------------------------------*/
  170. /*    SIORead: read mincount <= #characters <= maxcount    */
  171. /*    return  # of characters read, or -1 if timed out after    */
  172. /*    after tenths / 10 seconds.                */
  173. /*--------------------------------------------------------------*/
  174.  
  175.  
  176. int SIORead ( buf, mincount, maxcount, tenths )
  177. register char *buf;
  178. int    mincount;
  179. int    maxcount;
  180. int    tenths; /* timeout is in tenth's of seconds */
  181. {
  182.         /* A bit of a Kludge to get the project started */
  183.  
  184.     long seconds, microSeconds;
  185.     unsigned long waitMask;
  186.     int  timerON ;
  187.     register int received;
  188.  
  189.     /* fprintf (stderr, "Read  m= %d T= %ld", maxcount, Ticks  );/**/
  190.  
  191.  
  192.  
  193.     if ( tenths == 0 )
  194.        {
  195.         if ( !CheckSerIO() )
  196.            return -1;
  197.         tenths = 10;
  198.        }
  199.         seconds = tenths / 10;
  200.     microSeconds = ((long)(tenths) - seconds * 10L) * 100000L;
  201.         
  202.     received = 0;
  203.     waitMask = (1L << GetSerialSigBit()) | (1L << GetTimerSigBit() ); 
  204.     timerON = FALSE;
  205.     do
  206.       {
  207.          if ( CheckSerIO() )
  208.             {
  209.                 *buf = SerIORead();
  210.            ++buf;
  211.            ++received;
  212.            if ( mincount <= received && received <= maxcount )
  213.               return received;
  214.         }
  215.          if ( !timerON )
  216.         {
  217.            StartTimer( seconds, microSeconds );
  218.            timerON = TRUE;
  219.         }
  220.          Wait( waitMask );
  221.  
  222.       }    while ( !TimerExpired() );
  223.  
  224.     return -1;
  225. }
  226.  
  227.