来自SerialPort的字符串,转换为字节数组,然后与其他字节进行比较

时间:2016-09-06 10:18:37

标签: c# bytearray

我可以从一个字节到串口7进8或8字节。现在这些字节的形式如0x06(ACK)或0x15(NAK)到一个字节流,如0x1A,0x56,0xF7,0x01,0xC3等......

现在我的代码更像是这样:

    private void mySerialPort_DataReceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        rxString = mySerialPort.ReadExisting();
        this.Invoke(new EventHandler(DisplayText));
    }

    private void DisplayText(object sender, EventArgs e)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(rxString);

        tb_Status.Clear();

        if (bytes = (0x00, 0x00, 0x00, 0x06))
        {
           tb_Status.AppendText("Ok, we are GOOD!!!");
        }
    }

但是我得到了错误"严重性代码描述项目文件行 错误CS0029无法隐式转换类型' int'到' byte []'""

因此,当我有一个字节数组(从SerialPort编码为一个字符串)时,如何识别数组中的一个特定字节,然后将其与另一个字节进行比较?

由于

2 个答案:

答案 0 :(得分:1)

试试这个:

if(bytes.SequenceEquals(new byte[] { (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x06}))

答案 1 :(得分:0)

您正在尝试将数组与int进行比较。

您可以遍历数组成员或使用SequenceEqual

{
   "_id": {
       "$oid": "22383cef627ff2cc33ad5990"
   },

  "name": "data1",

  "users": [
   {
      "$ref": "user",
      "$id": {
          "$oid": "16a5fbcee4b0c2c2da3017ef"
      }
   },
   {
      "$ref": "user",
      "$id": {
          "$oid": "1795ff86e4b09fc66416cd2f"
       }
   },
 ],
},
相关问题