太上无情 2017-07-26 07:40 采纳率: 30%
浏览 690

系统移植时流文件传输中断

用c语言写的一个程序,通过fread函数读取一个bit文件(ucos.bin文件),每次读取4个字节
到缓冲区中,然后用write函数写入到开发板中,文件在读取到1024次后停止,有知道的帮忙解决一下吗?

#include
#include
#include
#include
#include
#include
#include

/* baudrate settings are defined in , which is
included by /
#define BAUDRATE B9600

/
change this definition for the correct port /
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1 /
POSIX compliant source */

#define FALSE 0
#define TRUE 1

void main(int argc, char ** argv)
{
int err = 0;
int fd,c, res;
struct termios oldtio,newtio;
char buf[1];
/*
Open modem device for reading and writing and not as controlling tty
because we don't want to get killed if linenoise sends CTRL-C.
*/
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current serial port settings /
bzero(&newtio, sizeof(newtio)); /
clear struct for new port settings */

/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;//| CRTSCTS

/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
newtio.c_iflag = IGNPAR; //| ICRNL;

/*
Raw output.
*/
newtio.c_oflag = 0;

/*
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
// newtio.c_lflag = ICANON;

/*
now clean the modem line and activate the settings for the port
*/
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

FILE* fp;
if(argc<2) fp = fopen("inst_rom.bin","rb");
else fp = fopen(argv[1],"rb");

if(!fp){
printf("Cannot open file\n");

}

int i=0;

printf(">> Start Trans\n");

while (!feof(fp)) {
i++;
fread(buf,4,1,fp);
if(!feof(fp)){
res = write(fd,buf,4);
if(res!=4)err++;
printf("%d: %d, ",i,res);
if(i%5==4)printf("\n");
usleep(100);
}

}
printf("end while");
for(i=0;i<4;i++)
buf[i]=-1;
printf("buf assigned");
res = write(fd,buf,4);
printf("\n<< Send FF*4\n");
printf("Err: %d\n",err);
printf("Goto: 0x0\n=====\n");

int rec;

while(1){
rec = read(fd,buf,1);
if(rec>0) printf("%c",buf[0]);
}

/* restore the old port settings */
tcsetattr(fd,TCSANOW,&oldtio);

}

  • 写回答

1条回答 默认 最新

  • jklinux 2017-07-26 14:56
    关注

    把具体代码帖一下,这样才好找问题

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办