#include #include #include #include #include #include "..\lib\7188.h" #define __SHOW_MSG__ #define ACK 0x06 #define NAK 0x15 #ifndef __TURBOC__ /* when use MSC, set memory pack size to 1 (for byte alignment) */ #pragma pack(1) #endif struct Block { unsigned datano; unsigned char data[256]; unsigned char CRC16Hi; unsigned char CRC16Lo; }; struct FileInfo { char fname[13]; long size; int year,month,day; int hour,minute,second; }; unsigned char DataBuf[260]; struct Block *block=(struct Block *)DataBuf; struct FileInfo fileinfo; unsigned Crc16Table[256]; void MakeCRC16Table(void) { int i,j,k,crc; for(i=0;i<256;i++){ k=i<<8; crc=0; for(j=0;j<8;j++){ if((crc^k)&0x8000) crc=(crc<<1)^0x1021; else crc<<=1; k<<=1; } Crc16Table[i]=(unsigned)crc; /* Print("0x%04X, ",crc); if((i&7)==7) Print("\n\r"); */ } } unsigned GetCRC16(unsigned char *data,int length) { unsigned crc=0; while(length--){ crc=(unsigned)(Crc16Table[crc>>8] ^ (crc<<8) ^ (*data++)); } return crc; } /* every block has data_length(2 bytes)+data(256 bytes)+CRC_16(2 bytes) =260 bytes. */ int ReceiveDataBlock(void) { unsigned char *dataptr; int i,quit=0; int times=0; while(!quit){ dataptr=DataBuf; for(i=0;i<260;){ if(IsCom4()){ *dataptr++=ReadCom4(); i++; } } #ifdef __SHOW_MSG__ printCom3("New Block CRC "); #endif if(GetCRC16(DataBuf,260)){ /* CRC error */ ToCom4(NAK); #ifdef __SHOW_MSG__ printCom3("Error\n\r"); #endif times++; if(times>=3) quit=1; } else { ToCom4(ACK); #ifdef __SHOW_MSG__ printCom3("OK, datano=%d\n\r",block->datano); #endif return NoError; } } return -1; } char filename[20]="B:\\"; FILE *file=NULL; int error=0; void OpenFileForSave(void) { fileinfo=*(struct FileInfo *)(DataBuf+2); /* save the file info. */ strcpy(filename+3,fileinfo.fname); file=fopen(filename,"wb"); #ifdef __SHOW_MSG__ printCom3("sizeof struct FileInfo=%d\n",sizeof(struct FileInfo)); printCom3("filename=%s size=%lu\n\r",filename,fileinfo.size); #endif } void SaveBlockToFile(void) { if(file) { if(1!=fwrite(DataBuf+2,block->datano,1,file)) error=1; } } void main(int argc,char *argv[]) { long baud=57600L; int i; unsigned BlockNo; if(!Is7188()){ puts("\nLOAD.EXE must run on I-7188"); return; } for(i=1;i