Search code examples
c#serial-porthexdump

(C#) How to make HexDump formating works with TextBox?


I have TextBox on Windows forms where I successfully receive hex-data from com-port, but it is shown in a line, but I need it to be shown in HexDumpFormat.

What I getting now:

What i getting now

After searching I found sample code: Quick and Dirty Hex dump. This seems like what I need, the TC said that all we need is to paste his function into code of mine, and call this function where we need, but I'm puzzled how exactly to make it work with my code? Much thanks. (I cant attach more then 3 links, therefore you can see on a link page, how HexDump format looks like.

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 textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        string rs;
        byte re;
        private void serialPort1_DataReceived(object sender,    SerialDataReceivedEventArgs e) // Da
        {
            try
            {
                //rs = serialPort1.ReadByte();
                //re = Convert.ToByte(serialPort1.ReadByte());
                rs = serialPort1.ReadExisting();
                System.Text.Encoding.ASCII.GetString(new[] { re });
                this.Invoke(new EventHandler(type));
            }
            catch (System.TimeoutException) { }
        }
        void type(object s, EventArgs e)              // receive data
        {
            textBox4.Text += rs;
        }
    }
}

Solution

  • I tried the code from the link. This simple example:

    byte[] byte_array = new byte[5];
    
    byte_array[0] = 0xAC;
    byte_array[1] = 0x12;
    byte_array[2] = 0xA0;
    byte_array[3] = 0xC5;
    byte_array[4] = 0x88;
    
    Console.WriteLine(Utils.HexDump(byte_array, byte_array.Length));
    

    results in the following output:

    enter image description here

    Important is that you specify how many bytes from the array should be printed in 1 line.

    You need to get your data from the SerialPort into a byte array. I would suggest to try the following code:

    SerialPort serialPort1 = new SerialPort();
    
    byte[] byte_array = new byte[1024];
    int counter = 0;
    
    while (serialPort1.BytesToRead > 0)
    {
        byte_array[counter] = (byte)serialPort1.ReadByte();
        counter++;
    }
    

    EDIT

    to be able to use the code from the Link, add a new class file to the project. And copy paste the code inside it. Make sure that the *.cs class file is in the same project as your Form1 class.

    EDIT 2:

    If you receive the HEX numbers as string. you could just convert it yourself to a byte array and then use the Utils class from you link to display it. Here is a small example code to accomplish this:

    string rs = "0004";   // example from your code
    
    byte[] byte_array = new byte[rs.Length/2]; // array is half as long as your string
    int idx = 0; // count variable to index the string
    int cnt = 0; // count variable to index the byte array
    
    while (idx < rs.Length-1)
    {
        byte_array[cnt] = (byte)Convert.ToInt32(rs.Substring(idx, 2), 16);
        idx += 2;
        cnt++;
    }
    

    Then you can print the result like this:

    textBox4.Text += Utils.HexDump(byte_array, cnt) + Environment.NewLine;