Have you tried opening the port in binary mode?
The fwrite documentation is rather specific on the topic...
Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.