首页 > 用户发贴区 > 编程问题提问区 > 请高手改个程序
2009
02-20

是个从com1读取数据的程序,现在读出来的数据是在屏幕上不停的循环,现在我想从com2读数,读出一组数据保存在一个TXT文件里,保存后程序就停止。也就是说运行一次程序保存一组数据就停止。不用在屏幕上显示了。谢谢谢谢兄弟!我请兄弟吃饭!下面是源码,在dos下用
#include <stdio.h>
#include <dos.h>
#include <conio.h>


#define BUFFLEN 1024


void InitCOM(); 
void OpenPort(); 
void ClosePort();
void interrupt(*asyncoldvect)(…);


unsigned char Buffer[BUFFLEN];
int buffin=0;
int buffout=0;
//unsigned char ch;


void OpenPort()
{
    unsigned char ucTemp;
    InitCOM(); 


        asyncoldvect=getvect(0x0c);
    disable();      
    inportb(0x3f8);
    inportb(0x3fe);
    inportb(0x3fb);
    inportb(0x3fa);
    outportb(0x3fc,0×08|0x0b);
    outportb(0x3f9,0×01);
    ucTemp=inportb(0×21)&0xef;
    outportb(0×21,ucTemp);
    setvect(0x0c,asyncint);
    enable();       }


void interrupt far asyncint(…)
{
    //unsigned char ch;
    Buffer[buffin++] = inportb(0x3f8);
    if (buffin >= BUFFLEN) 
        buffin=0;        
    outportb(0×20,0×20);
}


void ClosePort(void) {
    disable();
    outportb(0x3f9,0×00);
    outportb(0x3fc,0×00);
    outportb(0×21,inportb(0×21)&0×10);
    enable();
    setvect(0x0c,asyncoldvect);
}


void InitCOM()


    outportb(0x3fb,0×80); 
   
       outportb(0x3f8,0x0C); 
    outportb(0x3f9,0×00);


   
   
    outportb(0x3fb,0×03);  
    outportb(0x3fc,0×08|0x0b);
    outportb(0x3f9,0×01);
}



unsigned char read_char(void)
{
    unsigned unch;
    if(buffout != buffin)
    {
        unch = Buffer[buffout];
        buffout++;
        if(buffout >= BUFFLEN)
            buffout=0;
        return(unch);
    }
    else
        return(0xff);
}


void main()
{
    unsigned char unChar;
    short bExit_Flag=0;


    OpenPort();


    fprintf(stdout, “\n\nReady to Receive DATA\n”
            “press [ESC] to quit…\n\n”);


    do {
        if (kbhit())
        {
            unChar=getch();
            /* Look for an ESC key */
            switch (unChar)
            {
            case 0x1B:  
                bExit_Flag = 1;  /* Exit program */
                break;
            //You may want to handle other keys here
            }
        }
        unChar = read_char(); 
        if (unChar != 0xff)
        {
            fprintf(stdout,”%c”,unChar);
        }
    } while (!bExit_Flag);


    ClosePort();


 


请高手改个程序》有 2 条评论

  1. wgmwgm 说:

    对啦!忘告诉在Turbo C下编写的

  2. wgmwgm 说:

    请大家帮个忙吧

留下一个回复