home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
OS2UTIL.ZIP
/
OS2ASY.C
< prev
next >
Wrap
Text File
|
1990-05-21
|
19KB
|
470 lines
/* os2asy.c */
/* This file contains twelve routines for asynchrounous communication through*/
/* com1 and com2. The routines: port_open(), port_close(), port_close_all(), */
/* send_line(), send_asy(), asy_chr(), asyget(), asy_stream(), inqsize(), */
/* asy_get(), fasy_get(), rdch(), flushib() and flushob() are provided below.*/
/* The file os2asy.h should be included at the top of your program */
#define OS2ASY
#include "os2asy.h"
/* port_open - open com port and return error message */
/* */
/* CALLING SYNTAX: port_open(port) */
/* */
/* int port_open(); open port port */
/* 0 - OK */
/* >0 - error */
/* int port; com port number; range 1-2 */
/* */
/* MEG - 05/16/90 */
port_open(port, baud, xon)
int port, baud, xon;
{
int sts, res;
char portstr[5];
if (port > MAX_PORTS)
return -1;
sprintf(portstr, "com%d", port);
sts = DosOpen(portstr, &asy.porthf[port], &res, 0L, 0, 1, 0x12, 0L);
if(sts) {
/*printf("com%d port error sts = %d\n", port, sts);*/
return 1;
} /* puts("com opened"); */
DosDevIOCtl(0L, &baud, 0x41, 0x01, asy.porthf[port]); /* puts("buad rate set"); */
DosDevIOCtl(0L, &comm_ctl, 0x42, 0x01, asy.porthf[port]);/* puts("other param set"); */
/* enable xon/xoff for xmit and receive */
DosDevIOCtl(&di, 0L, 0x73, 0x01, asy.porthf[port]);
di.f1 = 0;
di.f2 = (xon) ? (char) 0x03 : (char) 0x00;
DosDevIOCtl(0L, &di, 0x53, 0x01, asy.porthf[port]);
asy.curptr[port] = asy.commbuff[port];
asy.bufptr[port] = asy.buff[port];
return(0);
}
/* port_close - close com port and return error message */
/* */
/* CALLING SYNTAX: port_close(port) */
/* */
/* int port_close(); close port port */
/* 0 - OK */
/* >0 - error */
/* int port; com port number; range 1-2 */
/* */
/* MEG - 12/22/88 */
port_close(port)
int port;
{
return( DosClose(asy.porthf[port]) );
/* printf("com%d closed\n", port); */
}
/* port_close_all - close all com ports and return error message */
/* */
/* CALLING SYNTAX: port_close_all() */
/* */
/* int port_close(); close all com ports */
/* 0 - OK */
/* 2 - file not found COM1 */
/* 5 - access denied COM1 */
/* 6 - invalid handle COM1 */
/* 16 - file not found COM2 */
/* 40 - access denied COM2 */
/* 48 - invalid handle COM2 */
/* */
/* error codes for COM1 and COM2 are added together */
/* */
/* MEG - 12/22/88 */
port_close_all()
{
return( DosClose(asy.porthf[COM1]) + 8*DosClose(asy.porthf[COM2]) );
}
/* send_line - send a line of data to the com port */
/* */
/* CALLING SYNTAX: send_line(port, str) */
/* */
/* int send_line(); send string to a com port */
/* 0 - OK */
/* >0 - error */
/* int port; com port number; range 1-2 */
/* char *str data to be send */
/* */
/* MEG - 03/09/90 */
send_line(port, str)
char *str;
{
char cmd[256];
int size = 1, cnt, tmp, retcode;
QInfo qdata;
/*printf(" port = %d str='%s'\n", port, str);*/
strncpy(cmd, str, 256);
cmd[255] = NULL;
cnt = strlen(cmd); /* get string length */
/*printf(" cnt=%d cmd='%s'", cnt, cmd);*/
retcode=DosWrite(asy.porthf[port], cmd, cnt, &tmp);
/*printf(" write retcode=%d", retcode);*/
while(size) { /* wait until sent */
DosDevIOCtl(&qdata, 0L, 0x69, 0x01, asy.porthf[port]);
size = qdata.chrs_in_q;
/*printf(" size = %d", size);*/
}
return(retcode);
}
/* send_asy -- send a line of data to the com port w/CR */
/* */
/* CALLING SYNTAX: send_asy(port, str) */
/* */
/* int send_asy(); send string to a com port */
/* 0 - OK */
/* >0 - error */
/* int port; com port number; range 1-2 */
/* char *str data to be send -- CR tacked on */
/* */
/* MEG - 03/12/90 */
send_asy(port, str)
int port;
char *str;
{
send_line(port, str);
return asy_chr(port, '\r');
}
/* asy_chr - send a character to the com port */
/* */
/* CALLING SYNTAX: asy_chr(port, chr) */
/* */
/* int asy_chr(); send string to a com port */
/* 0 - OK */
/* >0 - error */
/* int port; com port number; range 1-2 */
/* char *chr data to be send */
/* */
/* MEG - 12/22/88 */
asy_chr(port, chr)
int port;
char chr;
{
int size=1, tmp, retcode;
char str[2];
QInfo qdata;
/* printf("string='%s'\n", send);*/
str[0] = chr; str[1] = NULL;
retcode = DosWrite(asy.porthf[port], str, 1, &tmp);
while (size) {
DosDevIOCtl(&qdata, 0L, 0x69, 0x01, asy.porthf[port]);
size = qdata.chrs_in_q;
}
return(retcode);
}
/* asyget - receive data from com port port, if available */
/* */
/* CALLING SYNTAX: asyget(port) */
/* */
/* int asyget(); receive string from port com */
/* 0 - indicates a record is ready to be */
/* parsed. data is stored in a global */
/* varaible named: commbuff[port] */
/* 1 - no complete record send */
/* int port com port number; range: 1 - 2 */
/* */
/* MEG - 12/22/88 */
asyget(port)
int port;
{
int retcode = 1;
static int asyret[3] = { 0, 0, 0 }, bufflen[3] = { 0, 0, 0 };
int numread, qcnt;
char *ptr;
/* move date from other buffer to tmpbuff */
if( *(asyret+port) ) {
*(asyret+port) = 0;
*(asy.curptr+port) = *(asy.commbuff+port);
while( (*(bufflen+port))--) {
if(**(asy.bufptr+port) == LF) {
*((*(asy.curptr+port))-1) = 0;
++*(asy.bufptr+port);
*(asyret+port) = 1;
retcode = 0;
break;
}
else
*(*(asy.curptr+port))++ = *(*(asy.bufptr+port))++;
}
}
qcnt = inqsize(port);
*(iqsize+port) = qcnt;
if(qcnt > *(hi_q+port) )
*(hi_q+port) = qcnt;
if(qcnt > 800)
qcnt = 800;
if(qcnt && !*(asyret+port) ) {
DosRead(*(asy.porthf+port), *(asy.curptr+port), qcnt, &numread);
while(numread--) {
if(**(asy.curptr+port) == LF && !*(asyret+port) ) {
*((*(asy.curptr+port))-1) = 0;
ptr = *(asy.curptr+port) + 1;
*(asy.curptr+port) = *(asy.buff+port);
*(bufflen+port) = 0;
*(asy.bufptr+port) = *(asy.buff+port);
*(asyret+port) = 1;
retcode = 0;
continue;
}
if( *(asyret+port) ) {
**(asy.curptr+port) = *ptr++;
++*(bufflen+port);
}
++*(asy.curptr+port);
}
}
return(retcode);
}
/* asy_stream - receive chrs bytes of data from com port port */
/* */
/* CALLING SYNTAX: asy_stream(port, chrs, string, ltime) */
/* */
/* int asy_stream(); receive string chrs chars long from comm */
/* -1 - insufficient chars in queue */
/* 0 - indicates a record is ready to be */
/* parsed. data is stored in string */
/* 1 - no complete record send */
/* int port com port number; range: 1 - 2 */
/* int chrs number of character to be grabbed */
/* char *string return string */
/* unsigned long ltime time limit in ticks for error if input */
/* queue does not receive chr charactes */
/* */
/* MEG - 05/16/90 */
asy_stream(port, chrs, string, ltime)
int port, chrs;
long ltime;
char *string;
{
int numread;
long ctime;
ctime = dostime();
/* wait until queue ready */
while (inqsize(port) < chrs && (long)(dostime()-ctime) < ltime);
if ((long)(dostime() - ctime) >= ltime) /* timeout exceeded */
return -1;
/* get chrs amount of characters */
DosRead(*(asy.porthf+port), string, chrs, &numread);
if (numread == chrs) /* correct flow */
{
/*strcpy(string, asy.curptr[port]);*/
return 0;
}
else /* error flow */
{
flushib(port); /* reset for next call */
return 1; /* improper number of chars read */
}
}
/* inqsize - get queue size from com port */
/* */
/* CALLING SYNTAX: inqsize(port) */
/* */
/* int inqsize(); return input queue size of com port */
/* int port; com port number; range: 1-2 */
/* */
/* MEG - 12/22/88 */
inqsize(port)
int port;
{
QInfo qdata;
DosDevIOCtl(&qdata, 0L, 0x68, 0x01, asy.porthf[port]);
return(qdata.chrs_in_q);
}
/* asy_get - receive data from com port, if available */
/* */
/* CALLING SYNTAX: asy_get(port, string) */
/* */
/* int asy_get(); receive string from com port */
/* */
/* int port; com port number; range 1-2 */
/* */
/* char *string; data from com port */
/* */
/* int retcode; 0 - indicates a record is ready to be */
/* parsed. data is stored in a variable */
/* named: string */
/* 1 - no complete record send */
/* */
/* MEG - 12/22/88 */
asy_get(port, string)
int port;
char *string;
{
int retcode;
DosSleep(250L); /* wait 1/4 seconds */
if ( (retcode = asyget(port)) == OK)
strcpy(string, asy.commbuff[port]);
return(retcode);
}
/* fasy_get - receive data with no wait from com port, if avail */
/* */
/* CALLING SYNTAX: fasy_get(port, string) */
/* */
/* int fasy_get(); receive string from com port */
/* */
/* int port; com port number; range 1-2 */
/* */
/* char *string; data from com port */
/* */
/* int retcode; 0 - indicates a record is ready to be */
/* parsed. data is stored in a variable */
/* named: string */
/* 1 - no complete record send */
/* */
/* MEG - 03/14/90 */
fasy_get(port, string)
int port;
char *string;
{
int retcode;
if ( (retcode = asyget(port)) == OK)
strcpy(string, asy.commbuff[port]);
return(retcode);
}
/* rdch - read a character from com port, if available */
/* */
/* CALLING SYNTAX: rdch(port, ch, cnt, numread) */
/* */
/* int rdch(); read a character from the com port */
/* */
/* int port; com port number; range 1-2 */
/* */
/* char *ch; data from com port */
/* */
/* int cnt; number of characters to be read */
/* */
/* int *numread; number of characters read from com port */
/* */
/* int retcode; 0 - indicates a record is ready to be */
/* parsed. data is stored in a variable */
/* named: string */
/* 1 - no complete record send */
/* */
/* MEG - 04/11/89 */
rdch(port, ch, cnt, numread)
int port, cnt, *numread;
char *ch;
{
int retcode;
if (inqsize(port))
retcode = DosRead(asy.porthf[port], ch, cnt, numread);
else
return 2;
return (retcode == 0 && *numread == cnt) ? 0 : 1;
}
/* flushib - flush the input buffer */
/* */
/* CALLING SYNTAX: flush(port) */
/* */
/* int flushib(); flush the input buffer to com port */
/* int port; com port number; range: 1-2 */
/* */
/* MEG - 03/12/90 */
/* */
flushib(port)
int port;
{
/* FLUSH INPUT buffer */
return DosDevIOCtl(0L, 0, 0x0001, 0x000B, asy.porthf[port]);
}
/* flushob - flush the output buffer */
/* */
/* CALLING SYNTAX: flushob(port) */
/* */
/* int flushob(); flush the output buffer to com port */
/* int port; com port number; range: 1-2 */
/* */
/* MEG - 03/12/90 */
/* */
flushob(port)
int port;
{
/* FLUSH OUTPUT buffer */
return DosDevIOCtl(0L, 0, 0x0002, 0x000B, asy.porthf[port]);
}
/* dostime.c */
unsigned long dostime() /* ticks */
{
GINFOSEG FAR *info;
SEL gbl, local;
int hour, min, sec;
unsigned long gcurtime;
DosGetInfoSeg(&gbl, &local);
info = MAKEPGINFOSEG(gbl);
hour = info->hour;
min = info->minutes;
sec = info->seconds;
gcurtime = hour*3600L + min*60L + sec;
gcurtime = gcurtime * 18 + gcurtime / 5;
gcurtime += info->hundredths / 5;
return(gcurtime);
}