Altera_Forum
Honored Contributor
20 years agofile handling problems with jtag uart
I have some nios2 test code below that uses the jtag_uart. It should do non-blocking read & write of the jtag uart when fed from the nios2-terminal. The code behaves as expected when using uart0, but not when using the jtag uart.
When using the jtag uart, the feof() and ferror calls() behave as though they have been swapped! feof() is never true. ferror() is set true (err=1) when no chars are read, and false when a char is read. This looks like a bug. int tch,count,err; char DataIn[3]; int jtagfdi,jtagfdo; FILE * JtagPortI, *JtagPortO; //non-blocking IO with streams jtagfdi = open("/dev/jtag_uart", O_RDONLY|O_NONBLOCK|O_NOCTTY); jtagfdo = open("/dev/jtag_uart", O_WRONLY|O_NONBLOCK|O_NOCTTY); JtagPortI = fdopen(jtagfdi,"r"); JtagPortO = fdopen(jtagfdo,"w"); while(1){ if(feof(JtagPortI)) clearerr(JtagPortI);//must clear feof condition else //may have chars waiting { count = (int) fread(&tch,1,1,JtagPortI);//read 1 char. if((err = ferror(JtagPortI)) != 0) { printf("Read ferror: %d \r\n", err); clearerr(JtagPortI); //must clear ferror condition } else //no ferror { if ( (tch != EOF) && (count == 1) ) { DataIn[0] = toupper((char)tch); fwrite(DataIn, 1, 1, JtagPortO); } }//endif(ferror()) }//endif(feof()) }//endwhile(1)