This post was last edited by jinglixixi on 2020-10-18 10:45
An interesting test about USB read/write. The routine on the official website only provides an example of creating a new file on the USB flash drive, which is used to generate a text file.
- First, build the test circuit as shown in Figure 1;
- Download the compiled test program to the development board;
- Open the serial port debugging tool and run the routine, the execution effect is shown in Figure 2.
Figure 1 Test circuit
Figure 2 Creation and writing results
After inserting the U disk into the computer, you can indeed see the newly generated file NEWFILE1.TXT in the U disk, and its content is shown in Figure 3.
Figure 3 File content
Having the write function is certainly welcome, but how to read?
There is no follow-up in the routine, so I referred to other materials and supplemented the reading program by myself. The execution effect is shown in Figure 4.
Figure 4 Reading results
But the problem is that I clearly wrote "Note: This program reads and writes USB files in bytes, and simply demonstrates the function." How come the first 100 characters read are 100 "CCCCCCCCCC..." which have nothing to do with the written content? What is the problem?
Later I found out that the problem was with the following statement:
for ( i=0; i!=mCmdParam.ByteRead.mByteCount; i++ ) printf( "%C", mCmdParam.ByteRead.mByteBuffer );
Above, should be changed to:
for ( i=0; i!=mCmdParam.ByteRead.mByteCount; i++ ) printf( "%c", mCmdParam.ByteRead.mByteBuffer );
Figure 5 Correct reading result
The reading problem is finally solved!
Another problem I was concerned about was how to generate a BMP image file, so I generated a file with a BMP suffix by storing binary numbers. Then the trouble came.
What's going on?
That is, it really generates a file with that name, but the problem is that you can neither view its contents with WinHex nor delete it freely. If you continue this experiment, there is only one result, which is to reformat the USB drive, otherwise it will be unusable, but the formatting method may not work.
What should I do? I can only fight poison with poison. At first, I used the file deletion program on the development board to delete it, but unfortunately it didn't work. What else can I do?
So I first created a text file on the development board with the same file name, and then deleted the file. This time it worked, and the stubborn problem was removed from the USB drive!
The effect of deleting files is shown in Figure 6
Figure 6 Deleting files
In file operations, if the specified file directory does not exist, all files under the root directory will be listed, as shown in Figure 7.
Figure 7 Listing the root directory files
Is it possible to write binary byte data into a file? Of course it is possible. Look at the following program, isn't it clear that the carriage return and line feed characters are written into the file?
printf( "ByteWrite\n" );
i = sprintf( (PCHAR)buf,"Note: \xd\xaThis is a simple example of reading and writing USB flash drives in bytes.\xd\xa");
mCmdParam.ByteWrite.mByteCount = i;
mCmdParam.ByteWrite.mByteBuffer = buf;
s = CH579ByteWrite() ;
mStopIfError( s );
printf("Written successfully\n");
Later we will continue to explore the method of generating and reading BMP files in order to realize the function of digital photo frame on this development board.
|