A C serial port communication program

xiaoxiao2021-03-06  118

#include #include #include #include

#define com232 0x2f8 # define comint 0x0b

#define maxbuflen 500 # Define port8259 0x20 # define eofint 0x20

Static int CHAR Maskb; Static Unsigned Char Buffer [Maxbufflen]; Static Int Charsinbuf, Circin, Circout

Static void (Interrupt Far * Oldasyncint) (); Static Void Interrupt Far Asyncint (Void);

Void init_com (int COMPORTADDR, Unsigned CHAR INTVECTNUM, INT BAUD, UNSIGNED CHAR DATA, UNSIGNED CHAR Stop, Unsigned Char Parity) {Unsigned Char High, Low; INT F

COMPORTADDR = COMPORTDR; intVectnum = intVectnum;

Charsinbuf = 0; Circin = 0; Circout = 0;

/ * set baud rate * / f = (baud / 100); f = 1152 / f; high = f / 256; low = f-high * 256; OUTP (COMPORTDR 3, 0X80); OUTP (COMPORTADDR, LOW) OUTP (COMPORTADDR 1, HIGH);

/ * SET DATA BITS, Stop Bits, And Parity * / Data = (STOP-1) * 4); if (Parity == 2) DATA = DATA | 0x18; Else IF (Parity == 1) DATA = DATA | 0x8; OUTP (COMPORTADDR 3, DATA);

/ * SET MCR (DTR, RTS = 0, OUT1, OUT2 = 1) * / OUTP (COMPORTADDR 4, 0X0A);

/ * SET INPUT DATA Ready Interrupt Mode * / OUTP (COMPORTADDR 1,0x01);

/ * set 0c interrupt vector * / disable (); oldasyncint = getVect (intVectnum); setVect (intVectnum, asyncint); enable ();

/ * Open Interrupt * / Maskb = INP (port8259 1); if (intVectnum == 0x0c) OUTP (port8259 1, maskb & 0xef); / * int4 * / else outp (port8259 1, maskb & 0xf7); / * int3 * /}

Static void Interrupt Far asyncint (void) {disable ();

if (CharsInBuf

FLAG = -1;

IF (Charsinbuf> 0) {(* char) = buffer [Circout]; if (Circout

INT sentchar (unsigned char char) {= ((INP (COMPORTADDR 5) & 0x20) == 0) Return -1; OUTP (Comportaddr, char); return 0;}

Main () {Int i, c; unsigned char in

INIT_COM (COM232, COMINT, 1200, 8, 1, 0); While (1) {IF (kbhit ()) {IF ((Inchar = getCH ()) == 27) Break; else while (SENDCHAR)) ;} If (getcharinbuf (& inchar) == 0) / * Displaying Input char * / printf ("% c", inchar);} restore ();}

转载请注明原文地址:https://www.9cbs.com/read-101821.html

New Post(0)