home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8708 / 24 / sio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  7.0 KB  |  440 lines

  1. /*            sio.c
  2.  
  3.  
  4.                 Serial I/O for rz/sz
  5.  
  6.  
  7.                 Copyright (c) 1986 Stuart Lynne
  8.  
  9.                 June 1986
  10.  
  11.                 
  12.  
  13. */
  14. #include <local/sio.h>
  15. #include <stdio.h>
  16.  
  17. #include <pb.h>
  18.  
  19. #include <osutil.h>
  20.  
  21. /* #include <max/macglobals.h> */
  22. #include <max/debug.h>
  23. #include <max/asc.h>
  24.  
  25. #define FALSE 0
  26. #ifndef TRUE
  27. #define TRUE 1
  28. #endif
  29.  
  30.  
  31. struct SIOPort curPort;
  32.  
  33.  
  34. struct {
  35.     unsigned baudr;
  36.     int speedcode;
  37. } speeds[] = {
  38.     300,    baud300,
  39.     600,    baud600,
  40.     1200,    baud1200,
  41.     2400,    baud2400,
  42.     4800,    baud4800,
  43.     9600,    baud9600,
  44.     19200,    baud19200,
  45. /*
  46.     38400,     baud38400,
  47. */
  48.     57600,     baud57600,
  49.     9600,    baud9600,
  50.     0,
  51. };
  52.  
  53. static unsigned
  54. getspeed(code)
  55. {
  56.     register n;
  57.  
  58.     for (n=0; speeds[n].baudr; ++n)
  59.         if (speeds[n].speedcode == code)
  60.             return speeds[n].baudr;
  61.     return 0;
  62. }
  63.  
  64.  
  65. static unsigned
  66. 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.  
  82. SIOInit ( whichport, speed )
  83. char * whichport;
  84. char * speed;
  85. {
  86.     char i;
  87.     SerShk    handshake;
  88.     int setting;
  89.     int err;
  90.  
  91.     /*  fprintf( stderr, "sioinit %s %s\n", whichport, speed ); /* */
  92.     
  93.     if ( 
  94.         strncmp( whichport, ".a", 2 ) == 0 || 
  95.         strncmp( whichport, "a",1 ) == 0 ||
  96.         strcmp( whichport, "modem") == 0
  97.         ) 
  98.     {
  99.         curPort.in = AIN;
  100.         curPort.out = AOUT;
  101.     }
  102.     else if (
  103.         strncmp( whichport, ".b", 2 ) == 0 || 
  104.         strncmp( whichport, "b", 1 ) == 0 ||
  105.         strcmp( whichport, "printer") == 0 
  106.         ) 
  107.     {
  108.         curPort.in = BIN;
  109.         curPort.out = BOUT;
  110.     }
  111.     else
  112.         return( -1 );
  113.  
  114.     if ((err = OpenDriver ( curPort.in,  &curPort.refin )) !=0 ) {
  115.         fprintf( stderr, "Err: %d\n", err);
  116.         debugMsg ( ctop( curPort.in ) );
  117.         SysBeep (20); 
  118.         return( -1 );
  119.     }
  120.  
  121.     
  122.     if ((err=OpenDriver ( curPort.out, &curPort.refout )) !=0 ) {
  123.         fprintf( stderr, "Err: %d\n", err);
  124.         debugMsg ( ctop( curPort.out ) );
  125.         SysBeep (20); 
  126.         return( -1 );
  127.     }
  128.     
  129.     SIOHandshake( FALSE, FALSE, FALSE, XON, XOF );
  130.     SIOSettings( speed, noParity, stop10, data8 );
  131.     return( 0 );
  132. }
  133.  
  134. SIOSpeed( speed )
  135. char * speed;
  136. {
  137.     SIOSetting( speed, curPort.parity, curPort.stopbits, curPort.databits );
  138. }
  139.  
  140. SIOHandshake ( fInx, fXOn, fCTS, xOn, xOff )
  141. {    
  142.     int err;
  143.     
  144.     curPort.handshake.fInX = fInx;
  145.     curPort.handshake.fXOn = fXOn;
  146.     curPort.handshake.fCTS = fCTS;
  147.  
  148.     curPort.handshake.xOn = xOn;
  149.     curPort.handshake.xOff = xOff;
  150.     /* handshake.fXOn = TRUE; */
  151.     
  152.     curPort.handshake.errs = 0;
  153.     curPort.handshake.evts = 0;
  154.  
  155.     if ((err=SerHShake (curPort.refin, &curPort.handshake ))!=0 ) {
  156.         fprintf( stderr, "Err: %d\n", err);
  157.         debugMsg ("\PSerHShake error");
  158.         SysBeep (20); 
  159.     }
  160. }
  161.  
  162. SIOSetting( speed, parity, stopbits, databits )
  163. char * speed;
  164. {
  165.     int err;
  166.     int setting;
  167.     
  168.     curPort.baud = getbaud(speed);
  169.     curPort.parity = parity;
  170.     curPort.stopbits = stopbits;
  171.     curPort.databits = databits;
  172.     
  173.     setting = curPort.baud + parity + stopbits + databits;
  174.  
  175.     if ((err=SerReset(curPort.refin, setting))!=0 )  {
  176.         fprintf( stderr, "Err: %d\n", err);
  177.         debugMsg ("\PSerReset error");
  178.         SysBeep (20);
  179.     }    
  180. }
  181.  
  182. SIOInBuffer ( buf, size )
  183. char * buf;
  184. int size;
  185. {
  186.     curPort.inbuffer = buf;
  187.     curPort.insize = size;
  188.     SerSetBuf( curPort.refin, buf, size ); 
  189. }
  190.  
  191. SIOOutBuffer ( buf, size )
  192. char * buf;
  193. int size;
  194. {
  195.     curPort.outbuffer = buf;
  196.     curPort.outsize = size;
  197.     SerSetBuf( curPort.refout, buf, size ); 
  198. }
  199.  
  200.  
  201. SIOClose ( dtr )
  202. {
  203.     struct ParamBlkRec SIOpb;
  204.     
  205.     register Ptr *sp;
  206.     long count;
  207.  
  208.     if ( dtr != 0) {
  209.         /* drop DTR */
  210.         SIOpb.ioCompletion = NULL;
  211.         SIOpb.u.cp.csRefNum = curPort.refin;
  212.         SIOpb.u.cp.csCode = 18;
  213.         sp = (Ptr) &SIOpb;
  214.         
  215.         /**/ PBControl (&SIOpb, FALSE); /**/
  216. /*
  217. #asm
  218.             move.l    a3,a0
  219.             dc.w    $a004
  220. #endasm
  221. */
  222.     }
  223.     
  224.     SIOInBuffer( NULL, 0 );
  225.     SIOOutBuffer( NULL, 0 );
  226.     CloseDriver( curPort.refin );
  227.     CloseDriver( curPort.refout );
  228. }
  229.  
  230.  
  231. sfflushout ()
  232. {
  233.     }
  234.  
  235. SIOPurge ()
  236. {
  237.     char        ch;
  238.  
  239.     while (sread (&ch, 1, 1) == 1);
  240.     
  241.     }
  242.  
  243. SIOPutchar ( ch )
  244. char ch;
  245. {
  246.     /*fprintf(stderr, "<%02x>", ch);*/
  247.     return( putu ( &ch, 1, curPort.refout ) );    
  248. }
  249.  
  250. SIOWrite ( buf, count )
  251. char * buf;
  252. int count;
  253. {
  254.     return( putu (buf, count, curPort.refout) );
  255. }
  256.  
  257. SIOWStr ( st )
  258. char * st;
  259. {
  260.     return( putu (st, strlen(st), curPort.refout) );
  261. }
  262.  
  263. #define    Ticks            (*(long *)            0x16a)
  264.     
  265.  
  266. /* timeout is in tenths of a second */
  267. int SIORead ( byt, mincount, maxcount, tenths )
  268. char * byt;
  269. int    mincount;
  270. int     maxcount;
  271. int tenths;
  272. {
  273.     long    timea;
  274.     short    actCount;
  275.  
  276.  
  277.     int        i;
  278.     
  279.     timea = Ticks;
  280.     tenths *= 6;
  281.     /* fprintf (stderr, "Read  m= %d T= %ld", maxcount, Ticks  );/**/
  282.     do {
  283.         actCount = getn (byt, mincount, maxcount, curPort.refin );
  284.  
  285.         if (actCount > 0) {
  286.             /* fprintf (stderr, "  OK: %d\n", actCount);/**/
  287.             return actCount;
  288.             }
  289.         
  290.     } while ((long)(Ticks - timea) < (long) tenths);
  291.     
  292.     /* fprintf (stderr, "  Timed out: t= %d\n  T:%ld", tenths, Ticks );/**/
  293.     return -1;
  294.     }
  295.  
  296. srdchk ()
  297. {
  298.     struct ParamBlkRec SIOpb;
  299.     
  300.     register Ptr *sp;
  301.     long count;
  302.     
  303.     SIOpb.ioCompletion = NULL;
  304.     SIOpb.u.cp.csRefNum = curPort.refin;
  305.     SIOpb.u.cp.csCode = 2;
  306.     SIOpb.u.cp.csParam.asyncNBytes = 0L;
  307.     
  308.     sp = (Ptr) &SIOpb;
  309.     
  310.     /**/ PBStatus (&SIOpb, FALSE); /**/
  311. /*
  312. #asm
  313.         move.l    a3,a0
  314.         dc.w    $a005
  315. #endasm
  316. */
  317.     count = SIOpb.u.cp.csParam.asyncNBytes;
  318.     if (count >= 1) 
  319.         return TRUE;
  320.     else 
  321.         return FALSE;
  322.     }
  323.  
  324.  
  325. sleep (seconds)
  326. {
  327.     long timea;
  328.  
  329.     timea = Ticks;
  330.     seconds *= 60;
  331.  
  332.     while ((long)(Ticks - timea) < (long) seconds);
  333.  
  334.     }
  335.     
  336. ssendbrk (bnulls)
  337. int bnulls;
  338. {
  339.     int setting;
  340.     
  341.     setting = curPort.baud + noParity + stop10 + data8;
  342.  
  343.     if (SerReset(curPort.refin, setting) )  {
  344.         debugMsg ("\PSerReset error");
  345.         SysBeep (20);
  346.         }    
  347.  
  348.   swrite("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", bnulls*2);
  349.  
  350.     setting = curPort.baud + noParity + stop10 + data8;
  351.  
  352.     if (SerReset(curPort.refin, setting) )  {
  353.         debugMsg ("\PSerReset error");
  354.         SysBeep (20);
  355.         }    
  356.  
  357.    return;
  358.     }
  359.  
  360.  
  361. /*     Fill array *cp with characters from serial buffer, starting
  362.     at 0, until cmax. Return actual chars read. Don't read unless
  363.     at least cmin are available.
  364. */
  365. int getn ( cp, cmin, cmax, refin )
  366. char *cp;
  367. register int cmax, cmin;
  368. register short refin;
  369. {    
  370.     struct ParamBlkRec SIOpb;
  371. register    Ptr    pbp = &SIOpb;
  372.  
  373.     register int count;
  374.  
  375.     /* fprintf( stderr, " %d", cmin ); /* */
  376.     
  377.     SIOpb.ioCompletion = NULL;
  378.     SIOpb.u.cp.csRefNum = refin;
  379.     SIOpb.u.cp.csCode = 2;
  380.     SIOpb.u.cp.csParam.asyncNBytes = 0L;
  381.     
  382.     pbp = (Ptr) &SIOpb;
  383.  
  384.     /**/ PBStatus (&SIOpb, FALSE); /**/
  385. /*
  386. #asm
  387.         move.l    a3,a0
  388.         dc.w    $a005
  389. #endasm
  390. */
  391.     count = (int) SIOpb.u.cp.csParam.asyncNBytes;
  392.     SIOpb.u.iop.ioReqCount = (long) count;
  393.     if ( count >= cmin) {
  394.         if ( count >  cmax) 
  395.             SIOpb.u.iop.ioReqCount = (long) cmax;
  396.            SIOpb.u.iop.ioRefNum = refin;
  397.            SIOpb.u.iop.ioBuffer = (Ptr) cp;
  398.            SIOpb.u.iop.ioPosMode = 0;
  399.         /* */ PBRead (&SIOpb, FALSE); /* */
  400. /*
  401. #asm
  402.             move.l    a3,a0
  403.             dc.w    $a002
  404. #endasm
  405. */
  406.         count = (int) SIOpb.u.iop.ioActCount;
  407.         return( count );
  408.         }
  409.     else
  410.         return 0;
  411. }
  412.     
  413.  
  414. int putu( c, count, refout )
  415. char     *c;
  416. short    count;
  417. short    refout;
  418. {
  419.     struct ParamBlkRec  pb;
  420. register    Ptr    pbp = &pb;
  421.     
  422.     pb.u.iop.ioRefNum = refout;
  423.     pb.u.iop.ioBuffer = c;
  424.     pb.u.iop.ioReqCount = count;
  425.     pb.u.iop.ioPosMode = 0;
  426.   
  427.     /* */ PBWrite(&pb, FALSE ); /* */
  428. /*
  429. #asm
  430.     move.l    a3,a0
  431.     dc.w        $a003
  432. #endasm
  433. */
  434.     return( (short) pb.u.iop.ioActCount );
  435. }
  436.  
  437.  
  438.  
  439.  
  440.