Main Page   Compound List   File List   Compound Members   File Members  

serapi.c

Go to the documentation of this file.
00001 
00025 
00026 #pragma option -1 /* create 80186 code */
00027 
00028 #include "serapi_t.h"
00029 #include "serapi.h"
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <dos.h>
00034 
00040 static statusSTR status;
00041 statusPtr statusp = &status;
00042 
00049 static int received = 0;
00050 
00051 unsigned int initSerial(int port)
00052 {
00053     union  REGS  inregs;
00054     union  REGS  outregs;
00055     struct SREGS segregs;
00056 
00057     inregs.h.ah = 0x04;
00058     inregs.x.dx = (unsigned int) port;
00059     int86x(FOS_INT, &inregs, &outregs, &segregs);
00060 
00061     return (unsigned int) outregs.x.ax;
00062 }
00063 
00064 void deinitSerial(int port)
00065 {
00066     union  REGS  inregs;
00067     union  REGS  outregs;
00068     struct SREGS segregs;
00069 
00070     inregs.h.ah = 0x05;
00071     inregs.x.dx = (unsigned int) port;
00072     int86x(FOS_INT, &inregs, &outregs, &segregs);
00073 }
00074 
00075 void setParam(int port,
00076             unsigned long baudrate,
00077             unsigned int databits,
00078             char parity,
00079             unsigned int stopbits,
00080             unsigned int flowCtrl)
00081 {
00082     union  REGS  inregs;
00083     union  REGS  outregs;
00084     struct SREGS segregs;
00085 
00086     unsigned long maxbaud;
00087     unsigned bits, _parity, stop;
00088     int divisor, div1, div2;
00089     long baud1, baud2;
00090 
00091     inregs.h.ah = 0x8A;
00092     inregs.h.al = 2;
00093     int86x(0xA1, &inregs, &outregs, &segregs);
00094     maxbaud = (((unsigned long)outregs.x.dx<<16)+outregs.x.ax);
00095 
00096     /* parity handling */
00097     switch (parity) {
00098         case 'N':
00099             _parity = 0;
00100             break;
00101         case 'O':
00102             _parity = 1;
00103             break;
00104         case 'E':
00105             _parity = 2;
00106             break;
00107         default: /* as default there is no parity */
00108             _parity = 0;
00109             break;
00110     }
00111 
00112     switch (databits) {
00113         case 7:
00114             bits = 7;
00115             break;
00116         case 8:
00117             bits = 8;
00118             break;
00119         default: /* as default there are 8 databits */
00120             bits = 8;
00121             break;
00122     }
00123 
00124     switch (stopbits) {
00125         case 1:
00126             stop = 1;
00127             break;
00128         case 2:
00129             stop = 2;
00130             break;
00131         default: /* as default there is one stopbit */
00132             stop = 1;
00133             break;
00134     }
00135 
00136     if ( (baudrate == 300) ||
00137         (baudrate == 600) ||
00138         (baudrate == 1200) ||
00139         (baudrate == 2400) ||
00140         (baudrate == 4800) ||
00141         (baudrate == 9600) ||
00142         (baudrate == 19200) ||
00143         (baudrate == 28800) ||
00144         (baudrate == 38400L) ||
00145         (baudrate == 57600L) ||
00146         (baudrate == 115200L) )
00147     {
00148         baudrate = baudrate;
00149     } else {
00150         baudrate = 2400;
00151     }
00152 
00153     div1 = (int)(maxbaud/baudrate);
00154     baud1 = (maxbaud/div1);
00155     div2 = div1+1;
00156     baud2 = (maxbaud/div2);
00157     divisor = (baud1-baudrate) < (baudrate-baud2) ? div1 : div2;
00158 
00159     // init baudrate
00160     inregs.h.ah = 0x81;
00161     inregs.h.al = bits-5; // data bits
00162     inregs.h.bh = _parity; // parity
00163     inregs.h.bl = stop-1; // 1 stop bit
00164     inregs.x.cx = divisor;// baud divider
00165     inregs.x.dx = (unsigned int) port;   // port
00166     int86x(FOS_INT, &inregs, &outregs, &segregs);
00167 
00168     /* flow control */
00169     if (flowCtrl != NO_FLOWCTRL) {
00170         inregs.h.ah = 0x0F;
00171         inregs.h.al = (byte) flowCtrl;
00172         inregs.x.dx = (unsigned int) port;
00173         int86x(FOS_INT, &inregs, &outregs, &segregs);
00174     }
00175 }
00176 
00177 void enableRS485(int port, unsigned int activityMode)
00178 {
00179     union  REGS  inregs;
00180     union  REGS  outregs;
00181     struct SREGS segregs;
00182 
00183 
00184     inregs.h.ah = 0x80;
00185     inregs.h.al = (byte) activityMode; 
00186     inregs.x.dx = (unsigned int) port;
00187     int86x(FOS_INT, &inregs, &outregs, &segregs);
00188 }
00189 
00190 byte blockedSend(int port, byte b)
00191 {
00192     union  REGS  inregs;
00193     union  REGS  outregs;
00194     struct SREGS segregs;
00195 
00196     inregs.h.ah = 0x01;
00197     inregs.h.al = b;
00198     inregs.x.dx = (unsigned int) port;
00199     int86x(FOS_INT, &inregs, &outregs, &segregs);
00200 
00201     return outregs.h.ah;
00202 }
00203 
00204 bool unblockedSend(int port, byte b)
00205 {
00206     union  REGS  inregs;
00207     union  REGS  outregs;
00208     struct SREGS segregs;
00209 
00210     inregs.h.ah = 0x0B;
00211     inregs.h.al = b; 
00212     inregs.x.dx = (unsigned int) port;
00213     int86x(FOS_INT, &inregs, &outregs, &segregs);
00214 
00215     if (outregs.x.ax == 1) {
00216         return 1;
00217     } else {
00218         return 0;
00219     }
00220 }
00221 
00222 unsigned int sendBlock(int port, byte far* dataBlock, unsigned int length)
00223 {
00224     union  REGS  inregs;
00225     union  REGS  outregs;
00226     struct SREGS segregs;
00227 
00228     inregs.h.ah = 0x19;
00229     inregs.x.cx = length;
00230     inregs.x.dx = (unsigned int) port;
00231     segregs.es  = FP_SEG(dataBlock);
00232     inregs.x.di = FP_OFF(dataBlock);
00233     int86x(FOS_INT, &inregs, &outregs, &segregs);
00234 
00235     return outregs.x.ax;
00236 }
00237 
00238 byte blockedReceive(int port)
00239 {
00240     union  REGS  inregs;
00241     union  REGS  outregs;
00242     struct SREGS segregs;
00243 
00244     inregs.h.ah = 0x02;
00245     inregs.x.dx = (unsigned int) port;
00246     int86x(FOS_INT, &inregs, &outregs, &segregs);
00247     return outregs.h.al;                    
00248 }
00249 
00250 byte unblockedReceive(int port)
00251 {
00252     union  REGS  inregs;
00253     union  REGS  outregs;
00254     struct SREGS segregs;
00255 
00256     inregs.h.ah = 0x0C;
00257     inregs.x.dx = (unsigned int) port;
00258     int86x(FOS_INT, &inregs, &outregs, &segregs);
00259 
00260     if (outregs.x.ax == (unsigned int) 0xFFFF) { /* no byte was available */
00261         received = 0;
00262     } else { /* there was a byte available */
00263         received = 1;
00264         return outregs.h.al;
00265     }
00266     
00267     return 0;
00268 }
00269 
00270 bool byteAvail()
00271 {
00272     if (received != 0) {
00273         return 1;
00274     } else {
00275         return 0;
00276     }
00277 }
00278 
00279 unsigned int receiveBlock(int port, byte far* dataBlock, unsigned int length)
00280 {
00281     union  REGS  inregs;
00282     union  REGS  outregs;
00283     struct SREGS segregs;
00284 
00285     inregs.h.ah = 0x18;
00286     inregs.x.cx = length; 
00287     inregs.x.dx = (unsigned int) port;
00288     segregs.es  = FP_SEG(dataBlock);
00289     inregs.x.di = FP_OFF(dataBlock);
00290     int86x(FOS_INT, &inregs, &outregs, &segregs);
00291 
00292     return outregs.x.ax;
00293 }
00294 void flushOutBuffer(int port)
00295 {
00296     union  REGS  inregs;
00297     union  REGS  outregs;
00298     struct SREGS segregs;
00299 
00300     inregs.h.ah = 0x08;
00301     inregs.x.dx = (unsigned int) port;
00302     int86x(FOS_INT, &inregs, &outregs, &segregs);
00303 }
00304 
00305 void deleteOutBuffer(int port)
00306 {
00307     union  REGS  inregs;
00308     union  REGS  outregs;
00309     struct SREGS segregs;
00310 
00311     inregs.h.ah = 0x09;
00312     inregs.x.dx = (unsigned int) port;
00313     int86x(FOS_INT, &inregs, &outregs, &segregs);    
00314 }
00315 
00316 void deleteInBuffer(int port)
00317 {
00318     union  REGS  inregs;
00319     union  REGS  outregs;
00320     struct SREGS segregs;
00321 
00322     inregs.h.ah = 0x0A;
00323     inregs.x.dx = (unsigned int) port;
00324     int86x(FOS_INT, &inregs, &outregs, &segregs);
00325 }
00326 
00327 statusPtr getStatus(int port, byte statByte)
00328 {
00329     union  REGS  inregs;
00330     union  REGS  outregs;
00331     struct SREGS segregs;
00332 
00333     byte stat;
00334 
00335     byte outBufferEmpty;
00336     byte outBufferNotFull;
00337     byte recOverrun;
00338     byte dataAvailable;
00339 
00340     outBufferEmpty = 64;
00341     outBufferNotFull = 32;
00342     recOverrun = 2;
00343     dataAvailable = 1;
00344 
00345     stat = statByte;
00346 
00347     if ( statByte == NO_STATUS ) {
00348         inregs.h.ah = 0x03;
00349         inregs.x.dx = (unsigned int) port;
00350         int86x(FOS_INT, &inregs, &outregs, &segregs);
00351         stat = outregs.h.ah;
00352     }
00353 
00354     statusp->outBufferEmpty = stat & outBufferEmpty;
00355     statusp->outBufferNotFull = stat & outBufferNotFull;
00356     statusp->recOverrun = stat & recOverrun;
00357     statusp->dataAvailable = stat & dataAvailable;
00358 
00359     return statusp;
00360 }

Generated at Fri Oct 26 12:43:45 2001 for SerialAPI by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001