C#根据用户输入更改字节数组的部分

C# Changing parts of byte array based on user input

我正在使用以下代码发送 API 帧:

byte[] bytesToSend5 = new byte[] 
{ 
    0x7E, 0x00, 0x10, 0x01, 0x00, 0x13, 
    0xA2, 0x00, 0x40, 0xA6, 0x5E, 0x23, 
    0xFF, 0xFE, 0x02, 0x44, 0x37, 0x04, 0x4D 
};
serialPort1.Write(bytesToSend5, 0, bytesToSend5.Length);

拆分成这样:

byte[] bytesToSend5 = new byte[] 
{ 
    5Startbits(won't change), 
    8IDbits(changes when a part of the device is swapped), 
    6determinationbits(tells the device what to do), 
    1checksumbit(calculated based on previous bits) 
};    

第一个代码示例符合当前产品的要求。如果出于某种原因需要更改设备的一部分,它将无法工作,因为 ID 位不合适。 ID号印在设备上,16位数字和字母,如“0013A20043A25E86”。

我想要做的是制作一个文本框,用户可以在其中输入新的 ID 号,它将被上述字节数组中的适当位替换。

这是我使用 Array.Copy 函数的尝试,试图在文本框中显示结果 - 但未检测到任何变化。我尝试输入“1”“1,2,3”等以及实际 ID 的“0013A20043A25E86”:

        string xbee_serienr = prop1_serienr.Text;
        byte[] front = { 0x7E, 0x00, 0x10, 0x17, 0x01 };
        byte[] back = { 0xFF, 0xFE, 0x02, 0x44, 0x37, 0x04 };
        string[] xbee = { xbee_serienr };
        byte[] combined = new byte[front.Length + xbee.Length + back.Length];
        Array.Copy(front, combined, front.Length);
        Array.Copy(back, 0, combined, 5, back.Length);
        var result = string.Join(",", combined.Select(x => x.ToString()).ToArray());
        OutputWindow.Text = result;

必须能够根据用户输入更改 8ID 位,并根据其余位计算最后的校验和位。

我已经在互联网上搜索并尝试了 Array.copy、Concat 等,但我没有取得任何进展。对此的任何指导或意见将不胜感激,即使这意味着指导我采取不同的方法。

编辑:

我现在在字节数组 "result" 中有了所需的信息,使用与下面大致相同的示例(为 var "xbee_serienr" 获取用户输入)。我现在想将其传递给如下所示的方法:

    private void button_D07_Lav_Click(object sender, EventArgs e)
    {
        byte[] bytesToSend5 = new byte[] { 0x7E, 0x00, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0xA6, 0x5E, 0x23, 0xFF, 0xFE, 0x02, 0x44, 0x37, 0x04, 0x4D };
        serialPort1.Write(bytesToSend5, 0, bytesToSend5.Length);

并使 "bytesToSend5" 使用另一种方法中的数组 "result"。

我试过使用 this 示例,如下所示:

byte result { get; set; } //above and outside of the two methods

var result = string.Join(string.Empty, combined.Select(x => x.ToString("X2")).ToArray()); //this is the end of the first method

private void button_D07_Lav_Click(object sender, EventArgs e)
    {
        byte[] bytesToSend5 = new byte[] { 0x7E, 0x00, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0xA6, 0x5E, 0x23, 0xFF, 0xFE, 0x02, 0x44, 0x37, 0x04, 0x4D };
        bytesToSend5 = result; //using the array stored in result instead of the one currently in bytesToSend5.
        serialPort1.Write(bytesToSend5, 0, bytesToSend5.Length);
    }

我意识到这里有一个明显的问题,它不在同一个表格上。这就是为什么我想拆分数组,在数组中的每一项前面加上0x,并用逗号分隔。

一旦我弄清楚了,我也会将它用于几个不同的设备,这让我担心会有很多重复的代码,但我怀疑一旦我理解了如何传递和使用数组以不同的方法,我总是可以 "duplicate" 每个设备的代码,因为不同设备的 ID 确实需要不同。

好吧,你永远不会添加已解析的字符串。

var xbee_serienr = "0013A20043A25E86";
byte[] front = { 0x7E, 0x00, 0x10, 0x17, 0x01 };
byte[] back = { 0xFF, 0xFE, 0x02, 0x44, 0x37, 0x04 };
var xbee = new byte[xbee_serienr.Length / 2];

for (var i = 0; i < xbee.Length; i++)
{
  xbee[i] = byte.Parse(xbee_serienr.Substring(i * 2, 2), NumberStyles.HexNumber);
}

byte[] combined;
using (var ms = new MemoryStream(front.Length + xbee.Length + back.Length))
{
  ms.Write(front, 0, front.Length);
  ms.Write(xbee, 0, xbee.Length);
  ms.Write(back, 0, back.Length);

  combined = ms.ToArray();
}

var result = string.Join(string.Empty, combined.Select(x => x.ToString("X2")).ToArray());

因为你是一个接一个地添加多个数组,所以我只用了一个MemoryStream。如果您已经准备好 byte[] (并且可变),则可以直接写入该字节数组并避免分配(和收集)额外的数组,但是当限制因素是UI 无论如何。