2013-03-12 55 views
0

我想寫一個讀取5個變量的庫,然後通過串口將它們發送給藍牙接收器,我得到了一些錯誤,我不知道從哪裏去,我需要實現指針嗎?在庫中傳遞數組Arduino

下面是Arduino的代碼....

#include <serialComms.h> 
serialComms testing; 

void setup() 
{ 
Serial.begin(9600); 
} 
char data[] = {1,2,3,4,5,6}; 

void loop() 
{ 

for(int t = 0;t<6;t++) 
{ 
    data[t] = data[t]++; 
} 
    testing.updateUser(data); 
    delay(250); 

} 

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::readAllBytes() // Target Pin,Values 
{ 
} 
void serialComms::assignBytes() 
{ 
    for(int t = 0;t<5;t++) 
    { 
    digitalWrite(12,HIGH); 
    delay(250); 
    digitalWrite(12,LOW); 
    } 
} 
void serialComms::updateUser(char t[]) 
{ 
    Serial.write(t,5); 
} 

serialComms.h

#ifndef serialComms_h 
#define serialComms_h 
/* serialComms Class */ 
class serialComms 
{ 
    public: 
     serialComms() {}; 
void init(); 
void readAllBytes(); // Will be used to create the array --> two variables for now... 
void assignBytes(); 
void updateUser(char t[]); 
    }; 
#endif 

下面是我收到的錯誤.. - serialComms.cpp:28:error:初始化虛擬size_t的參數1 print :: write(const uint8_t *,si ze_t)」

- 
- serialComms.cpp:28: error: invalid conversion from 'char*' to 'const uint8_t*' 
- serialComms.cpp: In member function 'void serialComms::updateUser(char*)': 
- serialComms.cpp:27: error: expected primary-expression before ']' token 
+0

哪裏serialComms.cpp的線28? – 2013-03-12 04:05:39

+0

+1因爲我在你的問題看到沒有錯誤和stackoverflow充滿了誰是熱衷於把-1,我不知道它解決什麼目的 – hazzelnuttie 2013-03-12 10:36:32

+0

我已經發布了所有的serialComms.cpp,我不知道這是爲什麼拋出這個錯誤 – Bubo 2013-03-12 13:20:56

回答

1

實施例:

void setup() 
{ 

    Serial.begin(9600); 
    char string_array[] = "hello"; 
    char data_array[] = {1,2,3,4,5,6}; 
    unsigned char data_array_uchar[] = {21,22,23,24,25,26}; 
    uint8_t uint8_array[] = {11,12,13,14,15,16}; 
    char alpha_array[] = {0x41,0x42,0x43,0x44,0x45,0x46}; 
    // take note that sizeof() is a precompile command... number of places/size of each place. 

    updateUserPrint(string_array); 
    updateUserWrite(data_array, sizeof(string_array)); 
    updateUserWriteUchar(data_array_uchar, sizeof(data_array_uchar)); 
    updateUserWriteUchar(uint8_array, sizeof(uint8_array)); 
    updateUserWriteUint(uint8_array, sizeof(string_array)); 
    updateUserAlpha(alpha_array, sizeof(string_array)); 
} 
void updateUserPrint(char *s) 
{ //note a string aka array of char's is ended with a null. 
    Serial.print(s); // this can detect. 
    Serial.println(); 
} 
void updateUserWrite(char *t, size_t len) 
{ //note an array of int's is not ended with a null. so you need to know how long it is. 
    for (int n = 0; n < len ; n++) { 
    Serial.print(t[n],DEC); 
    Serial.print(","); 
    } 
    Serial.println(); 
} 
void updateUserWriteUchar(unsigned char *t, size_t len) 
{ //note an array of int's is not ended with a null. so you need to know how long it is. 
    for (int n = 0; n < len ; n++) { 
    Serial.print(t[n],DEC); 
    Serial.print(","); 
    } 
    Serial.println(); 
} 
void updateUserWriteUint(uint8_t *t, size_t len) 
{ //note an array of int's is not ended with a null. so you need to know how long it is. 
    for (int n = 0; n < len ; n++) { 
    Serial.print(t[n],DEC); 
    Serial.print(","); 
    } 
    Serial.println(); 
} 
void updateUserAlpha(char *t, int len) 
{ //note an array of int's is not ended with a null. so you need to know how long it is. 
    for (int n = 0; n < len ; n++) { 
    Serial.write(t[n]); 
    } 
    Serial.println(); 
} 

產生以下:

hello 
1,2,3,4,5,6, 
21,22,23,24,25,26, 
11,12,13,14,15,16, 
11,12,13,14,15,16, 
ABCDEF 
+0

在Serial.write(t);這是如何工作的,我知道它指的是那裏的價值,但是它不需要增加? – Bubo 2013-03-12 22:17:53

+0

這幾乎沒有一個工作正常,你不能通過alphaArray updateUserAlpha,或者至少這是什麼arduino告訴我...它說,是不能轉換int8_t * char * ....任何建議? – Bubo 2013-03-12 23:37:16

+0

其實大部分工作。這是如何做指針的基本例子。間歇性地出現了一些錯字。但基本的原則都在那裏。你自己正確識別了錯誤。所以你得到了校長。我不確定爲什麼char和uint8_t不可互換。所以我添加了另一個例子以及錯字修正。這運行良好。注意,如果你的數組元素類型不是單字節,就像int16_t和更大,那麼實際長度將是元素數量的倍數。 – mpflaga 2013-03-14 01:29:24

1

Serial.write只能發送常量字符串像

​​

這就是爲什麼錯誤錯誤:從無效轉換 '的char *' 到 '常量uint8_t *'

使用盡可能

char temp[max_length]; 
sprintf(temp,"%s",t); 
Serial.write(temp); 
+0

所以,如果我想從一個arduino接收一組數據,那麼最好的方法是什麼? – Bubo 2013-03-12 13:22:05

+0

@VRKnight Serial.readBytesUntil()將串行緩衝區中的字符讀入數組。如果檢測到終止符字符,確定的長度已被讀取或超時(見Serial.setTimeout()),該函數終止。 – hazzelnuttie 2013-03-13 07:06:04

+0

請參考鏈接http://arduino.cc/en/Serial/ReadBytesUntil – hazzelnuttie 2013-03-13 07:06:24