采用458串口升级固件(主机master,从机slave)
linux平台
1. 首先编译出一个可执行固件hello,比如能打印“hello,world”
2. 再分别主机运行./serial slave 主机./serial master。
传输完成后,把从机收到的hello文件更改为可执行权限。
/××××××××××××××××××××××××××××××××××××××××××××××××××××××××
××文件名:serial.c
××××××××××××××××××××××××××××××××××××××××××××××××××××××××/
#include <stdio.h> #include <stdlib.h> #include <string.h>
//每帧大小和波特率和每帧间隔有关,当前已验证波特率115200每帧1024个字节时每帧间隔200ms,2.8M压缩包大概9分钟完成传输,每秒传输5帧
#define BUF_SIZE 1024
int openport(int num,int baud,int databits,int parity,int stopbits) { struct termios TtyAttr; if(num==0) { CommFd = open("/dev/ttymxc0", O_RDWR|O_NOCTTY|O_NDELAY, 0); } else if(num==1) { CommFd = open("/dev/ttymxc1", O_RDWR|O_NOCTTY|O_NDELAY, 0); } else if(num==2) { CommFd = open("/dev/ttymxc2", O_RDWR|O_NOCTTY|O_NDELAY, 0); } else if(num==3) { CommFd = open("/dev/ttymxc3", O_RDWR|O_NOCTTY|O_NDELAY, 0); } else if(num==4) { CommFd = open("/dev/ttymxc4", O_RDWR|O_NOCTTY|O_NDELAY, 0); } if (CommFd < 0) { return -1; } bzero(&TtyAttr,sizeof(struct termios)); tcflush(CommFd,TCIOFLUSH); //清空串口接收发送缓冲中的数据 if(baud==9600) { cfsetispeed(&TtyAttr,B9600); //设置串口接收波特率 cfsetospeed(&TtyAttr,B9600); //设置串口发送波特率 } else if(baud==19200) { cfsetispeed(&TtyAttr,B19200); cfsetospeed(&TtyAttr,B19200); } else if(baud==38400) { cfsetispeed(&TtyAttr,B38400); cfsetospeed(&TtyAttr,B38400); } else if(baud==57600) { cfsetispeed(&TtyAttr,B57600); cfsetospeed(&TtyAttr,B57600); } else if(baud==115200) { cfsetispeed(&TtyAttr,B115200); cfsetospeed(&TtyAttr,B115200); } TtyAttr.c_cflag |= CREAD | CLOCAL; //启动接收器, 不管modem的控制线状态 TtyAttr.c_cflag &= ~CSIZE; //在c_cflag里把表示数据位的位域清零 if(databits == 6) //数据位设置 { TtyAttr.c_cflag |= CS6; } else if(databits == 7) { TtyAttr.c_cflag |= CS7; } else if(databits == 8 || databits == NULL) { TtyAttr.c_cflag |= CS8; } if(parity == 1) { TtyAttr.c_cflag |= PARENB; //奇偶校验使能 TtyAttr.c_cflag &= ~PARODD; //偶校验 } else if(parity == 2) { TtyAttr.c_cflag |= PARENB; TtyAttr.c_cflag |= PARODD; } else if(parity == NULL) //无校验位 { TtyAttr.c_cflag &= ~PARENB; } if(stopbits == 2) { TtyAttr.c_cflag |= CSTOPB; } else if(stopbits == NULL) { TtyAttr.c_cflag &= ~CSTOPB; //使用一位停止位 } TtyAttr.c_cc[VTIME] = 0; TtyAttr.c_cc[VMIN] = 0; tcflush(CommFd,TCIOFLUSH); if (tcsetattr(CommFd, TCSANOW, &TtyAttr) < 0) //立即生效 { close(CommFd); return -1; } return CommFd; } int closeport(int CommFd) { if(close(CommFd)==-1) { return -1; } return 0; } int main(int argc, char *argv[]) { int count_read = 0; int count_write = 0; int count = 0; char buf[BUF_SIZE]; int CommFd = openport(3, 115200, databits, parity, stopbits); //打开串口,参数可自行设置 if(strcmp(argv[1],"master")==0) { FILE *fp_read; if((fp_read=fopen("/clever/bin/hello", "rb"))==NULL) //以二进制方式打开文件并判断是否成功 { printf("he file can not be opened\n"); exit(1);//结束程序的执行 } while(1) { memset(buf,0,sizeof(buf)); count_read = fread(buf, BUF_SIZE, 1, fp_read); printf("buf_len = %d\n", strlen(buf)); printf("count_read = %d\n", count_read); usleep(200*1000); //当波特率115200每帧1024个字节时每帧间隔200ms write(CommFd,buf,BUF_SIZE); //向串口中写数据 if(count_read<1) break; } write(CommFd,"end",BUF_SIZE); //告诉从机已经发送完成 fclose(fp_read); } else if(strcmp(argv[1],"slave")==0) { FILE *fp_write; if((fp_write=fopen("/clever/bin/hello", "wb"))==NULL) //写文件并判断是否成功 { printf("The write.txt file can not be opened.\n"); exit(1);//结束程序的执行 } while(1) { memset(buf, 0, BUF_SIZE); count = read(CommFd, buf,BUF_SIZE); //读串口 if(strcmp(buf, "end")==0) break; if(count>0) { printf("buf_len = %d\n", strlen(buf)); printf("count = %d\n", count); count_write = fwrite(buf, BUF_SIZE, 1, fp_write); printf("count_write = %d\n", count_write); } } fclose(fp_write); } else { printf("error\n"); return 0; } closeport(CommFd); printf("Exit this program\n"); return 0; }

浙公网安备 33010602011771号