Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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)   // Here i send a byte to MK
        {

            //  serialPort1.RtsEnable = true; serialPort1.DtrEnable = true;
            //  var content = new List<byte>();
            // content.AddRange(Encoding.ASCII.GetBytes("1"));
            //  content.Add(3); // ASCII ETX
            //byte[] buffer = content.ToArray();
            // serialPort1.Write(buffer, 0, buffer.Length);


            //byte[] MyMessage = System.Text.Encoding.UTF8.GetBytes("2");
            //serialPort1.Write(MyMessage, 0, MyMessage.Length);

            serialPort1.Write("\u0005");
            serialPort1.Write(Convert.ToString(0x05));


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)  // choosing a right com port
        {
            serialPort1.PortName = textBox1.Text;
            serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);

        }
       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;
            

        }

        private void button3_Click(object sender, EventArgs e)   // OPen port
        {
            serialPort1.Open();
        }

        private void button4_Click(object sender, EventArgs e)  // Close port
        {
            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                   // 8 characters for the address
                + 3;                  // 3 spaces

            int firstCharColumn = firstHexColumn
                + bytesPerLine * 3       // - 2 digit for the hexadecimal value and 1 space
                + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
                + 2;                  // 2 spaces 

            int lineLength = firstCharColumn
                + bytesPerLine           // - characters to show the ascii value
                + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)

            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();
        }
    }
}
Posted
Updated 8-Feb-17 21:29pm
Comments
Ralf Meier 9-Feb-17 0:57am    
I'm not sure what you doing there ...
Do you get any data and it isn't converted in the right way ?
Or don't you get any data out of the SerialPort ?
In the Moment it doesn't may sense for me what you write to the SerialPort inside the Button1_Click-Method.

Perhaps you explain a Little bit more ...
JeezyWonder 9-Feb-17 1:40am    
Sorry for missunderstadings, please , dont look on buttons method, i use them for other commands, i getting data immediatly from my microcontroller(the code for him is already wrote by me in avr compile, therefore when i turn on my windows form and coonnect uart from pc to mk i get data in hex from mk's eeprom) and this data will be shown on textBox4, but it despaled in the stardart way( see the picture). But i want it to be displaed in HexDump way, colums(see the link), not lines. THanks
Ralf Meier 9-Feb-17 1:56am    
Sorry ... I misunderstood your issue ... :(
When I look to your serialPort1_DataReceived-Method you don't use the HexDump-Method from your code.
I suggest that you use that instead of the Converter System.Text.Encoding.ASCII.GetString (which works like you see ...)

1 solution

Actually, the Utils.HexDump method does correctly its job (I tried it with random data).
However, I don't see, in the code you posted, how are you using it.
 
Share this answer
 
Comments
JeezyWonder 9-Feb-17 22:33pm    
yeah, i know, i dont know how to use it(
i have a textbox4 where i want to display it
CPallini 10-Feb-17 2:51am    
Simply collect the data coming from the serial line, call the Hexdump method and then display the result in the TextBox. you need to se it multiline, see, for instance:
https://msdn.microsoft.com/en-us/library/a5hcydxx(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900