📄 download.c
字号:
长度4 返回 of f 发送数据 arm接受数据 or dataw w 开始写flash 每写1K发送一个 . 写完所有的数据 发送 k 通信结束*/inttransfer(int fd, int rate, char *addr, char *filename) { char buf; // save the client's reply, 1 byte int tmp; // save the information for send, 4 bytes FILE *fp; // the file's handle we send int track = 0; // for show the finished rate int oldtrack = 0; int filelen = 0; // the file's length we send int step = 0; // the scale about the track char str[1024]; // the buffer for store data int finished = 0; // the rate of the finished int i; if(!set_speed(fd, 9600)) { printf("Unsupported Baud Rate.\n"); exit(0); } if (set_Parity(fd,8,1,'N')== FALSE) { printf("Set Parity Error\n"); exit(0); } setvbuf(stdout, (char *)NULL, _IONBF, 0); printf("Waiting Armboard Wakeup..."); buf = '\0'; /* be sure armboard is ready */ while(buf != '<') { while(read(fd,&buf,1)==0); } printf("OK.\n"); /* send client program (2k) */ printf("Sending Client..."); send_client(fd); buf = '\0'; while(buf != '>') { while(read(fd,&buf,1)==0); } printf("OK.\n"); printf("Check Client..."); /* be sure client is ready */ buf = 'a'; write(fd, &buf, 1); buf = '\0'; while(buf != 'a') { while(read(fd,&buf,1)==0); } printf("OK.\n"); printf("Setting Baud Rate..."); /* set baud rate */ buf = 's'; write(fd, &buf, 1); buf = '\0'; while(buf != 's') { while(read(fd,&buf,1)==0); } switch (rate) { case 115200: tmp = BR_115200; break; case 57600: tmp = BR_57600; break; case 38400: tmp = BR_38400; break; case 19200: tmp = BR_19200; break; case 9600: tmp = BR_9600; break; case 2400: tmp = BR_2400; break; case 1200: tmp = BR_1200; break; default: printf("Unsupported Baud Rate.\n"); setlinebuf(stdout); exit(0); } write(fd, &tmp, 4); read(fd,str,4); if(!set_speed(fd, rate)) { printf("Unsupported Baud Rate.\n"); setlinebuf(stdout); exit(0); } if (set_Parity(fd,8,1,'N')== FALSE) { printf("Set Parity Error\n"); exit(0); } buf = '\0'; while(buf != 'o') { while(read(fd,&buf,1)==0); } printf("OK.\n"); printf("Setting Address..."); /* set address */ buf = 'd'; write(fd, &buf, 1); buf = '\0'; while(buf != 'd') { while(read(fd,&buf,1)==0); } tmp = atohex(addr); if((tmp%0x20000) != 0) { printf("Unsupported Address.\n"); setlinebuf(stdout); exit(0); } write(fd, &tmp, 4); buf = '\0'; while(buf != 'o') { while(read(fd,&buf,1)==0); } printf("OK.\n"); printf("Setting Data Length..."); /* set file length */ if((fp=fopen(filename, "rb+")) == NULL) { printf("Fail To Open The File: %s\n", filename); setlinebuf(stdout); exit(0); } filelen = flen(fp); tmp = filelen; if((filelen%2) == 1) filelen++; buf = 'l'; write(fd, &buf, 1); while(buf != 'l') { while(read(fd,&buf,1)==0); } write(fd, &filelen, 4); buf = '\0'; while(buf != 'o') { while(read(fd,&buf,1)==0); } printf("OK.\n"); /* file operation */ buf = 'f'; write(fd, &buf, 1); buf = '\0'; while(buf != 'f') { while(read(fd,&buf,1)==0); } /* at the moment we only can write the file to board */ filelen = tmp; step = tmp/50; printf("Sending Data : [ 0% ]"); while(tmp>1024) { fread((void *)str, 1024, 1, fp); write(fd, str, 1024); tmp = tmp-1024; finished += 1024; oldtrack = track; while(finished >= step*(track+1)) { track++; } if(oldtrack != track) { oldtrack = track; printf("\rSending Date : "); for(i=0;i<track;i++) printf("#"); printf(" [ %d% ]", track*2); } } fread((void *)str, tmp, 1, fp); write(fd, str, tmp); if((filelen%2) == 1) { buf = 0xff; write(fd, &buf, 1); filelen++; } track = 50; printf("\rSending Date : "); for(i=0;i<track;i++) printf("#"); buf = '\0'; while(buf != 'o') { while(read(fd,&buf,1)==0); } printf(" [ 100% ]\n"); buf = 'w'; write(fd, &buf, 1); buf = '\0'; while(buf != 'w') { while(read(fd,&buf,1)==0); } /* show the track of the eeprom store */ track = 0; step = filelen/50; // the length of the file on the board is not same as the length of the file // on the computer finished = 0; oldtrack = 0; printf("Writing Flash: [ 0% ]"); while(1) { buf = '\0'; while((buf!='k') && (buf!='.')) { while(read(fd,&buf,1)==0); } if(buf == '.') { finished += 1024; oldtrack = track; while(finished >= step*(track+1)) { track++; } if(oldtrack != track) { oldtrack = track; printf("\rWriting Flash: "); for(i=0;i<track;i++) printf("#"); printf(" [ %d% ]", track*2); } } if(buf == 'k') { track = 50; printf("\rWriting Flash: "); for(i=0;i<track;i++) printf("#"); printf(" [ 100% ]\n"); setlinebuf(stdout); return(TRUE); } } setlinebuf(stdout); return (FALSE);}intmain(int argc, char **argv) { int fd = 0; char cmd[128]; char param[128]; int i = 1; int readparam = 0; char device[128]; int rate; char filename[128]; char address[128]; default_init(device, &rate, filename, address); while(i < argc) { i++; if(strlen(argv[i-1]) > 127) { printf("Too long Parameter, Error.\n"); print_help(); exit(0); } if(readparam == 1) { strcpy(param, argv[i-1]); if(param[0] == '-') { printf("Parameter Error.\n"); print_help(); exit(0); } if((strcmp(cmd, "--device")==0) || (strcmp(cmd, "-d")==0)) { strcpy(device, "/dev/"); strcat(device, param); } else if((strcmp(cmd, "--rate")==0) || (strcmp(cmd, "-r")==0)) { rate = atol(param); } else if((strcmp(cmd, "--filename")==0) || (strcmp(cmd, "-f")==0)) { strcpy(filename, param); } else if((strcmp(cmd, "--address")==0) || (strcmp(cmd, "-a")==0)) { strcpy(address, param); } else { printf("Parameter Error.\n"); print_help(); exit(0); } readparam = 0; } else { strcpy(cmd, argv[i-1]); if(strlen(cmd) <= 1) { printf("Parameter Error.\n"); print_help(); exit(0); } if(cmd[0] != '-') { printf("Parameter Error.\n"); print_help(); exit(0); } if((strcmp(cmd, "--help")==0) || (strcmp(cmd, "-h")==0)) { print_help(); exit(0); } if((strcmp(cmd, "--version")==0) || (strcmp(cmd, "-v")==0)) { PRINT_VERSION exit(0); } readparam = 1; } } if((readparam==1) && (argc!=1)) { printf("Parameter Error.\n"); print_help(); exit(0); } fd = OpenDev(device); if(fd <= 0) { printf("Can't Open Serial Port, Error.\n"); exit(0); } if(!transfer(fd, rate, address, filename)) { printf("Fail To Send The File.\n "); exit(0); } tty_reset(fd); close(fd); printf("All Done Successful !\n"); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -