创建数组 C++

Creating an Array C++

我有两个整数值:

v_y
v_x

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

我目前的代码无法正常工作:


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



........

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

我目前遇到两个错误:

a nonstatic member reference must be relative to a specific object

'Serial::WriteData':illegal call of non-static member function.

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

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

// 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