简体   繁体   English

Arduino和CPP文件通讯

[英]Arduino and cpp file communication

I've connected a sensor with my Arduino board and am running a sketch which retrieves some data from the sensor and stores it in 4 double variables. 我已经将传感器与Arduino开发板连接起来,正在运行一个草图,该草图从传感器中检索一些数据并将其存储在4个双变量中。 I need to access these 4 variables from another .cpp file. 我需要从另一个.cpp文件访问这4个变量。

To do this I've created a common header file for both which declares 4 extern variables. 为此,我为两者创建了一个公共头文件,该文件声明了4个extern变量。 These are then defined in the arduino sketch. 然后在arduino草图中定义它们。 The problem with this is that when I try to access the variables from the .cpp file, a compiler error is stating that they are undefined. 这样做的问题是,当我尝试从.cpp文件访问变量时,编译器错误指出它们是未定义的。

The arduino sketch : arduino草图:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <varDec.h>

#define BNO055_SAMPLERATE_DELAY_MS (1000)

double x,y,z,w;

Adafruit_BNO055 bno = Adafruit_BNO055();

void setup()
{
  Serial.begin(9600);

  if(!bno.begin())
  {
    Serial.println("Not connected");
    while(1);
  }

  bno.setExtCrystalUse(false);

}

void loop()
{
  imu::Quaternion quat = bno.getQuat();

  x = quat.x();
  y = quat.y();
  z = quat.z();
  w = quat.w();

  uint8_t system, gyro, accel, mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);  

  delay(BNO055_SAMPLERATE_DELAY_MS);

}

The cpp file : cpp文件:

#include <unistd.h> // sleep()
#include <stdio.h>  // fopen(), fclose(), fprintf(), perror()
#include <stdlib.h> // exit() and EXIT_FAILURE
#include <iostream>
#include "/home/matthew/sketchbook/libraries/Custom/varDec.h"

using namespace std;

int main()
{   
   cout << x; 
   getchar();

   getchar();

    return(0);
} // end function: main

And the header : 和标题:

extern double x;
extern double y;
extern double z;
extern double w;

From the structure of what I am reading, I guess, what you call "another .cpp file" is a program that runs on a computer.... 根据我正在阅读的结构,我想您所说的“另一个.cpp文件”是在计算机上运行的程序。

If that is the case, then what you are trying to do will never work. 如果真是这样,那么您尝试做的将永远无法执行。 variables, are named spaced of allocated memory. 变量被命名为分配的内存间隔。 and a program running on a computer will not get access to allocated memory on the arduino. 并且计算机上运行的程序将无法访问arduino上分配的内存。

The easiest way to get the values from the sensors to the program running on your computer is to program the arduino to write the values to the serial port. 从传感器获取值到计算机上运行的程序的最简单方法是对arduino进行编程,以将值写入串行端口。 (here you find good examples on how to send values of different format to the serial port : https://www.arduino.cc/en/serial/print ) (在这里您可以找到有关如何将不同格式的值发送到串行端口的好示例: https : //www.arduino.cc/en/serial/print

And the program running on your computer should read the serial port for the expected values. 并且计算机上运行的程序应读取串行端口的期望值。 reading the serial port on your computer, isn't a hard task. 读取计算机上的串行端口并非易事。 but it is system dependant. 但这取决于系统。 there is plenty of doc on the internet) 互联网上有很多文档)

Good luck my friend ;-) 祝你好运,我的朋友 ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM