Communication Program for Mac OS


With the permission of Mr. Kakinoki, here are the source codes for a communication program for a Macintosh.


The following are the source codes for communication programming for a Macintosh. You can use the codes in your program, but it is your responsibility to make your own communication routines. I cannot guarantee that the codes are free of bugs.


#include      <Serial.h>
byte com_speed = 1, com_bit = 1, com_parity, com_x, com_stop;
short RsPort; // 0:Modem, 1:Printer
static short  in, out;
static short  com_speeds[5] = {baud300, baud1200, baud2400,
       baud4800, baud9600};
static short  com_bits[2] = {data7, data8};
static short  com_paritys[3] = {noParity, oddParity, evenParity};
static short  com_stops[3] = {stop10, stop15, stop20};
/*==============================
       return          0: OK
                     -1: error
==============================*/
int    openRS(void)
{
       SerShk shk;
       OSErr  err;
       short conf = com_speeds[com_speed]
                     + com_stops[com_stop]
                     + com_paritys[com_parity]
                     + com_bits[com_bit];
       shk.fXOn = 0;
       shk.fCTS = 1;
       shk.xOn = 0x13;
       shk.xOff = 0x11;
       shk.errs = parityErr + hwOverrunErr + framingErr;
       shk.evts = ctsEvent;
       shk.fInX = 0;
       if (RsPort)   // printer port / KEYSPAN USB-SERIAL ADAPTER
       {
              err = OpenDriver("\p.BIn", &in);
              if (err != noErr)
                     return -1;
              err = OpenDriver("\p.BOut", &out);
       }
       else   // modem port
       {
              err = OpenDriver("\p.AIn", &in);
              if (err != noErr)
                     return -1;
              err = OpenDriver("\p.AOut", &out);
       }
       if (err != noErr)
              return -1;
       err = SerReset(in, conf);
       if (err != noErr)
              return -1;
       err = SerReset(out, conf);
       if (err != noErr)
              return -1;
       err = SerHShake(in, &shk);
       if (err != noErr)
              return -1;
       err = SerHShake(out, &shk);
       if (err != noErr)
              return -1;
       return 0;
}
void   closeRS(void)
{
       CloseDriver(in);
       CloseDriver(out);
}
/*======================
       readRS
       read 1byte
       return          N: OK
                     -1: error
                     -2: break
======================*/
static short readRS(long *len, char *buf)
{
       SerStaRec     st;
       short         err, ref;
       SerStatus(in, &st);
       if (st.cumErrs)
       {
              *len = 0L;
              return -1;
       }
       for (;;)
       {
              if ((err = SerGetBuf(in, len)) != noErr)
                     return -1;
              if (*len)
              {
                     FSRead(in, len, buf);
                     return *len;
              }
              if (break_check())   // operator's break function
                     return -2;
       }
       return 0;
}
/*======================
       return          0: OK
                            != 0: error
======================*/
static short  writeRS(long *len, char *buf)
{
       SerStaRec     st;
       short         err;
       SerStatus(out, &st);
       if (st.cumErrs || st.wrPend || st.ctsHold)
              return -1;
       return FSWrite(out, len, buf);
}
/*============*/
/*======================
       return          0: OK
                            != 0: error
======================*/
static int    puts_rs(char *s)
{
       int    r;
       long   len;
       len = strlen(s);
       r = writeRS(&len, s);
       return r;
}
/*======================
       return          >0: char
                     -1: error
                     -2: break
======================*/
static int    get_rs(void)
{
       long   len = 1;
       char   buf[256];
       int    r;
       r = readRS(&len, buf);
       if (r < 0)
              return r;
       else
              return buf[0];
}