Arduino串行TX和RX消息

发布于 2025-02-11 04:15:29 字数 1329 浏览 1 评论 0原文

我正在使用Arduino和M5邮票( https://shop.m5stack.com/products/ M5STAMP-PICO-5PCS ),并尝试实现TX和RX消息的串行接口。 TX消息类似于0x55 0xcd 0x47 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x69 0x69 0x0d 0x0a和Rx Messages类似x00 0x03 0x00 0x00 0x00 0x2a 0x00 0x0c 0x00 0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x47 0x47 0x03 0xe3 0xe3 0x0d 0x0d 0x0a and Code> eys 40 hex。

我设法提出了一个令人讨厌的解决方案,其中包括40个十六进制,并打印出消息。

void receivemsgloop(void *pvParam)
{
  Serial.println("receivemsgloop start");
  int incomingByte = 0;
  unsigned char msg[40];
  int i = 0;
  while (1)
  {
    if (Serial2.available() > 0)
    {
      // read the incoming byte:
      incomingByte = Serial2.read();
      // counting the incomingByte and store in the array??
      if (i < 40)
      {
        msg[i] = incomingByte;
        i++;
      }
      else
      { //else array full and print out
        Serial.print("s2 received msg: ");
        for(int c=0;c<40;c++){
        Serial.println(msg[c],HEX);}
        Serial.println();
        i=0;
      }
    }
  }
  Serial.println("receivemsgloop end");
}

无论如何,是否在不使用阵列循环的情况下存储了十六进制阵列?我已经尝试了serial.ReadSting(),但实际上无法正常工作。谢谢。

I am using Arduino with M5 Stamp (https://shop.m5stack.com/products/m5stamp-pico-5pcs) and trying to implement a serial interface to tx and rx message . The Tx message is like 0x55 0xCD 0x47 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x69 0x0D 0x0A and the Rx message is like 0xAA 0x00 0x00 0x00 0x00 0x02 0xDA 0x00 0x00 0x03 0x32 0x08 0x8E 0x18 0xF6 0x00 0x03 0x00 0x00 0x00 0x2A 0x00 0x0C 0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x47 0x03 0xE3 0x0D 0x0A and always is 40 HEX.

I manage to come up with a nasty solution of counting 40 HEX with array and print the message out.

void receivemsgloop(void *pvParam)
{
  Serial.println("receivemsgloop start");
  int incomingByte = 0;
  unsigned char msg[40];
  int i = 0;
  while (1)
  {
    if (Serial2.available() > 0)
    {
      // read the incoming byte:
      incomingByte = Serial2.read();
      // counting the incomingByte and store in the array??
      if (i < 40)
      {
        msg[i] = incomingByte;
        i++;
      }
      else
      { //else array full and print out
        Serial.print("s2 received msg: ");
        for(int c=0;c<40;c++){
        Serial.println(msg[c],HEX);}
        Serial.println();
        i=0;
      }
    }
  }
  Serial.println("receivemsgloop end");
}

Is there have anyway store the HEX array without using the array loop? I have try the Serial.readSting() but not really working. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2025-02-18 04:15:29

更新了该功能,并添加了一个IF为检查结束序列的原因,如果结束序列不丢弃0x0d 0x0a。测试了一个晚上,看起来还可以。

void receivemsgloop(void *pvParam)
{
  Serial.println("receivemsgloop start");
  int incomingByte = 0;
  uint8_t msg[40];
  static String msg_out="";
  char *msg_out_1;
  int i = 0;

  while (1)
  {
    if (Serial2.available() > 0)
    {
      while (i < 40)
      { // read the incoming byte:
        if (Serial2.available())
        {
          incomingByte = Serial2.read();
          msg[i] = incomingByte;
          i++;
        }
      }

      Serial.print("s2 received msg: ");
      if (msg[38] == 0x0D && msg[39] == 0x0A)
      {
        for (int c = 0; c < 40; c++)
        {
          Serial.print(msg[c], HEX);
          msg_out += msg[c];
        }
      }

      Serial.println();
      Serial.println(msg_out);
      msg_out = "";
      i = 0;
    }

  }
  Serial.println("receivemsgloop end");
}

updated the function and added a if cause for checking the ending sequence if the ending sequence is not0x0D 0x0A discard it. Tested for one night and seem ok.

void receivemsgloop(void *pvParam)
{
  Serial.println("receivemsgloop start");
  int incomingByte = 0;
  uint8_t msg[40];
  static String msg_out="";
  char *msg_out_1;
  int i = 0;

  while (1)
  {
    if (Serial2.available() > 0)
    {
      while (i < 40)
      { // read the incoming byte:
        if (Serial2.available())
        {
          incomingByte = Serial2.read();
          msg[i] = incomingByte;
          i++;
        }
      }

      Serial.print("s2 received msg: ");
      if (msg[38] == 0x0D && msg[39] == 0x0A)
      {
        for (int c = 0; c < 40; c++)
        {
          Serial.print(msg[c], HEX);
          msg_out += msg[c];
        }
      }

      Serial.println();
      Serial.println(msg_out);
      msg_out = "";
      i = 0;
    }

  }
  Serial.println("receivemsgloop end");
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文