发送文件:
int main(int argc, char *argv[])
{
if((filep=fopen(argv[1],"r+b"))==NULL)
{
perror("open file error!\n");
exit(EXIT_FAILURE);
}
fd=Com_Inital(0,1152); //串口初始化
while(!feof(filep))
{
rsize=fread(sbuf,1,max_buffer_size,filep);
if(rsize>0)
{
retv=write(fd,sbuf,rsize); /*发数据*/
if(retv!=rsize)
{
perror("write");
}
printf("the number of charater sent is %d\n",retv);
}
}
close(fd);
}
串口接收文件:
int readDataFun (int fd, char *pp, int *iLen)
{
char ch[1024];
int i;
memset (ch, 0, sizeof (ch));
if ((*iLen = read (fd, ch, 50)) > 0)
{
memcpy(pp,ch,*iLen);
return *iLen;
}
return 0;
}
main()
{
int fd;
int num = 0;
char read_buf[50] = {0};
int iLen;
int ncount = 0;
FILE *fp;
int signo;
fd_gpio = open("/dev/gpio",O_RDWR|O_NONBLOCK);
if(fd_gpio<0)
{
printf("Open gpio device error\n");
exit(0);
}
if((fp=fopen("serialdata.db","wb"))==NULL)
{
perror("can not open/create file serialdata.");
exit(EXIT_FAILURE);
}
fd = serial_init(); //串口初始化
while(1)
{
if((num = readDataFun(fd_uart[UART_PORT], read_buf, &iLen))>0)
{
fwrite(read_buf,num,1,fp);
}
}
close(fd_uart[UART_PORT]);
if(fclose(fp)<0)
perror("closing file serialdata fail!");
exit(EXIT_SUCCESS);
}
阅读(7509) | 评论(0) | 转发(0) |