Search code examples
gnuplot

How to Get Gnuplot to Read Input File with Hex Data


I'm collecting data from a CAN bus viewer that saves it as a text file in a fixed column format, like this:

  4681)     24123.0  Rx         0488  8  79 00 DF 58 66 00 FF 00

I'd like to have gnuplot create a plot using column 2 for 'x' and the sum of columns 10 and 11 as the 'y' value, where column 10 is the LSB and column 11 is the MSB (so it needs to be multiplied by 256 first). Both columns 10 and 11 are in hexadecimal. The documentation I've read implies that a sprintf format can be used, but I'm not having any luck getting the correct syntax.


Solution

  • If I correctly understood that you have a text not binary file with hexadecimal numbers, this would be my suggestion. No need for external scripts, all can be done with gnuplot 4.6.0 (version at the time of OP's question). A few more data lines are added as well as labels on the plot to check the test result.

    Data: SO24727223.dat

    4681)     24123.0  Rx         0488  8  79 00 DF 58 66 00 FF 00
    4681)     24124.0  Rx         0488  8  79 00 DF 58 00 01 FF 00
    4681)     24125.0  Rx         0488  8  79 00 DF 58 01 02 FF 00
    4681)     24126.0  Rx         0488  8  79 00 DF 58 FF 01 FF 00
    

    Code: (works with gnuplot>=4.6.0)

    ### construct/plot hex numbers
    reset
    
    FILE = "SO24727223.dat"
    
    HexToInt(col1,col2) = int("0x".strcol(col1).strcol(col2))
    myLabel(col1,col2)  = sprintf("0x%s%s\n=%d",strcol(col1),strcol(col2),HexToInt(col1,col2))
    
    set key noautotitle
    set offsets 0.5,0.5,50,50
    
    plot FILE u 2:(HexToInt(11,10)) w lp pt 7, \
           '' u 2:(HexToInt(11,10)):(myLabel(11,10)) w labels offset 1,-1
    ### end of code
    

    Result:

    enter image description here