串口数据发送
小弟刚接触编程,求大侠指点。以下是龚老师一段很经典的串口程序,原文最后输出的是%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();
}