2013-03-08 45 views
2

我想为Arduino编写一个简单的库来解析和解释串行命令。我使用示例库的目标是读取预期的命令并打开一些LED。我已经通过arduino获得了串行通信的工作,并且我希望它可以通过库来处理。比如...我有下面的代码在我的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文件。我不确定该从哪里出发,有什么想法?

回答

3

首先改变你的

#ifndef serialComms 
#define serialComms 

#ifndef serialComms_h 
#define serialComms_h 

你不能有相同的名称作为实例的宏。

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


请确保关闭了所有的Arduino IDE的做一个新的图书馆目录,并在它的初始文件,第一次的时候。启动时的IDE会缓存文件列表。他们随后可以在那里改变内部。但直到下一次启动才会知道新文件。


以下编译适合我。一旦我纠正所有的错字的:

definetest.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); 
    } 
} 
+0

更改的,得到了​​这些错误“testingLibraries:2 :错误:'serialComms'没有命名一个类型' 'testingLibraries.ino:在函数'void serialEvent()':' 'testingLibraries:16:错误:'测试'未在此范围内声明' – Bubo 2013-03-09 01:30:25

+0

添加了代码,并修复了错别字。 – mpflaga 2013-03-09 01:50:40

+0

这工作完美,谢谢,我现在有麻烦试图传递数组,我需要使用指针来做到这一点?我不确定 – Bubo 2013-03-12 03:55:06