home *** CD-ROM | disk | FTP | other *** search
- /* sio.c
-
-
- Serial I/O for rz/sz
-
-
- Copyright (c) 1986 Stuart Lynne
-
- June 1986
-
-
-
- */
- #include <local/sio.h>
- #include <stdio.h>
-
- #include <pb.h>
-
- #include <osutil.h>
-
- /* #include <max/macglobals.h> */
- #include <max/debug.h>
- #include <max/asc.h>
-
- #define FALSE 0
- #ifndef TRUE
- #define TRUE 1
- #endif
-
-
- struct SIOPort curPort;
-
-
- struct {
- unsigned baudr;
- int speedcode;
- } speeds[] = {
- 300, baud300,
- 600, baud600,
- 1200, baud1200,
- 2400, baud2400,
- 4800, baud4800,
- 9600, baud9600,
- 19200, baud19200,
- /*
- 38400, baud38400,
- */
- 57600, baud57600,
- 9600, baud9600,
- 0,
- };
-
- static unsigned
- getspeed(code)
- {
- register n;
-
- for (n=0; speeds[n].baudr; ++n)
- if (speeds[n].speedcode == code)
- return speeds[n].baudr;
- return 0;
- }
-
-
- static unsigned
- getbaud(code)
- char *code;
- {
- register n;
- register int Baudrate;
-
- Baudrate = atoi(code);
-
- for (n=0; speeds[n].baudr; ++n)
- if (speeds[n].baudr == Baudrate)
- return speeds[n].speedcode;
- return 0;
- }
-
-
-
- SIOInit ( whichport, speed )
- char * whichport;
- char * speed;
- {
- char i;
- SerShk handshake;
- int setting;
- int err;
-
- /* fprintf( stderr, "sioinit %s %s\n", whichport, speed ); /* */
-
- if (
- strncmp( whichport, ".a", 2 ) == 0 ||
- strncmp( whichport, "a",1 ) == 0 ||
- strcmp( whichport, "modem") == 0
- )
- {
- curPort.in = AIN;
- curPort.out = AOUT;
- }
- else if (
- strncmp( whichport, ".b", 2 ) == 0 ||
- strncmp( whichport, "b", 1 ) == 0 ||
- strcmp( whichport, "printer") == 0
- )
- {
- curPort.in = BIN;
- curPort.out = BOUT;
- }
- else
- return( -1 );
-
- if ((err = OpenDriver ( curPort.in, &curPort.refin )) !=0 ) {
- fprintf( stderr, "Err: %d\n", err);
- debugMsg ( ctop( curPort.in ) );
- SysBeep (20);
- return( -1 );
- }
-
-
- if ((err=OpenDriver ( curPort.out, &curPort.refout )) !=0 ) {
- fprintf( stderr, "Err: %d\n", err);
- debugMsg ( ctop( curPort.out ) );
- SysBeep (20);
- return( -1 );
- }
-
- SIOHandshake( FALSE, FALSE, FALSE, XON, XOF );
- SIOSettings( speed, noParity, stop10, data8 );
- return( 0 );
- }
-
- SIOSpeed( speed )
- char * speed;
- {
- SIOSetting( speed, curPort.parity, curPort.stopbits, curPort.databits );
- }
-
- SIOHandshake ( fInx, fXOn, fCTS, xOn, xOff )
- {
- int err;
-
- curPort.handshake.fInX = fInx;
- curPort.handshake.fXOn = fXOn;
- curPort.handshake.fCTS = fCTS;
-
- curPort.handshake.xOn = xOn;
- curPort.handshake.xOff = xOff;
- /* handshake.fXOn = TRUE; */
-
- curPort.handshake.errs = 0;
- curPort.handshake.evts = 0;
-
- if ((err=SerHShake (curPort.refin, &curPort.handshake ))!=0 ) {
- fprintf( stderr, "Err: %d\n", err);
- debugMsg ("\PSerHShake error");
- SysBeep (20);
- }
- }
-
- SIOSetting( speed, parity, stopbits, databits )
- char * speed;
- {
- int err;
- int setting;
-
- curPort.baud = getbaud(speed);
- curPort.parity = parity;
- curPort.stopbits = stopbits;
- curPort.databits = databits;
-
- setting = curPort.baud + parity + stopbits + databits;
-
- if ((err=SerReset(curPort.refin, setting))!=0 ) {
- fprintf( stderr, "Err: %d\n", err);
- debugMsg ("\PSerReset error");
- SysBeep (20);
- }
- }
-
- SIOInBuffer ( buf, size )
- char * buf;
- int size;
- {
- curPort.inbuffer = buf;
- curPort.insize = size;
- SerSetBuf( curPort.refin, buf, size );
- }
-
- SIOOutBuffer ( buf, size )
- char * buf;
- int size;
- {
- curPort.outbuffer = buf;
- curPort.outsize = size;
- SerSetBuf( curPort.refout, buf, size );
- }
-
-
- SIOClose ( dtr )
- {
- struct ParamBlkRec SIOpb;
-
- register Ptr *sp;
- long count;
-
- if ( dtr != 0) {
- /* drop DTR */
- SIOpb.ioCompletion = NULL;
- SIOpb.u.cp.csRefNum = curPort.refin;
- SIOpb.u.cp.csCode = 18;
- sp = (Ptr) &SIOpb;
-
- /**/ PBControl (&SIOpb, FALSE); /**/
- /*
- #asm
- move.l a3,a0
- dc.w $a004
- #endasm
- */
- }
-
- SIOInBuffer( NULL, 0 );
- SIOOutBuffer( NULL, 0 );
- CloseDriver( curPort.refin );
- CloseDriver( curPort.refout );
- }
-
-
- sfflushout ()
- {
- }
-
- SIOPurge ()
- {
- char ch;
-
- while (sread (&ch, 1, 1) == 1);
-
- }
-
- SIOPutchar ( ch )
- char ch;
- {
- /*fprintf(stderr, "<%02x>", ch);*/
- return( putu ( &ch, 1, curPort.refout ) );
- }
-
- SIOWrite ( buf, count )
- char * buf;
- int count;
- {
- return( putu (buf, count, curPort.refout) );
- }
-
- SIOWStr ( st )
- char * st;
- {
- return( putu (st, strlen(st), curPort.refout) );
- }
-
- #define Ticks (*(long *) 0x16a)
-
-
- /* timeout is in tenths of a second */
- int SIORead ( byt, mincount, maxcount, tenths )
- char * byt;
- int mincount;
- int maxcount;
- int tenths;
- {
- long timea;
- short actCount;
-
-
- int i;
-
- timea = Ticks;
- tenths *= 6;
- /* fprintf (stderr, "Read m= %d T= %ld", maxcount, Ticks );/**/
- do {
- actCount = getn (byt, mincount, maxcount, curPort.refin );
-
- if (actCount > 0) {
- /* fprintf (stderr, " OK: %d\n", actCount);/**/
- return actCount;
- }
-
- } while ((long)(Ticks - timea) < (long) tenths);
-
- /* fprintf (stderr, " Timed out: t= %d\n T:%ld", tenths, Ticks );/**/
- return -1;
- }
-
- srdchk ()
- {
- struct ParamBlkRec SIOpb;
-
- register Ptr *sp;
- long count;
-
- SIOpb.ioCompletion = NULL;
- SIOpb.u.cp.csRefNum = curPort.refin;
- SIOpb.u.cp.csCode = 2;
- SIOpb.u.cp.csParam.asyncNBytes = 0L;
-
- sp = (Ptr) &SIOpb;
-
- /**/ PBStatus (&SIOpb, FALSE); /**/
- /*
- #asm
- move.l a3,a0
- dc.w $a005
- #endasm
- */
- count = SIOpb.u.cp.csParam.asyncNBytes;
- if (count >= 1)
- return TRUE;
- else
- return FALSE;
- }
-
-
- sleep (seconds)
- {
- long timea;
-
- timea = Ticks;
- seconds *= 60;
-
- while ((long)(Ticks - timea) < (long) seconds);
-
- }
-
- ssendbrk (bnulls)
- int bnulls;
- {
- int setting;
-
- setting = curPort.baud + noParity + stop10 + data8;
-
- if (SerReset(curPort.refin, setting) ) {
- debugMsg ("\PSerReset error");
- SysBeep (20);
- }
-
- 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);
-
- setting = curPort.baud + noParity + stop10 + data8;
-
- if (SerReset(curPort.refin, setting) ) {
- debugMsg ("\PSerReset error");
- SysBeep (20);
- }
-
- return;
- }
-
-
- /* Fill array *cp with characters from serial buffer, starting
- at 0, until cmax. Return actual chars read. Don't read unless
- at least cmin are available.
- */
- int getn ( cp, cmin, cmax, refin )
- char *cp;
- register int cmax, cmin;
- register short refin;
- {
- struct ParamBlkRec SIOpb;
- register Ptr pbp = &SIOpb;
-
- register int count;
-
- /* fprintf( stderr, " %d", cmin ); /* */
-
- SIOpb.ioCompletion = NULL;
- SIOpb.u.cp.csRefNum = refin;
- SIOpb.u.cp.csCode = 2;
- SIOpb.u.cp.csParam.asyncNBytes = 0L;
-
- pbp = (Ptr) &SIOpb;
-
- /**/ PBStatus (&SIOpb, FALSE); /**/
- /*
- #asm
- move.l a3,a0
- dc.w $a005
- #endasm
- */
- count = (int) SIOpb.u.cp.csParam.asyncNBytes;
- SIOpb.u.iop.ioReqCount = (long) count;
- if ( count >= cmin) {
- if ( count > cmax)
- SIOpb.u.iop.ioReqCount = (long) cmax;
- SIOpb.u.iop.ioRefNum = refin;
- SIOpb.u.iop.ioBuffer = (Ptr) cp;
- SIOpb.u.iop.ioPosMode = 0;
- /* */ PBRead (&SIOpb, FALSE); /* */
- /*
- #asm
- move.l a3,a0
- dc.w $a002
- #endasm
- */
- count = (int) SIOpb.u.iop.ioActCount;
- return( count );
- }
- else
- return 0;
- }
-
-
- int putu( c, count, refout )
- char *c;
- short count;
- short refout;
- {
- struct ParamBlkRec pb;
- register Ptr pbp = &pb;
-
- pb.u.iop.ioRefNum = refout;
- pb.u.iop.ioBuffer = c;
- pb.u.iop.ioReqCount = count;
- pb.u.iop.ioPosMode = 0;
-
- /* */ PBWrite(&pb, FALSE ); /* */
- /*
- #asm
- move.l a3,a0
- dc.w $a003
- #endasm
- */
- return( (short) pb.u.iop.ioActCount );
- }
-
-
-
-
-