Programming 版 (精华区)

发信人: Mice (鼠儿), 信区: Programming
标  题:  DOS下的串口通信编程----(2)
发信站: 紫丁香 (Mon Sep 15 15:58:03 1997)


//serial.h
#if !defined(_SERIAL_H)
#define _SERIAL_H

#include <dos.h>

#define TIMEOUT 0X8000  /*TIME OUT*/
#define EMPTYSHIFT      0X4000  /*      */
#define EMPTYDATA       0X2000  /*      */
#define BREAKDET        0X1000  /*      */
#define FRAMERR 0X0800  /*      */
#define PARITYERR       0X0400  /*      */
#define OVERERR 0X0200  /*      */
#define RECEIVERDY      0X0100  /*      */

#define B110            0x00            /* Set baudrate with bios */
#define B150            0x20
#define B300            0x40
#define B600            0x60
#define B1200           0x80
#define B2400           0xA0
#define B4800           0xC0
#define B9600           0xE0

#define BAUD110         0x0417  /*Set baudrate by direct port operation*/
#define BAUD150         0x0300
#define BAUD300         0x0180
#define BAUD600         0x00C0
#define BAUD1200        0x0060
#define BAUD1800        0x0040
#define BAUD2400        0x0030
#define BAUD3600        0x0020
#define BAUD4800        0x0018
#define BAUD7200        0x0010
#define BAUD9600        0x000C
#define BAUD19200       0x0006
#define BAUD28800       0x0004
#define BAUD38400       0x0003
#define NONEPAR 0x00
#define ODDPAR  0x08
#define EVENPAR 0x18

#define STOPBIT1        0x00
#define STOPBIT2        0x04
#define DATABIT7        0x02
#define DATABIT8        0x03

#define MAXPORT 4
#define MAXBAUD 12
#define MAXDATA 4
#define MAXSTOP 2
#define MAXPARITY       5

/*      UART REGISTER  OFFSET ADDRESS   */
#define RXD             0
#define TXD             0
#define LSB             0
#define MSB             1
#define IER             1
#define IIR             2
#define LCR             3
#define MCR             4
#define LSR             5
#define MSR             6

#define INVALIDPORT     -1
#define WAITCOUNT       8000

class ComPort {
        private:
                int selectedport;
                unsigned basereg;
        public:
                unsigned errcode;
                ComPort(int port) {
                        selectedport=port;
                        basereg=peek(0x40,port*2);
                        errcode=0;
                }
                void selectPort(int port){
                        selectedport=port;
                        basereg=peek(0x40,port*2);
              errcode=0;
                }
                unsigned char read(int reg){
                        return (inportb(basereg+reg));
                }
                void write(int reg,unsigned char  val){
                                outportb(basereg+reg,val);
                }
                unsigned state(void){
                        unsigned char lsr,msr;
                        unsigned st;
                        lsr=read(LSR);
                        msr=read(MSR);
                        st=((unsigned)lsr<<8)+(unsigned)msr;
                        return st;
                }
                unsigned baudReg(void){
                        unsigned char lcr,lsb,msb;
                        unsigned msblsb;
                        lcr=read(LCR);
                        write(LCR,0x80);
                        lsb=read(LSB);
 msb=read(MSB);
                        write(LCR,lcr);
                        msblsb=((unsigned)msb<<8)+(unsigned)lsb;
                        return msblsb;
                }
                void baudReg(unsigned baud){
                        unsigned char lsb,msb,lcr;
                        msb=(baud>>8)&0xff;
                        lsb=baud&0xff;
                        lcr=read(LCR);
                        write(LCR,0x80);
                        write(LSB,lsb);
                        write(MSB,msb);
                        write(LCR,lcr);
                }
                unsigned long baudRate()                {
                        return 1843200L/baudReg()/16;
                }
                void frame(unsigned char form){
                        write(LCR,form);
                }
                unsigned char read();
              void write(char chr);
                void write(char* str);
};

--




       我是一只好老鼠

※ 来源:.紫丁香 pclinux.hit.edu.cn.[FROM: 202.118.226.74]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.916毫秒