在VC6.0环境下编写串口程序,该如何处理
在VC6.0环境下编写串口程序
在VC下不能调用dos.h和bois.h 甚是疑惑
如果我要写串口通信程序又不用到MFC的类,我该用什么函数呢?
下面是我写的程序 但在vc不能编 曾尝试过用TC编译但它说头文件都错了 极度郁闷
请各位帮帮忙!
# include "dos.h "
# include "stdlib.h "
# include "stdio.h "
# define PORT 1
void port_init(int port,unsigned char code);
void sport(int port,char c);
unsigned int check_stat(int port);
unsigned char rport(int port);
void send_file(char *fname);
void send_file_name(char *f);
void wait(int port);
void port_init(int port,unsigned char code)
{
union REGS r;
r.x.dx=port; //define PORT
r.h.ah=0; //初始化端口功能
r.h.al=code; //初始参数
int86(0x14,&r,&r); //调用中断
}
void sport(int port,char c)
{
union REGS r;
r.x.dx=port;
r.h.al=c; //要发送的字符
r.h.ah=1; //初始化端口功能
int86(0x14,&r,&r);
if(r.h.ah&128) //发送不成功 则打印错误信息
{
printf( "Send error detected in serial port ");
exit(1);
}
}
//检测端口状态函数
unsigned int check_stat(int port)
{
union REGS r;
r.x.dx=port;
r.h.ah=3; //初始化端口功能
int86(0x14,&r,&r);
return r.x.ax;
}
//接收字符函数
unsigned char rport(int port)
{
union REGS r;
//等待一个字符,若“数据就绪”位为0则等待 为1 或按任意键推出
while(!(check_stat(port)&256))
{
if(kbhit())
gerch();
exit(1);
}
r.x.dx=port;
r.h.ah=2; //初始化端口功能
int86(0x14,&r,&r);
if(r.h.ah&128)
{
printf( "read error ");
}
return r.h.al;
}
/////////////////////////////////////////////
//发送文件函数
void send_file(char *fname)
{
FILE *fp;
char ch;
int i;
union
{
char c[sizeoff(long)];
long count;
}cnt;
if(!(fp=fopen(fname, "rb ")))
{
printf( "cannot open input file\n ");
exit(1);
}
//统计文件字节数
cnt.count=filesize(fp);
printf( "file size:%ld\n ",cnt.count);
send_file_name(fname); //发送文件名
//发送字节
for(i=0;i <sizeoff(long);i++)
{
sport(PORT,cnt.c[i]);
wait(PORT);
}
i=0;
do
{
ch=getc(fp);
if(ferror(fp))
{
printf( "error reading input file.\n ");
break;
}
//等待直到接收方就绪
if(!feof(fp))
{
sport(PORT,ch);
wait(PORT);
}
i++;
if(i==1024)
{
printf( ". ");
i=0;
}
}while(!feof(fp));
fclose(fp);
}
void send_file_name(char *f)
{
printf( "trasmitter waitting...\n ");
do
{
sport(PORT, '? '); //发送询问“?”
在VC下不能调用dos.h和bois.h 甚是疑惑
如果我要写串口通信程序又不用到MFC的类,我该用什么函数呢?
下面是我写的程序 但在vc不能编 曾尝试过用TC编译但它说头文件都错了 极度郁闷
请各位帮帮忙!
# include "dos.h "
# include "stdlib.h "
# include "stdio.h "
# define PORT 1
void port_init(int port,unsigned char code);
void sport(int port,char c);
unsigned int check_stat(int port);
unsigned char rport(int port);
void send_file(char *fname);
void send_file_name(char *f);
void wait(int port);
void port_init(int port,unsigned char code)
{
union REGS r;
r.x.dx=port; //define PORT
r.h.ah=0; //初始化端口功能
r.h.al=code; //初始参数
int86(0x14,&r,&r); //调用中断
}
void sport(int port,char c)
{
union REGS r;
r.x.dx=port;
r.h.al=c; //要发送的字符
r.h.ah=1; //初始化端口功能
int86(0x14,&r,&r);
if(r.h.ah&128) //发送不成功 则打印错误信息
{
printf( "Send error detected in serial port ");
exit(1);
}
}
//检测端口状态函数
unsigned int check_stat(int port)
{
union REGS r;
r.x.dx=port;
r.h.ah=3; //初始化端口功能
int86(0x14,&r,&r);
return r.x.ax;
}
//接收字符函数
unsigned char rport(int port)
{
union REGS r;
//等待一个字符,若“数据就绪”位为0则等待 为1 或按任意键推出
while(!(check_stat(port)&256))
{
if(kbhit())
gerch();
exit(1);
}
r.x.dx=port;
r.h.ah=2; //初始化端口功能
int86(0x14,&r,&r);
if(r.h.ah&128)
{
printf( "read error ");
}
return r.h.al;
}
/////////////////////////////////////////////
//发送文件函数
void send_file(char *fname)
{
FILE *fp;
char ch;
int i;
union
{
char c[sizeoff(long)];
long count;
}cnt;
if(!(fp=fopen(fname, "rb ")))
{
printf( "cannot open input file\n ");
exit(1);
}
//统计文件字节数
cnt.count=filesize(fp);
printf( "file size:%ld\n ",cnt.count);
send_file_name(fname); //发送文件名
//发送字节
for(i=0;i <sizeoff(long);i++)
{
sport(PORT,cnt.c[i]);
wait(PORT);
}
i=0;
do
{
ch=getc(fp);
if(ferror(fp))
{
printf( "error reading input file.\n ");
break;
}
//等待直到接收方就绪
if(!feof(fp))
{
sport(PORT,ch);
wait(PORT);
}
i++;
if(i==1024)
{
printf( ". ");
i=0;
}
}while(!feof(fp));
fclose(fp);
}
void send_file_name(char *f)
{
printf( "trasmitter waitting...\n ");
do
{
sport(PORT, '? '); //发送询问“?”