C#串口读取HEX数据 [英] C# Serial Port reading HEX data

查看:657
本文介绍了C#串口读取HEX数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C#应用程序同时从多个串行COM端口读取,以分析IPOD的数据通信。正在发送的数据需要被解释为HEX字节。例如,

0xFF 0x55 0x01 0x00 0x04 0xC3 0xFF 0x55 ...

我希望能够例如:

  0xFF 0x55 0x01 0x00 0x04 0xC3 
0xFF 0x55读取此内容并将其显示在富文本框中。 。

一个命令的开始包含一个头(0xFF 0x55),其余的是命令+参数+校验和。



最好的方法是什么?



我目前有:

  private delegate void SetTextDeleg(string text); 

void sp_DataReceivedRx(object sender,SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
尝试
{
字符串数据= IPODRxPort.ReadExisting(); //这是否合适?
//在UI线程上调用委托,并将接收到的数据发送到被调用的方法。
// ----si_DataReceived方法将在允许填充文本框的UI线程上执行。
this.BeginInvoke(new SetTextDeleg(si_DataReceivedRx),new object [] {data});
}
catch
{}
}

private void si_DataReceivedRx(字符串数据)
{
int dataLength = data。长* 2;
double numLines = dataLength / 16.0;
for(int i = 0; i IPODTx_rtxtBox.Text + =\\\
;

IPODRx_rtxtBox.Text + = SpliceText(convertAsciiTextToHex(data),32)+\\\
;
}

我可以读取数据,但它的格式不正确。 p>

我只是不确定从com端口获取十六进制数据并根据命令头(0xFF 0x55)逐行显示它的最佳方式。 p>

任何建议?

解决方案

Alex Farber的方法有效。以下是我的代码示例:

  SerialPort sp =(SerialPort)sender; 
// string s = sp.ReadExisting();
// labelSerialMessage.Invoke(this.showSerialPortDelegate,new object [] {s});

int length = sp.BytesToRead;
byte [] buf = new byte [length];

sp.Read(buf,0,length);
System.Diagnostics.Debug.WriteLine(Received Data:+ buf);

labelSerialMessage.Invoke(this.showSerialPortDelegate,new object [] {
System.Text.Encoding.Default.GetString(buf,0,buf.Length)});


I am writing a C# application to read from several serial COM ports at the same time to analyze the data communication of an IPOD. The data being sent needs to be interpreted as HEX bytes. For example,

0xFF 0x55 0x01 0x00 0x04 0xC3 0xFF 0x55 ...

I want to be able to read this and display it in a rich textbox, for example

0xFF 0x55 0x01 0x00 0x04 0xC3
0xFF 0x55 ... 

The start of a command includes a header (0xFF 0x55) and the rest is is the command + parameters + checksum.

What is the best way to go about this?

I currently have:

private delegate void SetTextDeleg(string text);

void sp_DataReceivedRx(object sender, SerialDataReceivedEventArgs e)
{
    Thread.Sleep(500);
    try
    {
        string data = IPODRxPort.ReadExisting(); // Is this appropriate??
        // Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.
        // ---- The "si_DataReceived" method will be executed on the UI thread which allows populating of the textbox.
        this.BeginInvoke(new SetTextDeleg(si_DataReceivedRx), new object[] { data });
    }
    catch
    { }
}

private void si_DataReceivedRx(string data)
{
    int dataLength = data.Length*2;
    double numLines = dataLength / 16.0;
    for (int i = 0; i < numLines; ++i)
        IPODTx_rtxtBox.Text += "\n";

    IPODRx_rtxtBox.Text += SpliceText(convertAsciiTextToHex(data), 32) + "\n"; 
}

I can read data, but it is not in the appropriate format.

Im just not sure what the best way to get the hex data from the com port and display it line by line by command based on the command header (0xFF 0x55).

Any Suggestions?

解决方案

Alex Farber's method works. Below is my code example:

SerialPort sp = (SerialPort) sender;
// string s = sp.ReadExisting();
// labelSerialMessage.Invoke(this.showSerialPortDelegate, new object[] { s });

int length = sp.BytesToRead;
byte[] buf = new byte[length];

sp.Read(buf, 0, length);
System.Diagnostics.Debug.WriteLine("Received Data:" + buf);

labelSerialMessage.Invoke(this.showSerialPortDelegate, new object[] { 
    System.Text.Encoding.Default.GetString(buf, 0, buf.Length) });

这篇关于C#串口读取HEX数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆