首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Arduino库中传递数组

在Arduino库中传递数组
EN

Stack Overflow用户
提问于 2013-03-12 12:03:53
回答 2查看 5.2K关注 0票数 0

我正在尝试编写一个库,读取5个变量,然后通过串口将它们发送到蓝牙接收器,我收到了许多错误,但我不确定从哪里开始,我需要实现指针吗?

这是Arduino代码...

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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:错误:初始化'virtual size_t Print::write(const uint8_t*,size_t)‘的参数1

代码语言:javascript
运行
复制
- 
- 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-12 21:22:35

示例:

代码语言:javascript
运行
复制
    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();
}  

生成以下内容:

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2013-03-12 18:33:13

Serial.write只能发送常量字符串,如

代码语言:javascript
运行
复制
Serial.write(“hello”);

这就是错误错误:从'char*‘到'const uint8_t*’的转换无效的原因。

用作

代码语言:javascript
运行
复制
char temp[max_length];
sprintf(temp,"%s",t);
Serial.write(temp);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15353092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档