±±¾©Ê¯ÓÍ»¯¹¤Ñ§Ôº2026ÄêÑо¿ÉúÕÐÉú½ÓÊÕµ÷¼Á¹«¸æ
²é¿´: 351  |  »Ø¸´: 0

AH_Shyun

гæ (³õÈëÎÄ̳)

[½»Á÷] ¡¾ÇóÖú¡¿Çó´ïÈ˽â¶ÁÒ»´®¿Úµ÷ÊÔ³ÌÐò

ÏÂÃæÊÇÀÏʦ¸øµÄÒ»¸öǶÈëʽϵͳÖд®¿Úµ÷ÊÔ³ÌÐò£¬µ«ÊÇÎÒ¿´²»´ó¶®£¬Çó¸ßÊÖ½«³ÌÐò»®·Ö¹¦ÄÜÄ£¿é£¬²¢¶Ôÿ¸öÄ£¿éÖеijÌÐò×ö¾¡Á¿ÏêϸһµãµÄ×¢ÊÍ,Óнð±Ò½±Àø£¬×ÃÇé¼ÓÉÍ
#include "serial.h"
#include
#include

//long int test_gnd_status=0;
//long int test_rem_status=0;

extern        void        SBC_SetBitInIMR  (unsigned char idx);
extern        void        SBC_ClearBitInIMR(unsigned char idx);
unsigned char   Status;
//Define Register ID
#define        THR                0                //transmit
#define        RDR                0                //receive
#define        BAUD_L        0                //baud rate low
#define        BAUD_H        1                //baud rate high
#define        IER                1                //Interrupt enable register
#define        IIR                2                //Interrupt index reg. (read)
#define        LCR                3                //Line control register(write)
#define        MCR                4                //Modem control reg. (write)
#define        LSR                5                //Line status(read)
#define        MSR                6                //Modem status(read)

struct        CPARAM        {
        unsigned                                base;
        volatile unsigned char        Rxhead,Rxtail,RxBuf[256];
        volatile unsigned char        Txhead,Txtail,TxBuf[256];
} SIO[8];

static        int                ComSum = 0;                //Serial ports total numbers
static        int                ISR_ComSum = 0;        //ISR vector numbers
static        int                id[4];
static        void _interrupt _far isr_com1(void);
static        void _interrupt _far isr_com2(void);
static        void _interrupt _far isr_com3(void);
static        void _interrupt _far isr_com4(void);
//
//int        Com4_Open( int com, unsigned baud, unsigned char mode )
//
//Return: 0=success; -1=fail
//
int                Com_Open(int base, int baud, unsigned char mode, char isr_num)
{
        unsigned char        B, LSB, MSB;
        int                                com;
       
//--[1]--Set Baud Rate
        B = _inp(base+LCR);        B = B | 0x80;        //Set B7
        _outp(base+LCR, B );
       
        LSB = baud & 0x00FF;        MSB = (baud & 0xFF00) / 256;
        _outp( base+BAUD_L, LSB );
        _outp( base+BAUD_H, MSB );
       
//--[2]--Set Line Control Register (LCR)
        B = inp( base+LCR );        B = B & 0x7F;                //Clear B7
        _outp( base+LCR, B );
    _outp( base+LCR, mode );
   
//--[3]--Set Interrupt Enable Register (IER)
        if (isr_num<0) {
            _outp(base+IER, 0x0); //¹ØÖжÏ
            _outp(base+MCR, 0x0); //²»ÓõÄMODEM¿ØÖƼĴæÆ÷ÇåÁã
    }
    else {
            _outp( base+IER, 0x1 );        //Rx Enabled
                _outp( base+MCR, 0x0B );        //ISR enabled
        }

//--[4]--¼ì²éportÊÇ·ñ´æÔÚ
        B = _inp(base+LCR);
        if (B!=mode)        com = -1;
        else {
                com = ComSum ++;                SIO[com].base   = base;
                SIO[com].Txhead = 0;        SIO[com].Txtail = 0;
                SIO[com].Rxhead = 0;        SIO[com].Rxtail = 0;
                if (isr_num>=0) {
                        id[ISR_ComSum] = com;
                        SBC_SetBitInIMR(isr_num);
                        switch (ISR_ComSum) {
                                case 0:        if (isr_num<8)        _dos_setvect(isr_num+8,    isr_com1);
                                                else                        _dos_setvect(isr_num+0x68, isr_com1);
                                                break;
                                case 1:        if (isr_num<8)        _dos_setvect(isr_num+8,    isr_com2);
                                                else                        _dos_setvect(isr_num+0x68, isr_com2);
                                                break;
                                case 2:        if (isr_num<8)        _dos_setvect(isr_num+8,    isr_com3);
                                                else                        _dos_setvect(isr_num+0x68, isr_com3);
                                                break;
                                case 3:        if (isr_num<8)        _dos_setvect(isr_num+8,    isr_com4);
                                                else                        _dos_setvect(isr_num+0x68, isr_com4);
                                                break;
                        }
                        SBC_ClearBitInIMR(isr_num);
                        ISR_ComSum ++;
                }
        }
        //printf("ComSum=%d ISR_ComSum=%d com=%d\n", ComSum,ISR_ComSum,com);
        //printf("SIO[com].base=%x\n", SIO[com].base);
        return (com);
}


int                Com_IsRxOK(int com)
{
        unsigned char        B;
       
        if ((com<0) || (com>(ComSum-1)))        return(0);
       
        B = _inp( SIO[com].base+LSR );
        return( B & 0x01 );
}

int         Com_IsTxOK(int com)
{
        unsigned char        B;
       
        if ((com<0) || (com>(ComSum-1)))        return(0);
       
        B = _inp( SIO[com].base+LSR ); Status = B;
        return ((B&0x20)/0x20);
}

//
//int        Com4_LenRx(int com)
//
//Return: -1=fail; other=success
//
/*int         Com_LenRx(int com)
{
        int        len;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);
       
        if ( SIO[com].Rxhead >= SIO[com].Rxtail )
                len = SIO[com].Rxhead - SIO[com].Rxtail;
        else
                len = 256 + SIO[com].Rxhead - SIO[com].Rxtail;
        return (len);
}*/

BYTE         Com_LenRx(int com)
{
        BYTE        len;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);
       
        len = SIO[com].Rxhead - SIO[com].Rxtail;
        return (len);
}

/*int         Com_LenTx(int com)
{
        int        len;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);

        if ( SIO[com].Txhead >= SIO[com].Txtail )
                len = SIO[com].Txhead - SIO[com].Txtail;
        else
                len = 256 + SIO[com].Txhead - SIO[com].Txtail;
        return (len);
}*/

BYTE         Com_LenTx(int com)
{
        BYTE        len;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);

        len = SIO[com].Txhead - SIO[com].Txtail;
        return (len);
}

int         Com_Out(int com, unsigned char *abyte, int nbyte)
{
        int                idx, len, num_out;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);

        len = Com_LenTx(com);
        if ((len+nbyte) <= 256)        num_out = nbyte;
        else                                        num_out = 256 - len;

        for (idx=0; idx                 SIO[com].TxBuf[SIO[com].Txhead++] = *(abyte+idx);

        if (Com_LenTx(com)) {
            _outp( SIO[com].base+IER, 0x3 );        //Rx | Tx Enabled
                for (idx=0;idx<500;idx++);                        //delay for settle down
        }
    return(num_out);
}

               
int          Com_In(int com,  unsigned char *abyte, int nbyte)
{
        int                idx, len, num_in;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);

        len = Com_LenRx(com);
        if (len >= nbyte)        num_in = nbyte;
        else                                num_in = len;

        for (idx=0; idx                  *(abyte+idx) = SIO[com].RxBuf[SIO[com].Rxtail++];
        return (num_in);
}

unsigned char        Com_GetChar(int com)
{
        if ((com<0) || (com>(ComSum-1)))        return (0);
       
        return (_inp(SIO[com].base+RDR));  
}

static        void _interrupt _far isr_com1(void)
{
        static        unsigned char         status;
    static        int        com;
       
        com = id[0];
        status = _inp(SIO[com].base+IIR);
        //test_gnd_status++;
/////////////////////////////////////////////////////////////
       
//        SIO[com].TxBuf[SIO[com].Txtail]=SIO[com].TxBuf[SIO[com].Txhead];
       
//        SIO[com].RxBuf[SIO[com].Rxhead++] = Com_GetChar(com);
//        _outp(SIO[com].base+IER, 0x1); //[make Tx/Rx disabled]
/////////////////////////////////////////////////////////////                               

        do {
                switch (status & 0x06) {
                        case 4:        //[Rx ready]
                                while (Com_IsRxOK(com))
                                        SIO[com].RxBuf[SIO[com].Rxhead++] = Com_GetChar(com);
                                break;
                        case 2:        //[Tx ready]
                                while (Com_IsTxOK(com)) {
                                        _outp(SIO[com].base, SIO[com].TxBuf[SIO[com].Txtail++]);
                                        if (!Com_LenTx(com))         _outp(SIO[com].base+IER, 0x1);
                                                                                        //[make Tx disabled]
                                }
                                break;
                }
                     status = _inp(SIO[com].base+IIR);
    } while ((status & 0x1)==0);
        _outp(0x20,0x20);
}

static        void _interrupt _far isr_com2(void)
{
        static        unsigned char         status;
    static        int        com;
       
        //test_rem_status++;
       
        com = id[1];
        status = _inp(SIO[com].base+IIR);
        do {
                switch (status & 0x06) {
                        case 4:        //[Rx ready]
                                while (Com_IsRxOK(com))
                                        SIO[com].RxBuf[SIO[com].Rxhead++] = Com_GetChar(com);
                                break;
                        case 2:        //[Tx ready]
                                while (Com_IsTxOK(com)) {
                                        _outp(SIO[com].base, SIO[com].TxBuf[SIO[com].Txtail++]);
                                        if (!Com_LenTx(com))         _outp(SIO[com].base+IER, 0x1);
                                                                                        //[make Tx disabled]
                                }
                                break;
                }
                     status = _inp(SIO[com].base+IIR);
    } while ((status & 0x1)==0);
        _outp(0x20,0x20);
}

static        void _interrupt _far isr_com3(void)
{
        static        unsigned char         status;
    static        int        com;
       
        com = id[2];
        status = _inp(SIO[com].base+IIR);
        do {
                switch (status & 0x06) {
                        case 4:        //[Rx ready]
                                while (Com_IsRxOK(com))
                                        SIO[com].RxBuf[SIO[com].Rxhead++] = Com_GetChar(com);
                                break;
                        case 2:        //[Tx ready]
                                while (Com_IsTxOK(com)) {
                                        _outp(SIO[com].base, SIO[com].TxBuf[SIO[com].Txtail++]);
                                        if (!Com_LenTx(com))         _outp(SIO[com].base+IER, 0x1);
                                                                                        //[make Tx disabled]
                                }
                                break;
                }
                     status = _inp(SIO[com].base+IIR);
    } while ((status & 0x1)==0);
        _outp(0x20,0x20);
}

static        void _interrupt _far isr_com4(void)
{
        static        unsigned char         status;
    static        int        com;
       
        com = id[3];
        status = _inp(SIO[com].base+IIR);
        do {
                switch (status & 0x06) {
                        case 4:        //[Rx ready]
                                while (Com_IsRxOK(com))
                                        SIO[com].RxBuf[SIO[com].Rxhead++] = Com_GetChar(com);
                                break;
                        case 2:        //[Tx ready]
                                while (Com_IsTxOK(com)) {
                                        _outp(SIO[com].base, SIO[com].TxBuf[SIO[com].Txtail++]);
                                        if (!Com_LenTx(com))         _outp(SIO[com].base+IER, 0x1);
                                                                                        //[make Tx disabled]
                                }
                                break;
                }
                     status = _inp(SIO[com].base+IIR);
    } while ((status & 0x1)==0);
        _outp(0x20,0x20);
}

//
//void    Com4_NS550(int com, int trigger)
//
void    Com_NS550(int com, int trigger)
{
        if ((com<0) || (com>(ComSum-1)))        return;
        _outp(SIO[com].base+2, trigger+1);
}

void        Com_SetRTS(int com)
{
        unsigned char        B;
       
        if ((com<0) || (com>(ComSum-1)))        return;
       
        B = _inp(SIO[com].base+MCR);
        B = B | 0x02;
        _outp(SIO[com].base+MCR, B);
                       
}

void        Com_ClearRTS(int com)
{
        unsigned char        B;

        if ((com<0) || (com>(ComSum-1)))        return;
       
        B = _inp(SIO[com].base+MCR);
        B = B & 0xFD;
        _outp(SIO[com].base+MCR, B);
}

void         Com_Refresh(int com)
{
        if ((com<0) || (com>(ComSum-1)))        return;

        while (Com_IsRxOK(com))
                SIO[com].RxBuf[SIO[com].Rxhead++] = Com_GetChar(com);
}

int         Com_Out2(int com, unsigned char *abyte, int nbyte)
{
        int                idx;
       
        if ((com<0) || (com>(ComSum-1)))        return (0);

        for (idx=0; idx                 //while ( (_inp(SIO[com].base+LSR) & 0x20)==0 )
                while (!Com_IsTxOK(com));        //waiting for Tx Ready
                _outp( SIO[com].base, *(abyte+idx) );
        }
        return (nbyte);
}

void        Com_PutChar(int com, BYTE Char )
{
        if ((com<0) || (com>(ComSum-1)))        return;
       
        _outp(SIO[com].base+THR, Char);
}

[ Last edited by AH_Shyun on 2010-10-17 at 16:43 ]
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ AH_Shyun µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] ¿¼Ñе÷¼Á +3 Draa 2026-04-03 3/150 2026-04-03 17:37 by hgwz7468
[¿¼ÑÐ] 321Çóµ÷¼Á +10 ÈÏÕæÇóÉÏѧ 2026-04-02 10/500 2026-04-03 16:17 by lijunpoly
[¿¼ÑÐ] 266·Ö£¬Çó²ÄÁÏÏà¹Ø×¨Òµµ÷¼Á +13 ÍÛºôºßºôºß 2026-03-30 15/750 2026-04-03 15:24 by arrow8852
[¿¼ÑÐ] 085600ר˶²ÄÁÏÓ뻯¹¤348·ÖÇóµ÷¼Á +10 ÉÏѧÀ²£¡ 2026-04-01 11/550 2026-04-03 14:13 by °ÙÁéͯ888
[¿¼ÑÐ] µ÷¼Á +4 asdasdassda 2026-04-03 5/250 2026-04-03 14:02 by °ÙÁéͯ888
[¿¼ÑÐ] ÊýÒ»Ó¢Ò»285Çóµ÷¼Á +7 AZMK 2026-04-03 9/450 2026-04-03 13:03 by ms629
[¿¼ÑÐ] ³õÊԳɼ¨337ÕÒµ÷¼Á +3 ??? ?. ? 2026-04-03 3/150 2026-04-03 11:43 by ÍÁľ˶ʿÕÐÉú
[¿¼ÑÐ] һ־Ըɽ¶«´óѧ»¯Ñ§Ó뻯¹¤Ñ§Ôº²ÄÁÏÓ뻯¹¤×¨Ë¶£¬360·ÖÇóµ÷¼Á +4 ²»Ô¸Í¸Â¶ÐÕÃûµÄË 2026-04-02 4/200 2026-04-03 09:29 by ÒÅÍüÏûʧµÄž™
[¿¼ÑÐ] 366Çóµ÷¼ÁÒ»Ö¾Ô¸¶«±±´óѧ +8 ÔËÆøÀ´µÃÈôÓÐËÆÎ 2026-04-02 8/400 2026-04-02 21:39 by dongzh2009
[¿¼ÑÐ] 270Çóµ÷¼Á +8 С½Üpp 2026-03-31 10/500 2026-04-02 12:57 by yulian1987
[¿¼ÑÐ] ¸÷λÀÏʦºÃ£¬ÎÒµÄһ־ԸΪ±±¾©¿Æ¼¼´óѧ085601²ÄÁÏר˶ +13 Koxui 2026-03-28 13/650 2026-04-02 09:35 by ßÕßÕßÕßÉßÉßÉ
[¿¼ÑÐ] Ò»Ö¾Ô¸Äϲý´óѧ324Çóµ÷¼Á +7 hanamiko 2026-03-30 7/350 2026-04-01 13:22 by JourneyLucky
[¿¼ÑÐ] »¯Ñ§0703 µ÷¼Á 306·Ö Ò»Ö¾Ô¸211 +12 26ÒªÉϰ¶ 2026-03-28 12/600 2026-04-01 11:10 by chemdavid
[˶²©¼ÒÔ°] ²©Ò»±»ËͳöÁªÅà¸Ð¾õ²»ÊÊÓ¦Ôõô°ì +3 È«´åµÄ¹· 2026-03-31 3/150 2026-04-01 10:44 by 328838485
[¿¼ÑÐ] 301Çóµ÷¼Á +8 axibli 2026-04-01 8/400 2026-04-01 09:51 by ÎҵĴ¬Îҵĺ£
[¿¼ÑÐ] ¡¾µ÷¼Á¡¿Ò»Ö¾Ô¸ÏôóÉúÎïÓëÒ½Ò©µ÷¼Á +3 EchoϺÃ× 2026-03-31 3/150 2026-04-01 08:40 by JourneyLucky
[¿¼ÑÐ] Çóµ÷¼Á ÉúÎïѧ 377·Ö +6 zzll03 2026-03-31 6/300 2026-03-31 17:33 by ÌÆãå¶ù
[¿¼ÑÐ] 085404 22408 315·Ö +5 zhuangyan123 2026-03-31 6/300 2026-03-31 13:48 by limeifeng
[¿¼ÑÐ] 276Çóµ÷¼Á +3 ÕԾûª 2026-03-29 3/150 2026-03-31 10:06 by cal0306
[¿¼ÑÐ] 312£¬ÉúÎïѧÇóµ÷¼Á +3 СÒëͬѧabc 2026-03-28 3/150 2026-03-28 15:32 by ÂäÉ˼
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û