#define SER_STOP_1 0 /*1stop bit per character*/
#define SER_STOP_2 4
#define SER_BITS_5 0 /*send 5 bit characters*/
#define SER_BITS_6 1 /*send 6 bit characters*/
#define SER_BITS_7 2 /*send 7 bit characters*/
#define SER_BITS_8 3 /*send 8 bit characters*/
#define SER_PARITY_NONE 0 /*no parity*/
#define SER_PARITY_ODD 8
#define SER_PARITY_EVEN 24
#define SER_DIV_LATCH_ON 0x80 /*used to turn reg 0,1 into divisor latch*/
#define PIC_IMR 0x21 /*pic's interrupt mask reg.*/
#define PIC_ICR 0x20 /*pic/s interrupt control reg.*/
#define INT_SER_PORT_0 0x0C /*port 0 interrupt com 1&3*/
#define INT_SER_PORT_1 0x0B
#define Int_Mode 0
#define Ask_Mode 1
#define SERIAL_BUFF_SIZE 4096 /*current size of circulating receive buffer*/
#define SER_BAUD_600 192
#define SER_BAUD_1800 64
#define SER_BAUD_2400 48
#define SER_BAUD_9600 12
#define SER_BAUD_19200 6
#define SER_BAUD_38400 3
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt (*Old_Isr)(__CPPARGS); /*holds old com port interrupt handler*/
unsigned char ser_buffer[SERIAL_BUFF_SIZE]; /*the receive buffer*/
int ser_end = -1, ser_start = -1; /* start & and of ring buffer */
int char_ready=0; /* count of received byted */
int old_int_mask; /* backup of interrupt mask */
unsigned open_port; /* what port we use */
int serial_lock = 0; /* serial port is in use */
//Input byte from port
unsigned char _fastcall inb(unsigned int port)
{
asm mov dx, port;
asm in al, dx;
return _AL;
}
//Output byte to port
void _fastcall outb(unsigned int port, unsigned char value)
{
asm mov dx, port
asm mov al, value
asm out dx, al
}
/* Insterrupt Service Routine - receive and put to ring buffer */
void interrupt ReadSer(void) /*ISR program.*/
{
unsigned char ch;
serial_lock = 1; /* lock serial port */
ch = inb(open_port + SER_RBF); /* input byte from port */
if (++ser_end > SERIAL_BUFF_SIZE - 1) ser_end = 0; /* check for ring end */
ser_buffer[ser_end] = ch; /* put character to ring buffer */
char_ready++; /* Notipy that character is ready */
outb(PIC_ICR,0x20); /* Send End Of Interrupt signal */
serial_lock=0; /* Unlock serial port */
}
/* Get a byte from input ring buffer */
int ReadQueue(void)
{
int ch;
while (serial_lock); /* While port is locked (ISR active) */
/* Check for ring buffer overlap */
if (ser_end != ser_start) {
/* We have some data to receive */
if (++ser_start > SERIAL_BUFF_SIZE - 1) ser_start = 0;
/* get a byte from ring buffer */
ch = ser_buffer[ser_start];
/* Notify that we have one byte less left */
if (char_ready > 0) char_ready--;
return ch;
}
else
/* We have no data to receive */
return -1;
}
/* Send a byte via com port */
void WriteSer(char ch)
{
while( !(inb(open_port + SER_LSR) & 0x20) ); /*Waiting for Sent Buffer empty.*/
/* Transmit a byte */
asm cli;
outb( open_port + SER_THR, ch);
asm sti;
}
/* Open and initialize serial link */
void OpenSerial(int port_base,int baud,int configuration)
{
open_port = port_base; /* Save what port we use */
outb(open_port + SER_LCR, SER_DIV_LATCH_ON); /*Initating*/
outb(open_port + SER_DLL, baud); /*Set baud rate*/
outb(open_port + SER_DLH, 0);
outb(open_port + SER_LCR, configuration); /*Set frame format*/
outb(open_port + SER_MCR, SER_GP02); /*Set interrupt on RX*/
outb(open_port + SER_IER, 1); /*Enable interrupt*/
old_int_mask = inb(PIC_IMR); /*Save old interrupt mask*/
if (open_port==COM_1) {
Old_Isr = getvect(INT_SER_PORT_0); /*Capture interrupt vector*/
setvect(INT_SER_PORT_0, ReadSer);
outb(PIC_IMR, old_int_mask & 0xEF); /* Unmask interrupt */
}
else {
Old_Isr = getvect(INT_SER_PORT_1); /*Capture interrupt vector*/
setvect(INT_SER_PORT_1, ReadSer);
outb(PIC_IMR, old_int_mask & 0xF7); /* Unmask interrupt */
}
}
/* Shut down serial port */
void CloseSerial(void)
{
outb(open_port + SER_MCR, 0); /* Disable interrupts */
outb(open_port + SER_IER, 0);
outb(PIC_IMR, old_int_mask); /* Mask interrupts */
/* Restore original vectror */
if (open_port == COM_1) {
setvect(INT_SER_PORT_0, Old_Isr);
}
else {
setvect(INT_SER_PORT_1, Old_Isr);
}
}
/* Framework for retrieving byte count */
int DataReadyCount(void)
{
return char_ready;
}
Результат выполнения программ.
Сервер:
Клиент:
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.