【发布时间】: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