【问题标题】:(C#) How to make HexDump formating works with TextBox?(C#) 如何使 HexDump 格式与 TextBox 一起使用?
【发布时间】:2017-02-10 00:50:33
【问题描述】:

我在 Windows 窗体上有 TextBox,我成功地从 com-port 接收十六进制数据,但它显示在一行中,但我需要它以 HexDumpFormat 显示。

我现在得到的:

经过搜索,我找到了示例代码:Quick and Dirty Hex dump。这似乎是我需要的,TC 说我们只需要将他的函数粘贴到我的代码中,然后在需要的地方调用这个函数,但我很困惑如何让它与我的代码一起工作?非常感谢。 (我不能附加超过 3 个链接,因此您可以在链接页面上看到 HexDump 格式的外观。

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

【问题讨论】:

  • 您希望输出与链接中的完全相同吗?首先是二进制值,然后是用空格分隔的十六进制表示?
  • 其实没关系,谢谢回复。
  • 使用二进制会更好,但现在我很困惑,即使没有二进制也无法做到......
  • 十六进制数字是作为字符串发送的吗?是不是像这样:rs = "0004" ?
  • 实际上我从微控制器发送,其中代码使用 16 位二进制,但我配置 MK 将数据输出到 com 端口,而不是二进制,而是 .hex。无论如何,我可以再次配置它并发送它,因为它是 - 在二进制中,它看起来像 1 的“0000000000000001”,如果它会更容易做事

标签: c# serial-port hexdump


【解决方案1】:

我尝试了链接中的代码。这个简单的例子:

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

产生以下输出:

重要的是您指定应该在 1 行中打印数组中的多少字节。

您需要将您的数据从SerialPort 获取到一个字节数组中。我建议尝试以下代码:

SerialPort serialPort1 = new SerialPort();

byte[] byte_array = new byte[1024];
int counter = 0;

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

编辑

为了能够使用链接中的代码,请将新的类文件添加到项目中。并复制粘贴其中的代码。确保 *.cs 类文件与您的 Form1 类位于同一项目中。

编辑 2:

如果您收到 HEX 数字作为字符串。您可以自己将其转换为字节数组,然后使用链接中的Utils 类来显示它。这是一个小示例代码来完成此操作:

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

然后你可以像这样打印结果:

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

【讨论】:

  • 非常感谢,但是如何将结果打印到 textBox4 中?
  • 我试过 textBox4.Text = (Utils.HexDump(buffer));但不起作用
  • 我得到一个错误,“Utils”和“buffer”在这个上下文中不存在
  • void type(object s, EventArgs e) { textBox4.Text = (Utils.HexDump(buffer)); }
  • 你看到我的第二个例子了吗?您需要在代码中声明 byte[]。到目前为止,我没有看到任何东西。您需要创建一个新的 *.cs 文件并在其中复制 Utils 类代码。确保它与您的 Form1 类在同一个项目中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多