home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Boldly Go Collection
/
version40.iso
/
TS
/
17A
/
DRWIN101.ZIP
/
SERUTIL.H
< prev
next >
Wrap
Text File
|
1990-12-12
|
14KB
|
254 lines
#ifndef __SERUTIL
#define __SERUTIL
/****************************************************************************/
/* HEADER serutil.h */
/****************************************************************************/
/*-----------------------------Description----------------------------------*/
/* This file contains constants, data definitions and function prototypes */
/* for the serial communications functions in serutil.c. */
/* A sample program is located at the end of this file. */
/*-----------------------------Constraints/Gotchas--------------------------*/
/* This code requires another utility module, bufutil.c (.h), in order to */
/* run. */
/*--Date--------Programmer----------Comments--------------------------------*/
/* 1990.07.12 D. Rogers initial release, 1.00 */
/****************************************************************************/
#define SER_VERSION "1.00"
#include "bufutil.h"
#ifndef WORD
#define WORD unsigned int
#endif
#ifndef BYTE
#define BYTE unsigned char
#endif
/****************************************************************************/
/* default constants, high-level constants.. */
/****************************************************************************/
#define MAXPORTS 4
enum COMPORTS { COM1=0,COM2,COM3,COM4 };
enum SERERRORS {
SER_NONE=0, /*no serial errors*/
SER_BADPORT, /*port pointer was NULL*/
SER_UNINIT, /*port is not yet initialized*/
SER_BADCOM, /*port pointer way off, com field bad*/
SER_ILLEGALSPEC, /*bad cfg specifier*/
SER_ILLEGALCOM, /*bad cfg COM value, c*/
SER_ILLEGALBAUD, /*bad cfg baud rate, b*/
SER_ILLEGALPARY, /*bad cfg parity, p*/
SER_ILLEGALDATA, /*bad cfg data bits, d*/
SER_ILLEGALSTOP, /*bad cfg stop bits, s*/
SER_ILLEGALRSIZE, /*bad cfg rx buffer size, r*/
SER_ILLEGALTSIZE, /*bad cfg tx buffer size, t*/
SER_ILLEGALDTR, /*bad cfg DTR value specified*/
SER_ILLEGALRTS, /*bad cfg RTS value specified*/
SER_ILLEGALBRK, /*bad cfg Break value specified*/
SER_NOAVAILABLE, /*com port not available*/
SER_NOBUFMEM, /*could not allocate serial buffers*/
SER_RXOVER, /*receive buffer overflow*/
SER_TXFULL, /*transmit buffer full*/
SER_RXEMPTY, /*receive buffer empty*/
SER_NOCOMSWITCH, /*switched COM ports in sersetcfg()*/
SER_NOBUFCHANGE, /*buffer size not changed in ^^^^^^*/
SER_LASTERR
};
#define SER_DEF_COM COM1
#define SER_DEF_BAUD 9600
#define SER_DEF_PARY 'N'
#define SER_DEF_DATA '8'
#define SER_DEF_STOP '1'
#define SER_DEF_RSIZ 256
#define SER_DEF_TSIZ 256
#define SER_DEF_DTR '1'
#define SER_DEF_RTS 0
#define SER_DEF_BRK '0'
/****************************************************************************/
/* low-level constants.. */
/****************************************************************************/
#define MSKVAL(a,m,v) ( ((a)&(~(m))) | ((v)&(m)) )
#define SER_COM1INT 0x0C
#define SER_COM2INT 0x0B
#define SER_COM3INT 0x0D
#define SER_COM4INT 0x0D
#define SER_COM1IMR 0x10 /*IMR bit for intr 0x0C, above*/
#define SER_COM2IMR 0x08
#define SER_COM3IMR 0x20
#define SER_COM4IMR 0x20
#define SER_BAUD_MSK 0x01FF
#define SER_BAUD_300 0x0180
#define SER_BAUD_600 0x00C0
#define SER_BAUD_1200 0x0060
#define SER_BAUD_2400 0x0030
#define SER_BAUD_4800 0x0018
#define SER_BAUD_9600 0x000C
#define SER_BAUD_19200 0x0006
#define IOP_PIC 0x0020 /*programmable interrupt controller*/
#define IOP_IMR 0x0021 /*interrupt mask register I/O port*/
#define SIO_RBR 0x00 /*receiver buffer register, DLAB=0*/
#define SIO_THR 0x00 /*transmit hold register, DLAB=0*/
#define SIO_IER 0x01 /*interrupt enable register*/
#define SIO_ERBFI 0x01 /*enable received data available int*/
#define SIO_ETBEI 0x02 /*enable transmitter hold register empty int*/
#define SIO_ELSI 0x04 /*enable receiver line status int*/
#define SIO_EDSSI 0x08 /*enable modem status interrupt*/
#define SIO_IIR 0x02 /*interrupt identification register*/
#define SIO_INTPEND 0x01 /*0 if interrupt pending*/
#define SIO_INTID 0x06 /*interrupt ID bits*/
#define SIO_INTRX 0x02 /*receive interrupt*/
#define SIO_INTTX 0x04 /*transmit hold register empty int*/
#define SIO_LCR 0x03 /*line control register*/
#define SIO_WLS 0x03 /*word length select bits: */
#define SIO_WLS0 0x01 /*WLS bit 0 00-5 01-6 */
#define SIO_WLS1 0x02 /*WLS bit 1 10-7 11-8 */
#define SIO_DAT 0x03 /*more convenient names..*/
#define SIO_DAT5 0x00
#define SIO_DAT6 0x01
#define SIO_DAT7 0x02
#define SIO_DAT8 0x03
#define SIO_STB 0x04 /*stop bits: 0-1 1-2*/
#define SIO_STB1 0x00
#define SIO_STB2 0x04
#define SIO_PEN 0x08 /*parity enable: 0-off 1-on*/
#define SIO_EPS 0x10 /*even parity select: 0-odd 1-even*/
#define SIO_PAR 0x18
#define SIO_PARN 0x00
#define SIO_PARO 0x10
#define SIO_PARE 0x18
#define SIO_SPAR 0x20 /*stick parity*/
#define SIO_SBRK 0x40 /*set break: 0-normal 1-set break*/
#define SIO_DLAB 0x80 /*divisor latch access bit*/
#define SIO_MCR 0x04 /*modem control register*/
#define SIO_DTR 0x01 /*data terminal ready, inverted*/
#define SIO_RTS 0x02 /*request to send, inverted*/
#define SIO_OUT1 0x04 /*user output 1, inverted*/
#define SIO_OUT2 0x08 /*user output 2, inverted*/
#define SIO_EINTS 0x08 /*OUT2=enable interrupts*/
#define SIO_LPBK 0x10 /*loopback of TX to RX*/
#define SIO_LSR 0x05 /*line status register*/
#define SIO_DR 0x01 /*data ready when 1*/
#define SIO_OR 0x02 /*overrun error, bits 1-4 can gen ints*/
#define SIO_PE 0x04 /*parity error*/
#define SIO_FE 0x08 /*framing error (RX stop bit bad)*/
#define SIO_BI 0x10 /*break interrupt (RX was BREAK)*/
#define SIO_THRE 0x20 /*transmitter hold register empty*/
#define SIO_TSRE 0x40 /*transmitter shift register empty*/
#define SIO_MSR 0x06 /*modem status register*/
#define SIO_DCTS 0x01 /*delta clear to send, CTS changed*/
#define SIO_DDSR 0x02 /*delta data set ready*/
#define SIO_TERI 0x04 /*trailing edge ring indicator*/
#define SIO_DRLSD 0x08 /*delta receive line signal detected*/
#define SIO_CTS 0x10 /*clear to send*/
#define SIO_DSR 0x20 /*data set ready*/
#define SIO_RI 0x40 /*ring indicator*/
#define SIO_RLSD 0x80 /*received line signal detected*/
#define SIO_DLL 0x00 /*divisor latch LSB, when DLAB=1*/
#define SIO_DLM 0x01 /*divisor latch MSB, when DLAB=1*/
/****************************************************************************/
/* PORT structure definition... */
/****************************************************************************/
typedef struct {
WORD com; /*COM port being used*/
BUFF *rx; /*receive buffer*/
BUFF *tx; /*transmit buffer*/
void interrupt (*serint)(void); /*receive interrupt vector*/
void interrupt (*serold)(void); /*old vector, replaced by rxint*/
WORD intnum; /*receive interrupt number*/
WORD intimr; /*IMR bit to disable ints*/
} PORT;
/****************************************************************************/
/* function prototypes.. */
/****************************************************************************/
int serbad(PORT *p); /*returns 1 if pointer is not good*/
int sersetdtr(PORT *p,int dtr); /*sets DTR (Data Terminal Ready)*/
int sersetrts(PORT *p,int rts); /*sets RTS (Request To Send)*/
int sersetbrk(PORT *p,int brk); /*sets the Break control line*/
int sergetdtr(PORT *p); /*gets DTR setting*/
int sergetrts(PORT *p); /*gets RTS setting*/
int sergetbrk(PORT *p); /*gets Break setting*/
int sergetcts(PORT *p); /*gets CTS (Clear To Send) input*/
int sergetdsr(PORT *p); /*gets DSR (Data Set Ready) input*/
int sersetbaud(PORT *p,int b); /*sets baud rate*/
int sersetpary(PORT *p,char val); /*sets parity, 'N', 'O', or 'E'*/
int sersetdata(PORT *p,char val); /*sets data bits, '5' - '8'*/
int sersetstop(PORT *p,char val); /*sets stop bits, '1' or '2'*/
int sergetbaud(PORT *p); /*gets baud rate*/
char sergetpary(PORT *p); /*gets parity*/
char sergetdata(PORT *p); /*gets data bits*/
char sergetstop(PORT *p); /*gets stop bits*/
PORT *seropen(char *cfg,...); /*opens serial port, returns handle*/
int serclose(PORT *p); /*closes serial port*/
int serputc(PORT *p,int c); /*puts out single character*/
int serputs(PORT *p,char *s); /*puts out NUL-terminated string*/
int serputns(PORT *p,char *s,int n); /*puts out string of n characters*/
int serprintf(PORT *p,char *fmt,...); /*does printf() to transmit buffer*/
int sergetc(PORT *p); /*gets one character from port*/
int sergets(PORT *p,char *s,int n); /*gets '\n'-terminated string*/
int sersetcfg(PORT *p,char *cfg,...); /*sets configuration like seropen()*/
char *sergetcfg(PORT *p,char *s,int n); /*gets readable config string, ret s*/
char *sererrmsg(int ec); /*error message for sererr code*/
extern volatile PORT serp[MAXPORTS]; /*serial port structures*/
extern int sererr; /*error condition indicator*/
extern char *sercomstr[8]; /*names of the COM ports*/
/*=== Sample progrm ========================================================*/
/* */
/* #include <stdio.h> */
/* #include "serutil.h" */
/* */
/* #define BUFFSIZE 1024 */
/* */
/* PORT *p; /.handle, pointer to serial port./ */
/* */
/* void main(void) /.loopback test program./ */
/* { */
/* int c; /.keyboard character./ */
/* */
/* p=seropen("c%d b2400 pNd8s1 r%d t%d",COM1,BUFFSIZE,BUFFSIZE); */
/* if (p==NULL) { /.check that port initialized ok./ */
/* printf("Error on open: %s\n",sererrmsg(sererr)); */
/* return; */
/* } /.if error./ */
/* */
/* printf("Connect lines 2 and 3 of COM1 for a loopback test.\n"); */
/* printf("Press <ESC> to quit.\n"); */
/* while (1) { */
/* while (kbhit()) { /.while keyboard input.../ */
/* c=getch(); /...get input./ */
/* if (c==0x1B) { /...if <ESC>.../ */
/* serclose(p); /.....close port./ */
/* return; /.....quit./ */
/* } /.if <ESC>./ */
/* serputc(p,c); /...else put char out serial port./ */
/* } /.while kb input./ */
/* while (!bufempty(p->rx)) /.if char received./ */
/* putchar(sergetc(p)); /...echo it to screen./ */
/* } /.while forever./ */
/* } /.main./ */
/* */
/*==========================================================================*/
#endif /*if not already included*/