2013-03-08 2 views
2

Arduino가 직렬 명령을 구문 분석하고 해석하기위한 간단한 라이브러리를 작성하려고합니다. 예제 라이브러리를 사용하는 나의 목표는 예상되는 명령을 읽고 일부 LED를 켜는 것입니다. 나는 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와 testing.readbytes(); 당신이 밖으로 닫고 있는지 확인 B


을 통지 모든 아두 이노 IDE의 처음 NEW 라이브러리 디렉토리와 그의와 초기 파일을 만들 때. 시작시 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 : error : 'serialComms'의 이름이 ' '입니다. testingLibraries.ino : 'void serialEvent()': ' 'testingLibraries : 16 : 오류 :이 범위에서'testing '이 (가) 선언되지 않았습니다.' – Bubo

+0

오타가 수정 된 코드가 추가되었습니다. – mpflaga

+0

이것은 완벽하게 작동했습니다. 감사합니다. 지금 배열을 전달하는 데 문제가 있습니다. 포인터를 사용해야합니까? 확실하지 않아요 – Bubo