Y
YangFuHome
Unregistered / Unconfirmed
GUEST, unregistred user!
用TurboPower Async 或者 Spcomm来做
/*文件的编译模式选择compact*/
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <bios.h>
unsigned char *combuf; /*模拟控制器程序内存的缓冲区指针*/
unsigned int asta,aend,par[130];/*asta内存开始地址,aend内存结束地址*/
char *buf1 = "COM1",*buf2 = "COM2";/*PC机串行口的标志*/
unsigned int comp;/*PC机串行口地址的偏移*/
unsigned int Wdey;
long int Sdey;
char dd,fname[20];
main(argc,argv)/*有参数的程序调用*/
int argc;
char *argv[];
{
long int i;
char ch;
disp1();
if(argc!=5) {/*如果参数个数不对,显示提示信息*/
gotoxy(20,10); printf("Command Format:");
gotoxy(20,12); printf("ss2 COM1/COM2 Filename Write_delay Set_delay");
gotoxy(20,14); printf("Write_delay=0--65000");
gotoxy(20,15); printf("Set_delay =0--65000");
gotoxy(40,17); printf("press any key..exit");
goto ee1;/*结束程序*/
}
/****开始处理文件:主要是将*.HEX文件按地址读入到设置的内存缓冲区中,同时计算出开始和结束的页地址*******/
if((combuf=malloc(0xfff7))==NULL)/*设置内存缓冲区*/
{printf("no memery");goto ee1;}/*如果内存缓冲器设置没有成功,结束程序*/
for(i=0;i<=0xfff7;i++)combuf=0xff;/*内存缓冲区全赋值为0xff*/
asta=0x2000;/*设置控制器内存起始地址为0x2000*/
aend=0x21ff;/*设置控制器内存结束地址为0x21ff*/
ch=strlen(argv[2]);/*argv[2]是要下载的文件名*/
for(i=1;i<=ch;i++){fname[i-1]=argv[2][i-1];}
dd=0;/*文件处理是否错误的标志*/
lfile();
if(dd==11) goto ee1;/*文件处理有错误,结束程序*/
/****结束处理文件*******/
comp=0x100;
if(!strcmpi(argv[1],buf1))comp=0x100;/*设置串行口地址为0x2f8+0x100,串行口1*/
if(!strcmpi(argv[1],buf2))comp=0x000;/*设置串行口地址为0x2f8+0x000,串行口2*/
Wdey=atoi(argv[3]);/*串行口写入的等待时间*/
Sdey=atol(argv[4]);/*串行口发送的等待时间*/
disp1();/*显示画面*/
disp2();/*显示画面*/
progr();/*向控制器下载软件的程序*/
ee1: getch();/*程序结束,按任意键推出*/
}
progr()
{
unsigned char summ,ss,ss1[2],ch;
unsigned int i,addr,byt;
gotoxy(37,20);printf(" ");
gotoxy(20,16);printf(" Download program..");
outportb(0x2fb+comp,0x80);/*初始化串行口,设置为8数据位,1起始位,1停止位,9600波特率*/
outportb(0x2f8+comp,0x0c);
outportb(0x2f9+comp,0x00);
outportb(0x2fb+comp,0x03);
outportb(0x2fc+comp,0x02);
/*在串行口上,需要这个设置,保持PC机的输出有效,这样才能在写入编程命令0xaa,0x02后有正确的接收*/
delay(Sdey);
delay(Sdey);
delay(Sdey); /*初始化后,要延时等待初始化完成,在某些PC机中如果没有延时,将无法工作*/
ch=inportb(0x2fd+comp);/*读串行口*/
while(ch&0x01){ ch=inportb(0x2f8+comp);ch=inportb(0x2fd+comp);}/*读空输入*/
/*写入编程命令0xaa,0x02*/
outportb(0x2f8+comp,0xaa);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*写入0xaa,等待发送完毕*/
gotoxy(45,16);printf("press any key exit");
outportb(0x2f8+comp,0x02);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*写入0x02,等待发送完毕*/
/*等待接收控制器返回的响应,同时按任意键结束,控制器返回2个字节0xaa和0x02*/
do{ss=inportb(0x2fd+comp);} while(!(ss&0x01)&&!bioskey(1));ss1[0]=inportb(0x2f8+comp);
do{ss=inportb(0x2fd+comp);} while(!(ss&0x01)&&!bioskey(1));ss1[1]=inportb(0x2f8+comp);
if(!(ss1[0]==0xaa&&ss1[1]==0x02))/*检查返回的数值是否等于0xaa,0x02*/
{
gotoxy(20,17);printf(" Download command faile!");
if(bioskey(1))getch();return;
}
gotoxy(20,17);printf(" Download command ok!");/*下载命令成功*/
ch=128; /*该存储器是按每页128个字节写入的*/
addr=asta; /*将开始的页地址赋值给当前的地址*/
byt=0; /*下传数据字节的总和,初值为0*/
loe: delay(Wdey);/*写入延时,等待控制器写程序*/
outportb(0x2f8+comp,0x55);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*传一页数据开始命令,写入0x55*/
outportb(0x2f8+comp, ch);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*开始传一组长度,128个字节*/
/*下传数据的开始地址*/
ss1[0]=addr>>8;/*计算地址的高位字节*/
ss1[1]=addr; /*计算地址的低位字节*/
outportb(0x2f8+comp,ss1[0]);do{ss=inportb(0x2fd+comp);}
while(!(ss&0x60));/*下传数据的开始地址的高位字节*/
outportb(0x2f8+comp,ss1[1]);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*下传数据的开始地址的高位字节*/
summ=0;/*校验和初值=0*/
for(i=0;i<ch;i++){
/*发送要写的数据,长度是128个*/
outportb(0x2f8+comp,combuf[addr+i]);
do{ss=inportb(0x2fd+comp);}
while((ss&0x60)!=0x60);
summ=summ+combuf[addr+i];/*累加校验和*/
}
/*读返回的校验字*/
do{ss=inportb(0x2fd+comp);}
while(!(ss&0x01)&&!bioskey(1));ss1[0]=inportb(0x2f8+comp);
if(ss1[0]!=summ)/*判别发送数据是否正确*/
{ /*发送数据错误*/
outportb(0x2f8+comp,0xbb);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));
gotoxy(20,19);printf(" Send program faile!");
if(bioskey(1))getch();return;
}
/*一组数据成功传输*/
byt=byt+ch;/*计算要显示的字节数*/
gotoxy (20,18); printf(" Send: %4u page %5u byte",byt/128,byt);
addr=addr+ch;/*计算下次要写入的开始地址*/
if((addr>aend)||(addr==0))/*当前地址大于程序缓冲区的最后地址,下载文件结束,addr=0表示程序最后一页写完,再+128就为0*/
{
outportb(0x2f8+comp,0xbb);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*关闭串行口*/
gotoxy(20,19);printf(" Send program ok!");return;/*文件传输结束,退出函数*/
}
goto loe;/*继续下载下一页数据*/
}
lfile()
{
FILE *fp;
char rbuf[80],ss[2],ad[4],ch;
unsigned char lengh,ad0,ad1,ad2,ad3;
unsigned int sadd,ladd,lladd,address,i;
sadd=60000;/*缓冲区开始地址初值*/
lladd=0; /*缓冲区结束地址判别初值*/
fp=fopen(fname,"r");
if(fp==NULL) goto ee;/*打开文件不正确*/
fgets(rbuf,80,fp); /*读一个文件记录*/
if(rbuf[0]!=':') goto ee2;/*文件格式不正确,记录是以':'开头*/
goto l1; /*开始处理文件一个记录*/
l2: fgets(rbuf,80,fp); /*读一个文件记录*/
l1: if(rbuf[1]<60) ss[0]=rbuf[1]-48;else ss[0]=rbuf[1]-55;/*将字符串长度高位代表的字符转换为数值*/
if(rbuf[2]<60) ss[1]=rbuf[2]-48;else ss[1]=rbuf[2]-55;/*将字符串长度低位代表的字符转换为数值*/
lengh=ss[0]*16+ss[1]; /*计算长度*/
if(lengh==0) goto ee1; /*长度为零,表示文件结束*/
for (i=0;i<=3;i++){
if(rbuf[i+3]<60) ad=rbuf[i+3]-48;else ad=rbuf[i+3]-55;}/*将数据开始地址的字符串转换为数值*/
address=ad[0]*4096+ad[1]*256+ad[2]*16+ad[3];/*计算开始地址*/
if(address>lladd) {lladd=address;ladd=address+lengh-1;}/*计算缓冲区结束地址*/
if(address<sadd) sadd=address; /*计算缓冲区开始地址*/
for (i=0;i<=lengh-1;i++){
if(rbuf[i*2+ 9]<60) ss[0]=rbuf[i*2+ 9]-48;else ss[0]=rbuf[i*2+ 9]-55;
if(rbuf[i*2+10]<60) ss[1]=rbuf[i*2+10]-48;else ss[1]=rbuf[i*2+10]-55;
combuf[address+i]=ss[0]*16+ss[1];} /*将一个记录的数据写入缓冲区*/
goto l2;
ee2: gotoxy(20,15);printf(" File format error !");goto ee3;
ee: gotoxy(20,15);printf(" No the filename or path error !");goto ee3;
ee1: asta=(sadd/128)*128; /*计算开始页地址*/
aend=(ladd/128)*128+127;/*计算结束页地址*/
goto ee4;
ee3: gotoxy(40,19); printf("press any key......");
dd=11;/*文件处理错误标志*/
ee4: fclose(fp);
}
disp2()
{
textmode(C80);
textbackground(BLUE);
textcolor(14);
gotoxy(18,9);printf("File name: %s",fname);
gotoxy(18,10);printf("File lengh: %5u byte Total: %3u page",(aend-asta+1),((aend-asta+1)/128));
gotoxy(18,12);printf("COM port: %2d",2-comp/0x100);gotoxy(35,12);printf("Speed: %5d",9600);
gotoxy(18,14);printf("Wdey=%u ",Wdey) ;gotoxy(35,14);printf("Sdey=%lu",Sdey);
textmode(C80);
textbackground(BLUE);
textcolor(15);
}
disp1()
{
clrscr();
textmode(C80);
textbackground(BLUE);
textcolor(14);
gotoxy(16,7);printf("======================================================");
gotoxy(16,21);printf("======================================================");
}
/*clock()是当前时钟计数器,CLK_TCK=18.2,即时钟每18.2个数是一秒*/
/*
delay_w(unsigned long int t)
{
clock_t start, end;
unsigned long int s,tt;
tt=t*182;
start = clock();
do{end = clock();s=10000*(end - start);}while(s<tt);
}
*/
/*文件的编译模式选择compact*/
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <bios.h>
unsigned char *combuf; /*模拟控制器程序内存的缓冲区指针*/
unsigned int asta,aend,par[130];/*asta内存开始地址,aend内存结束地址*/
char *buf1 = "COM1",*buf2 = "COM2";/*PC机串行口的标志*/
unsigned int comp;/*PC机串行口地址的偏移*/
unsigned int Wdey;
long int Sdey;
char dd,fname[20];
main(argc,argv)/*有参数的程序调用*/
int argc;
char *argv[];
{
long int i;
char ch;
disp1();
if(argc!=5) {/*如果参数个数不对,显示提示信息*/
gotoxy(20,10); printf("Command Format:");
gotoxy(20,12); printf("ss2 COM1/COM2 Filename Write_delay Set_delay");
gotoxy(20,14); printf("Write_delay=0--65000");
gotoxy(20,15); printf("Set_delay =0--65000");
gotoxy(40,17); printf("press any key..exit");
goto ee1;/*结束程序*/
}
/****开始处理文件:主要是将*.HEX文件按地址读入到设置的内存缓冲区中,同时计算出开始和结束的页地址*******/
if((combuf=malloc(0xfff7))==NULL)/*设置内存缓冲区*/
{printf("no memery");goto ee1;}/*如果内存缓冲器设置没有成功,结束程序*/
for(i=0;i<=0xfff7;i++)combuf=0xff;/*内存缓冲区全赋值为0xff*/
asta=0x2000;/*设置控制器内存起始地址为0x2000*/
aend=0x21ff;/*设置控制器内存结束地址为0x21ff*/
ch=strlen(argv[2]);/*argv[2]是要下载的文件名*/
for(i=1;i<=ch;i++){fname[i-1]=argv[2][i-1];}
dd=0;/*文件处理是否错误的标志*/
lfile();
if(dd==11) goto ee1;/*文件处理有错误,结束程序*/
/****结束处理文件*******/
comp=0x100;
if(!strcmpi(argv[1],buf1))comp=0x100;/*设置串行口地址为0x2f8+0x100,串行口1*/
if(!strcmpi(argv[1],buf2))comp=0x000;/*设置串行口地址为0x2f8+0x000,串行口2*/
Wdey=atoi(argv[3]);/*串行口写入的等待时间*/
Sdey=atol(argv[4]);/*串行口发送的等待时间*/
disp1();/*显示画面*/
disp2();/*显示画面*/
progr();/*向控制器下载软件的程序*/
ee1: getch();/*程序结束,按任意键推出*/
}
progr()
{
unsigned char summ,ss,ss1[2],ch;
unsigned int i,addr,byt;
gotoxy(37,20);printf(" ");
gotoxy(20,16);printf(" Download program..");
outportb(0x2fb+comp,0x80);/*初始化串行口,设置为8数据位,1起始位,1停止位,9600波特率*/
outportb(0x2f8+comp,0x0c);
outportb(0x2f9+comp,0x00);
outportb(0x2fb+comp,0x03);
outportb(0x2fc+comp,0x02);
/*在串行口上,需要这个设置,保持PC机的输出有效,这样才能在写入编程命令0xaa,0x02后有正确的接收*/
delay(Sdey);
delay(Sdey);
delay(Sdey); /*初始化后,要延时等待初始化完成,在某些PC机中如果没有延时,将无法工作*/
ch=inportb(0x2fd+comp);/*读串行口*/
while(ch&0x01){ ch=inportb(0x2f8+comp);ch=inportb(0x2fd+comp);}/*读空输入*/
/*写入编程命令0xaa,0x02*/
outportb(0x2f8+comp,0xaa);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*写入0xaa,等待发送完毕*/
gotoxy(45,16);printf("press any key exit");
outportb(0x2f8+comp,0x02);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*写入0x02,等待发送完毕*/
/*等待接收控制器返回的响应,同时按任意键结束,控制器返回2个字节0xaa和0x02*/
do{ss=inportb(0x2fd+comp);} while(!(ss&0x01)&&!bioskey(1));ss1[0]=inportb(0x2f8+comp);
do{ss=inportb(0x2fd+comp);} while(!(ss&0x01)&&!bioskey(1));ss1[1]=inportb(0x2f8+comp);
if(!(ss1[0]==0xaa&&ss1[1]==0x02))/*检查返回的数值是否等于0xaa,0x02*/
{
gotoxy(20,17);printf(" Download command faile!");
if(bioskey(1))getch();return;
}
gotoxy(20,17);printf(" Download command ok!");/*下载命令成功*/
ch=128; /*该存储器是按每页128个字节写入的*/
addr=asta; /*将开始的页地址赋值给当前的地址*/
byt=0; /*下传数据字节的总和,初值为0*/
loe: delay(Wdey);/*写入延时,等待控制器写程序*/
outportb(0x2f8+comp,0x55);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*传一页数据开始命令,写入0x55*/
outportb(0x2f8+comp, ch);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*开始传一组长度,128个字节*/
/*下传数据的开始地址*/
ss1[0]=addr>>8;/*计算地址的高位字节*/
ss1[1]=addr; /*计算地址的低位字节*/
outportb(0x2f8+comp,ss1[0]);do{ss=inportb(0x2fd+comp);}
while(!(ss&0x60));/*下传数据的开始地址的高位字节*/
outportb(0x2f8+comp,ss1[1]);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*下传数据的开始地址的高位字节*/
summ=0;/*校验和初值=0*/
for(i=0;i<ch;i++){
/*发送要写的数据,长度是128个*/
outportb(0x2f8+comp,combuf[addr+i]);
do{ss=inportb(0x2fd+comp);}
while((ss&0x60)!=0x60);
summ=summ+combuf[addr+i];/*累加校验和*/
}
/*读返回的校验字*/
do{ss=inportb(0x2fd+comp);}
while(!(ss&0x01)&&!bioskey(1));ss1[0]=inportb(0x2f8+comp);
if(ss1[0]!=summ)/*判别发送数据是否正确*/
{ /*发送数据错误*/
outportb(0x2f8+comp,0xbb);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));
gotoxy(20,19);printf(" Send program faile!");
if(bioskey(1))getch();return;
}
/*一组数据成功传输*/
byt=byt+ch;/*计算要显示的字节数*/
gotoxy (20,18); printf(" Send: %4u page %5u byte",byt/128,byt);
addr=addr+ch;/*计算下次要写入的开始地址*/
if((addr>aend)||(addr==0))/*当前地址大于程序缓冲区的最后地址,下载文件结束,addr=0表示程序最后一页写完,再+128就为0*/
{
outportb(0x2f8+comp,0xbb);do{ss=inportb(0x2fd+comp);}while(!(ss&0x60));/*关闭串行口*/
gotoxy(20,19);printf(" Send program ok!");return;/*文件传输结束,退出函数*/
}
goto loe;/*继续下载下一页数据*/
}
lfile()
{
FILE *fp;
char rbuf[80],ss[2],ad[4],ch;
unsigned char lengh,ad0,ad1,ad2,ad3;
unsigned int sadd,ladd,lladd,address,i;
sadd=60000;/*缓冲区开始地址初值*/
lladd=0; /*缓冲区结束地址判别初值*/
fp=fopen(fname,"r");
if(fp==NULL) goto ee;/*打开文件不正确*/
fgets(rbuf,80,fp); /*读一个文件记录*/
if(rbuf[0]!=':') goto ee2;/*文件格式不正确,记录是以':'开头*/
goto l1; /*开始处理文件一个记录*/
l2: fgets(rbuf,80,fp); /*读一个文件记录*/
l1: if(rbuf[1]<60) ss[0]=rbuf[1]-48;else ss[0]=rbuf[1]-55;/*将字符串长度高位代表的字符转换为数值*/
if(rbuf[2]<60) ss[1]=rbuf[2]-48;else ss[1]=rbuf[2]-55;/*将字符串长度低位代表的字符转换为数值*/
lengh=ss[0]*16+ss[1]; /*计算长度*/
if(lengh==0) goto ee1; /*长度为零,表示文件结束*/
for (i=0;i<=3;i++){
if(rbuf[i+3]<60) ad=rbuf[i+3]-48;else ad=rbuf[i+3]-55;}/*将数据开始地址的字符串转换为数值*/
address=ad[0]*4096+ad[1]*256+ad[2]*16+ad[3];/*计算开始地址*/
if(address>lladd) {lladd=address;ladd=address+lengh-1;}/*计算缓冲区结束地址*/
if(address<sadd) sadd=address; /*计算缓冲区开始地址*/
for (i=0;i<=lengh-1;i++){
if(rbuf[i*2+ 9]<60) ss[0]=rbuf[i*2+ 9]-48;else ss[0]=rbuf[i*2+ 9]-55;
if(rbuf[i*2+10]<60) ss[1]=rbuf[i*2+10]-48;else ss[1]=rbuf[i*2+10]-55;
combuf[address+i]=ss[0]*16+ss[1];} /*将一个记录的数据写入缓冲区*/
goto l2;
ee2: gotoxy(20,15);printf(" File format error !");goto ee3;
ee: gotoxy(20,15);printf(" No the filename or path error !");goto ee3;
ee1: asta=(sadd/128)*128; /*计算开始页地址*/
aend=(ladd/128)*128+127;/*计算结束页地址*/
goto ee4;
ee3: gotoxy(40,19); printf("press any key......");
dd=11;/*文件处理错误标志*/
ee4: fclose(fp);
}
disp2()
{
textmode(C80);
textbackground(BLUE);
textcolor(14);
gotoxy(18,9);printf("File name: %s",fname);
gotoxy(18,10);printf("File lengh: %5u byte Total: %3u page",(aend-asta+1),((aend-asta+1)/128));
gotoxy(18,12);printf("COM port: %2d",2-comp/0x100);gotoxy(35,12);printf("Speed: %5d",9600);
gotoxy(18,14);printf("Wdey=%u ",Wdey) ;gotoxy(35,14);printf("Sdey=%lu",Sdey);
textmode(C80);
textbackground(BLUE);
textcolor(15);
}
disp1()
{
clrscr();
textmode(C80);
textbackground(BLUE);
textcolor(14);
gotoxy(16,7);printf("======================================================");
gotoxy(16,21);printf("======================================================");
}
/*clock()是当前时钟计数器,CLK_TCK=18.2,即时钟每18.2个数是一秒*/
/*
delay_w(unsigned long int t)
{
clock_t start, end;
unsigned long int s,tt;
tt=t*182;
start = clock();
do{end = clock();s=10000*(end - start);}while(s<tt);
}
*/