Hi,
I'm gettin data in .hex through UART to TextBox, i received it in stardart format:
What im getting now[
^]
But i need to make it shown in HexDump format, i found an example in COdeProject
Quick and Dirty HexDump of a Byte Array[
^]
And tried it by my own, nothing happend, seems like im using it wrong, please help, im really in struggle now
What I have tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Write("\u0005");
serialPort1.Write(Convert.ToString(0x05));
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.PortName = textBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);
}
string rs;
byte re;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
rs = serialPort1.ReadExisting();
System.Text.Encoding.ASCII.GetString(new[] { re });
this.Invoke(new EventHandler(type));
}
catch (System.TimeoutException) { }
}
void type(object s,EventArgs e)
{
textBox4.Text += rs;
}
private void button3_Click(object sender, EventArgs e)
{
serialPort1.Open();
}
private void button4_Click(object sender, EventArgs e)
{
serialPort1.Close();
}
private void button5_Click(object sender, EventArgs e)
{
serialPort1.Write(Convert.ToString(0x02));
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
}
class Utils
{
public static string HexDump(byte[] bytes, int bytesPerLine = 16)
{
if (bytes == null) return "<null>";
int bytesLength = bytes.Length;
char[] HexChars = "0123456789ABCDEF".ToCharArray();
int firstHexColumn =
8
+ 3;
int firstCharColumn = firstHexColumn
+ bytesPerLine * 3
+ (bytesPerLine - 1) / 8
+ 2;
int lineLength = firstCharColumn
+ bytesPerLine
+ Environment.NewLine.Length;
char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
StringBuilder result = new StringBuilder(expectedLines * lineLength);
for (int i = 0; i < bytesLength; i += bytesPerLine)
{
line[0] = HexChars[(i >> 28) & 0xF];
line[1] = HexChars[(i >> 24) & 0xF];
line[2] = HexChars[(i >> 20) & 0xF];
line[3] = HexChars[(i >> 16) & 0xF];
line[4] = HexChars[(i >> 12) & 0xF];
line[5] = HexChars[(i >> 8) & 0xF];
line[6] = HexChars[(i >> 4) & 0xF];
line[7] = HexChars[(i >> 0) & 0xF];
int hexColumn = firstHexColumn;
int charColumn = firstCharColumn;
for (int j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytesLength)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
byte b = bytes[i + j];
line[hexColumn] = HexChars[(b >> 4) & 0xF];
line[hexColumn + 1] = HexChars[b & 0xF];
line[charColumn] = (b < 32 ? '·' : (char)b);
}
hexColumn += 3;
charColumn++;
}
result.Append(line);
}
return result.ToString();
}
}
}