home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1996 September
/
PCWK996.iso
/
polskie
/
orhmet
/
dfdisk
/
rlibc
/
rs232.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-28
|
3KB
|
151 lines
#include <dos.h>
#include "rs_defs.ch"
#define XON 5
#define XOFF 6
#define SET_RTS (outportb( COM_Adr+4, inportb( COM_Adr+4) | 2 ))
#define CLR_RTS (outportb( COM_Adr+4, inportb( COM_Adr+4) & 0xfd ))
#define CTS (inportb(COM_Adr+6)&0x10)
#define ISTIMEOUT(ticks,timeout) \
( ( mk_ticks() + (ticks+timeout>=1092?1092/2:0) )%1092 >\
(ticks+timeout + (ticks+timeout>=1092?1092/2:0) )%1092 )
#define TIMEOUT (3*18);
unsigned int rs232_timeout;
unsigned int COM_Adr;
void set_timeout ( int timeout )
{
rs232_timeout=timeout;
}
int get_timeout ( void )
{
return (rs232_timeout);
}
unsigned int mk_ticks ( void )
{
unsigned long wynik;
_AH=0x00;
asm int 1ah; /*odczytanie systemowego licznika czasu*/
wynik=(_CX<<16)+_DX;
wynik%=65543L; /*ile reszty z godzin*/
return((unsigned int)(wynik%1092L)); /*ile reszty z minut*/
}
unsigned char HandShake(void)
{
unsigned int pocz;
pocz = mk_ticks();
while ( CTS && !ISTIMEOUT( pocz, rs232_timeout) );
if ( CTS )
{
CLR_RTS;
return(0);
}
SET_RTS;
while ( !CTS && !ISTIMEOUT( pocz, rs232_timeout) );
if ( !CTS )
{
CLR_RTS;
return( 0 );
}
return(1);
}
unsigned char SendPortB( unsigned char byte)
{
if (!HandShake())
return(0xff);
outportb( COM_Adr, byte );
while( !(inportb( COM_Adr+5 ) & 0x20) );
CLR_RTS;
return(byte);
}
unsigned char RecvPortB(void)
{
unsigned char byte;
unsigned int pocz;
if (!HandShake())
return(0xff);
pocz = mk_ticks();
while ( !(inportb( COM_Adr+5 )&1) )
if (ISTIMEOUT( pocz, rs232_timeout) )
{
CLR_RTS;
return(0xff);
}
byte = inportb( COM_Adr );
CLR_RTS;
return(byte);
}
int recvport( unsigned char no, char *str )
{
unsigned char i=0,a;
do
{
a = RecvPortB();
if ( a == 0xff )
return( -1 );
if ( a == XOFF )
return( -1 );
} while ( a != XON );
while ( i < no )
{
if ((a = RecvPortB())==0xff)
return( -1 );
if ( a == XOFF )
return( -1 );
str[i++] = (char )a;
}
return(1);
}
int sendport( unsigned char no, char *str )
{
unsigned int i=0;
if ( SendPortB( XON )==0xff )
return( -1 );
while ( i < no )
if (SendPortB( (char )str[i++] )==0xff)
{
SendPortB( XOFF );
return(-1);
}
return(1);
}
int initport( unsigned int adr)
{
char tmp[3];
COM_Adr = adr;
outportb( COM_Adr+3, 0x80 );
outportb( COM_Adr, 12 );
outportb( COM_Adr+1, 0 );
outportb( COM_Adr+3, 3 );
outportb( COM_Adr+1, 0 );
outportb( COM_Adr+4, 0 );
rs232_timeout = TIMEOUT;
CLR_RTS;
tmp[0] = 'X';
tmp[1] = 'X';
tmp[2] = 0;
sendport( 2, tmp );
if ( recvport( 2, tmp )==-1 )
return(0);
return(1);
}