Skip to content

Instantly share code, notes, and snippets.

@blindman2k
Last active August 29, 2015 14:00
Show Gist options
  • Save blindman2k/ea13ff274d7080ca610e to your computer and use it in GitHub Desktop.
Save blindman2k/ea13ff274d7080ca610e to your computer and use it in GitHub Desktop.
Hexdump is a function to output binary data in a hex + text format on a serial port.

Hexdump

This function will dump a blob or string as a hexdump to a serial port.

The input looks like this:

hexdump("RECV", "Hello world\n", "uart57");

The output (with a more complex input!) will look like this:


RECV  80 0b 06 00 a8 04 77 b8  ec f3 40 10 00 ff 00         |......w...@.... |
RECV  80 0e 06 00 b7 00 5a 7c  48 13 81 47 01 ff 03 02      |......Z|H..G....|
RECV  01 1a                                                 |..              |
RECV  80 0b 06 00 b7 04 5a 7c  48 13 81 47 01 ff 00         |......Z|H..G... |
RECV  80 1a 06 00 ac 00 77 b8  ec f3 40 10 00 ff 0f 02      |......w...@.....|
RECV  01 1a 0b ff 4c 00 09 06  00 4c 0a 01 02 46            |....L....L...F  |
RECV  80 0b 06 00 ac 04 77 b8  ec f3 40 10 00 ff 00         |......w...@.... |
RECV  80 0e 06 00 b6 00 5a 7c  48 13 81 47 01 ff 03 02      |......Z|H..G....|
RECV  01 1a                                                 |..              |
RECV  80 0b 06 00 b7 04 5a 7c  48 13 81 47 01 ff 00         |......Z|H..G... |
RECV  80 1a 06 00 a7 00 77 b8  ec f3 40 10 00 ff 0f 02      |......w...@.....|
RECV  01 1a 0b ff 4c 00 09 06  00 4c 0a 01 02 46            |....L....L...F  |
RECV  80 0b 06 00 a7 04 77 b8  ec f3 40 10 00 ff 00         |......w...@.... |
RECV  80 1c 06 00 d5 00 05 a6  73 e5 c5 78 00 ff 11 02      |........s..x....|
RECV  01 06 03 02 0a 18 09 ff  a0 00 02 01 00 02 02 01      |................|
function hexdump(label, message, uart = "uart57") {
local line = blob(80);
local endofline = line.len();
local block1 = label.len() + 2;
local block3 = endofline-20;
for (local i = 0; i < line.len(); i++) line[i] = ' ';
line.seek(0); line.writestring(label);
line[block3] = '|'; line[endofline-3] = '|'; line[endofline-2] = '\r'; line[endofline-1] = '\n';
local hex_pos = block1, asc_pos = block3+1, ch_in_line = 0;
for (local i = 0; i < message.len(); i++) {
local ch = message[i];
ch_in_line++;
local ch_hex = format("%02x", ch);
line[hex_pos++] = ch_hex[0];
line[hex_pos++] = ch_hex[1];
if (ch_in_line == 16) hex_pos += 3;
else if (ch_in_line == 8) hex_pos += 2;
else hex_pos += 1;
if (ch >= ' ' && ch <= '~') line[asc_pos++] = ch;
else line[asc_pos++] = '.';
if (ch_in_line == 16) {
hex_pos = block1;
asc_pos = block3+1;
ch_in_line = 0;
hardware[uart].write(line.tostring());
// Reset the blob;
for (local i = 0; i < line.len(); i++) line[i] = ' ';
line.seek(0); line.writestring(label);
line[block3] = '|'; line[endofline-3] = '|'; line[endofline-2] = '\r'; line[endofline-1] = '\n';
}
}
if (ch_in_line > 0) {
hardware[uart].write(line.tostring());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment