2

我正在尝试为 Arduino 编写一个简单的库来解析和解释串行命令。我对示例库的目标是读取预期的命令并打开一些 LED。我已经通过 arduino 进行串行通信,我希望它由库处理。例如...我的arduino上有以下代码

Arduino代码:

#include <serialComms.h>
serialComms testing = serialComms();
void setup()
{
 Serial.begin(9600); 
}

void loop() // not terribly concerned with the main loop, only the serialEvent, which I        have tested and works
{

}


void serialEvent()
{
   testing.readNewBytes();
   testing.assignBytes();
}

serialComms.cpp

#include <Arduino.h>
#include <serialComms.h>

void serialComms::init()
{
  // This is where the constructor would be...right now we are too stupid to have one
}

void serialComms::readNewBytes()  // Target Pin,Values
{
        digitalWrite(11,HIGH);
        delay(250);
        digitalWrite(11,LOW);
        assignBytes();

}

void serialComms::assignBytes()
{
    for(int t  = 0;t<5;t++)
    {
        digitalWrite(10,HIGH);
        delay(250);
        digitalWrite(10,LOW);
    }   
   }

serialComms.h

#ifndef serialComms_h
#define serialComms_h



/* serialComms Class */
class serialComms
{
  public:
    serialComms() {};
    void init();
    void readNewBytes(); // Will be used to create the array --> two variables for now...
    void assignBytes();
    };

#endif

我的问题如下...

1.) 我的库结构是否正确?当我发送消息并触发 serialEvent 时,我只想让 LED 闪烁,当我在 arduino 中运行代码时,会出现以下错误。

testingLibraries:2: error: 'serialComms' does not name a type
testingLibraries.ino: In function 'void serialEvent()':
testingLibraries:16: error: 'testing' was not declared in this scope

我在库文件夹中名为 serialComms 的文件夹中有 .cpp 和 .h 文件。我真的不确定从这里去哪里,有什么想法吗?

4

1 回答 1

3

首先改变你的

#ifndef serialComms
#define serialComms

#ifndef serialComms_h
#define serialComms_h

您不能拥有与实例同名的宏。

然后检查你的大小写,例如 readBytes vs testing.readbytes(); 注意 B


第一次在其中创建新的库目录和初始文件时,请确保关闭所有 Arduino IDE。IDE 在启动时缓存文件列表。他们可以随后改变那里的内部。但是直到下一次开始才会知道新文件。


以下对我来说编译得很好。一旦我纠正了所有错字:

定义测试.ino

#include <serialComms.h>
serialComms testing;

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

void loop() {
}

void serialEvent()
{
  testing.readBytes();
  testing.assignBytes();
}

serialComms.cpp

#ifndef serialComms_h
#define serialComms_h

/* serialComms Class */
class serialComms
{
  public:
//       serialComms() {};
void init();
void readBytes(); // Will be used to create the array --> two variables for now...
void assignBytes();
    };

#endif

serialComms.h

#include <Arduino.h>
#include <serialComms.h>

void serialComms::init()
{
  // This is where the constructor would be...right now we are too stupid to have one
}

void serialComms::readBytes()  // Target Pin,Values
{
  digitalWrite(11,HIGH);
  delay(250);
  digitalWrite(11,LOW);
  assignBytes();
}

void serialComms::assignBytes()
{
  for(int t  = 0;t<5;t++)
  {
    digitalWrite(10,HIGH);
    delay(250);
    digitalWrite(10,LOW);
  }   
}
于 2013-03-09T00:57:40.367 回答