标题:串口数据发送
只看楼主
lemonming
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2013-4-6
结帖率:100%
 问题点数:0 回复次数:0 
串口数据发送
小弟刚接触编程,求大侠指点。
一下应该是一段很经典的串口程序了,原文最后输出的是%c,我改成%x了,其他的都没变。结果我用一台电脑输出十六进制字符串:55 AA 00 A6 06 A1 AB 01 12 00 94 00 00 00 5A 5A,得到结果:5500aa00000a600600a100ab00100120000094000aa00a600a105a105a00。求指点。谢谢。
#include <stdio.h>
#include <dos.h>
#include <conio.h>

#define BUFFLEN 1024
int a[32],b[32];

void InitCOM();
void OpenPort();
void ClosePort();

void interrupt far  asyncint();

void interrupt(*asyncoldvect)();
unsigned char read_char();

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



asyncoldvect=getvect(0x0c);
disable();      
inportb(0x3f8);
inportb(0x3fe);
inportb(0x3fb);
inportb(0x3fa);
outportb(0x3fc,0x08|0x0b);
outportb(0x3f9,0x01);
ucTemp=inportb(0x21)&0xef;
outportb(0x21,ucTemp);
setvect(0x0c,asyncint);
enable();      
}



Buffer[buffin++] = inportb(0x3f8);
Buffer[buffin++] = b[buffin++] ;
if (buffin >= BUFFLEN)  
buffin=0;           
outportb(0x20,0x20);
}

void ClosePort(void)
{
disable();
outportb(0x3f9,0x00);
outportb(0x3fc,0x00);
outportb(0x21,inportb(0x21)&0x10);
enable();
setvect(0x0c,asyncoldvect);
}

void InitCOM()
{

outportb(0x3fb,0x80);  

    outportb(0x3f8,0x0C);  
    outportb(0x3f9,0x00);

  
    outportb(0x3fb,0x03);   

    outportb(0x3fc,0x08|0x0b);
    outportb(0x3f9,0x01);
    }
unsigned char read_char(void)
{
unsigned unch;
if(buffout != buffin)
{
unch = Buffer[buffout];
a[buffout]=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();

switch (unChar)
{
case 0x1B:   /*/ESCASCII27*/
bExit_Flag = 1;  /* Exit program */
break;

}
}
unChar = read_char();
if (unChar != 0xff)
{  
fprintf(stdout,"%x",unChar);
}
} while (!bExit_Flag);
ClosePort();
}
搜索更多相关主题的帖子: void 十六进制 include 台电脑 
2013-04-17 15:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-404562-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039939 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved