如何将一组数据格式化为字节数组? [英] How to format a group of data into byte array?

查看:112
本文介绍了如何将一组数据格式化为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在研究需要通过TCP / IP发送数据显示在LED显示屏上的项目。供应商没有此LED的SDK,所以我现在尝试通过TcpClient .net库发送数据。



显示我的供应商提供的LED数据的命令格式是如下:

format = 0x01 +Z+00+ 0x02 +AA+ 0x06 +10+4500+ 0x01 +MyData+ 0x04



这是我向LED发送数据的示例代码。

Hello,

I am now working on the project that need to send data to show on LED display through TCP/IP. The supplier have no SDK for this LED so I am now trying to send data through TcpClient .net library.

Command format to display data on LED provided by my supplier is as follow:
format = 0x01 + "Z" + "00" + 0x02 + "AA" + 0x06 + "10" + "4500" + 0x01 + "MyData"+ 0x04

This is my example code to send data to LED.

private void SendData()
{
    TcpClient l_Client = new TcpClient();
    l_Client.Connect(txtIPAddress.Text, Convert.ToInt32(txtPortNumber.Text));
    NetworkStream l_stream = l_Client.GetStream();
    byte[] data = new byte[]{
        0x01, 
        Encoding.ASCII.GetBytes("Z")[0],
        Encoding.ASCII.GetBytes("0")[0],
        Encoding.ASCII.GetBytes("0")[0],
        0x02,
        Encoding.ASCII.GetBytes("AA")[0],
        Encoding.ASCII.GetBytes("AA")[1],
        0x06,
        Encoding.ASCII.GetBytes("1")[0],
        Encoding.ASCII.GetBytes("0")[0],
        0x0A,
        Encoding.ASCII.GetBytes("4")[0],
        Encoding.ASCII.GetBytes("5")[0],
        Encoding.ASCII.GetBytes("0")[0],
        Encoding.ASCII.GetBytes("0")[0],
        0x01,
        Encoding.ASCII.GetBytes("M")[0],
        Encoding.ASCII.GetBytes("y")[0],
        Encoding.ASCII.GetBytes("D")[0],
        Encoding.ASCII.GetBytes("a")[0],
        Encoding.ASCII.GetBytes("t")[0],
        Encoding.ASCII.GetBytes("a")[0],
        0x04
    };
    l_stream.Write(data, 0, data.Length);
}





之前我没有通过套接字将此类数据发送到硬件的经验。我认为我的代码绝对不对。特别是对于AA,4500,MyData。有没有简单明了的方式用提供的格式发送数据?



先谢谢了,
tslin



I have no experience sending such data to hardware through sockets before. And I think my code is definitely not right. Especially for "AA", "4500", "MyData". Is there any easy and clear way to send data with the provided format?

Thanks in advanced,
tslin

推荐答案

一种方法是将其创建为单个字符串,并带有嵌入的十六进制值:

One way to do it is to create it as a single string, with embedded hex values:
string format = "\x0001Z00\x0002AA\x0006104500\x0001MyData\x0004";
byte[] data = Encoding.ASCII.GetBytes(format);



将生成你想要的22字节数组:


That will generate the 22 byte array you want:

01
5A Z
30 0
30 0
02
41 A
41 A
06
31 1
30 0
34 4
35 5
30 0
30 0
01
4D M
79 y
44 D
61 a
74 t
61 a
04

它会起作用吗?不知道 - 那是标志!我会看看制造商是否在手册中或他的网站上有任何示例 - 因为这里的猜测可能需要很长时间! :笑:

Will it work? Dunno - that's down to the sign! I'd see if the manufacturer has any examples in the manual, or on his website - because guesswork here can take a seriously long time! :laugh:


你明白字符串 0x01 +Z+00+ 0x02 +AA+ 0x06 +10+4500 + 0x01 +MyData+ 0x04

我的意思是,00实际上是指两个0个字符,而不是 0x00 ?同样适用于AA:他们真的是两个A字符,还是他们宁愿 0xAA



您可能必须研究并理解char值(实际上是字节值,至少以ASCII为单位)与其字符串表示之间的区别。示例: 0xAA 是数字170,即以十六进制表示形式表示的字节值; AA是两个'A'字符,并由双字节 0x4141 存储在内存中(因为 0x41 是A字符的ASCII码。两者都没有相同的内存表示。



0x00 相同,这是一个单一的字节零值; 00是一个以双字节 0x3030 存储的字符串( 0x30 是'0'字符的ASCII码。



也许你的方法可以改写为:

Do you understand the string 0x01 + "Z" + "00" + 0x02 + "AA" + 0x06 + "10" + "4500" + 0x01 + "MyData"+ 0x04?
I mean, does "00" really mean two 0 characters, instead of 0x00? Same for "AA": are they really two A characters, or are they rather 0xAA?

You may have to study and understand the difference between a char value (which is in fact a byte value, at least in ASCII) and its string representation. Example: 0xAA is the number 170, i.e. a byte value expressed in its hexadecimal representation form; "AA" are two 'A' characters, and are stored in memory by the double-byte 0x4141 (because 0x41 is the ASCII code of the 'A' character). Both have not the same in-memory representation.

Same for 0x00, which is, well, a single byte zero value; "00" is a string stored with the double byte 0x3030 (0x30 is the ASCII code of the '0' character).

Maybe your method could be rewritten to:
private void SendData()
{
    TcpClient l_Client = new TcpClient();
    int port;
    if (!int.TryParse(txtPortNumber.Text, out port) || (port < 0) || (port > 65535))
    {
        // There is a problem with the port number
        throw new ArgumentOutOfRangeException("txtPortNumber.Text");
    }
    l_Client.Connect(txtIPAddress.Text, port);
    NetworkStream l_stream = l_Client.GetStream();
    byte[] data = new byte[]{
        0x01,
        Encoding.ASCII.GetBytes("Z")[0],
        0x00,
        0x02,
        0xAA,
        0x06,
        0x10,
        0x45,
        0x00,
        0x01,
        Encoding.ASCII.GetBytes("M")[0],
        Encoding.ASCII.GetBytes("y")[0],
        Encoding.ASCII.GetBytes("D")[0],
        Encoding.ASCII.GetBytes("a")[0],
        Encoding.ASCII.GetBytes("t")[0],
        Encoding.ASCII.GetBytes("a")[0],
        0x04
    };
    l_stream.Write(data, 0, data.Length);
}



(我包括一个基本的端口检查 - 绝不信任用户输入而不检查)



但是:这只是猜测;我没有足够的可靠信息,因为需要澄清LED供应商的预期。

这就是为什么你需要通过了解供应商真正期望你提供的东西来解决这个问题。一旦你理解了它,你的问题就会得到解决,因为你将能够提供正确的数据结构。



希望这会有所帮助。祝你好运。


(I included a basic port checking - never trust user inputs without checking)

BUT: it's just a guess; I do not have enough reliable information, as what is expected from the LED supplier needs to be clarified.
That's why you need to sort this out by understanding what the supplier really expects you to provide. Once you have understood it, your issue will be solved, because you will be able to provide it the right data structure.

Hope this helps. Good luck.


这篇关于如何将一组数据格式化为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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