【问题标题】:Creating an Array C++创建数组 C++
【发布时间】:2021-04-13 12:32:42
【问题描述】:

我有两个 int 值:

v_y
v_x

我想转换为大小为 2 的字符数组,然后写入我的串行端口。

我目前有这段代码不起作用:


void Array2 (char charArray[], int sizeOfArray);



........

                        }
                        {
                            char  one[] = { 'v_x', 'v_y' };
                            Array2(one, 2);
                            Serial::WriteData(one, 2);
                        }
                    }

我目前遇到两个错误:

非静态成员引用必须相对于特定对象

'Serial::WriteData':非法调用非静态成员函数。

任何关于我做错了什么的帮助、提示或想法都会很棒!

编辑:我正在使用此代码与我的串口CODE进行通信

【问题讨论】:

  • 错误消息看起来与您的阵列无关。此外,v_x 不是char,而是char const*(如果您没有收到关于此的警告,请提高编译器的警告级别)。
  • 你用的是什么库?
  • 我正在为我的 Arduino 使用此代码与我的 C++ 代码进行通信,CODE
  • 好的,但是,您的意思是要传输字符串“v_x”和“v_y”还是要传输名为v_x和v_y的两个变量的值?
  • 对不起,如果我的问题不清楚,我想传输名为 v_x 和 v_y 的两个变量的值

标签: c++ arrays arduino


【解决方案1】:
// data.h or similar, you need this for both the arduino and desktop machine
struct Data {
   double v_x;
   double v_y;
}

在你的 arduino 上:写

Data data = {
   .v_x = 1.0, // Example put your values here
   .v_y = 2.0,
};

// This should write from your Arduino to the computer
Serial::WriteData((const char*)data, sizeof(data));

在您的计算机上

Data data;

// This reads from the serial port, and put the data in the struct
auto len = SP->ReadData((const char*)data, sizeof(data));

// check that data is the right size
// use the data from data.v_x and data.v_y

auto v_x = data.v_x;
auto v_y = data.v_y; // etc

【讨论】:

  • 谢谢,这很有帮助!
  • 乐于助人:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 2021-10-21
  • 2011-12-26
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多