Search code examples
visual-c++fstreamiostreamifstreamhex-file

Printing Lines from Intel HEX Record File


I'm trying to send the contents of an Intel Hex file over a Serial connection to a microcontroller, which will process each line sent and program them into memory as needed. The processing code expects the lines to be sent as they appear in the Hex file, including the newline characters at the end of each line.

This code is being run in Visual Studio 2013 on a Windows 10 PC; for reference, the microcontroller is an ARM Cortex-M0+ model.

However, the following code doesn't seem to be processing the Intel Hex record file the way that I expected.

...
int count = 0;
char hexchar;
unsigned char Buffer[69]; // 69 is max ascii hex read length for microcontroller
ifstream hexfile("pdu.hex");
while (hexfile.get(hexchar))
{
    Buffer[count] = hexchar;
    count++;
    if (hexchar == '\n')
    {
        for (int i = 0; i < count; i++)
        {
            printf("%c", Buffer[i]); 
        }
        serial_tx_function(Buffer); // microcontroller requires unsigned char
        count = 0;
    }
}
...

Currently, the serial transmission call is commented out, and the for loop is there to verify that the file is being read properly. I expect to see each line of the hex file printed out to the terminal. Instead, I get nothing at all. Any ideas?

EDIT: After further investigation, I determined that the program isn't even entering the while loop because the file fails to open. I don't know why that would be the case, since the file exists and can be opened in other programs like Notepad. However, I'm not terribly experienced with file I/O, so I might be overlooking something.


Solution

  • *.hex files contain non-ascii data a lot of the times that can have issues being printed out on command-line terminals.

    I would just say you should try to open the file as a binary and print the characters as hexadecimal numbers.

    So make sure you open the file in binary mode with ifstream hexfile("pdu.hex", ifstream::binary); and if you want to print hex characters the printf specifier is %x or %hhx for char.

    The whole program would look something like this:

    #include <iostream>
    #include <fstream>
    #include <cassert>
    
    int main()
    {
        using namespace std;
        int count = 0;
        char hexchar;
        constexpr int MAX_LINE_LENGTH = 69;
        unsigned char Buffer[MAX_LINE_LENGTH]; // 69 is max ascii hex read length for microcontroller
        ifstream hexfile("pdu.hex",ios::binary);
        while (hexfile.get(hexchar))
        {
            assert(count < MAX_LINE_LENGTH);
            Buffer[count] = hexchar;
            count++;
            if (hexchar == '\n')
            {
                for (int i = 0; i < count; i++)
                {
                    printf("%hhx ", Buffer[i]);
                }
                printf("\n");
                //serial_tx_function(Buffer); // microcontroller requires unsigned char
                count = 0;
            }
        }
    }