本文介绍了(C#)如何使hexdump与串口一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在.hex中通过UART获取数据到TextBox,我收到stardart格式:
[]
但是我需要让它以HexDump格式显示,我在COdeProject中找到了一个例子
[]
并试过我自己,什么都没发生,好像我用错了,请帮忙,我现在真的很挣扎
我试过的:
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO.Ports;
命名空间WindowsFormsApplication8
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)//这里我向MK
发送一个字节{
// serialPort1.RtsEnable =真; 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)//选择正确的com端口
{
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)//接收数据
{
textBox4.Text + = rs;
}
private void button3_Click(object sender,EventArgs e)// OPEN port
{
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
{
公共静态字符串HexDump(byte [] bytes,int bytesPerLine = 16)
{
if(bytes == null)return< null>;
int bytesLength = bytes.Length;
char [] HexChars =0123456789ABCDEF.ToCharArray();
int firstHexColumn =
8 //地址
+ 3的8个字符; // 3个空格
int firstCharColumn = firstHexColumn
+ bytesPerLine * 3 // - 十六进制值和1个空格的2位数
+(bytesPerLine - 1)/ 8 // - 从第9个
+ 2起每8个字符增加一个空格; // 2个空格
int lineLength = firstCharColumn
+ bytesPerLine // - 显示ascii值的字符
+ Environment.NewLine.Length; //回车和换行(通常应为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] ='';
行[hexColumn + 1] ='';
line [charColumn] ='';
}
else
{
byte b = bytes [i + j];
行[hexColumn] = HexChars [(b>> 4)& 0xF];
行[hexColumn + 1] = HexChars [b& 0xF];
line [charColumn] =(b< 32?''':( char)b);
}
hexColumn + = 3;
charColumn ++;
}
result.Append(line);
}
返回result.ToString();
}
}
}
解决方案
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(); } } }
解决方案
这篇关于(C#)如何使hexdump与串口一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!