本文介绍了传递数组在图书馆的Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写,上面写着5个变量的库,然后将它们通过串行端口连接到蓝牙reciever,我得到了一些错误,我不知道在哪里何去何从,我是否需要实施指针?

下面是Arduino的code ....

 的#include< serialComms.h>
serialComms测试;无效设置()
{
 Serial.begin(9600);
}
CHAR数据[] = {1,2,3,4,5,6};无效循环()
{ 对于(INT T = 0; T< 6;吨++)
 {
  数据[T] =数据[T] ++;
 }
  testing.updateUser(数据);
  延迟(250);}

serialComms.cpp

 的#include< Arduino.h>
#包括LT&;&serialComms.h GT;无效serialComms ::的init()
{
  //这是构造将是...现在我们太傻有一个
}无效serialComms :: readAllBytes()//目标针,价值观
{
}
 无效serialComms :: assignBytes()
{
  对于(INT T = 0; T< 5;吨++)
  {
    digitalWrite(12,高);
    延迟(250);
    digitalWrite(12,低);
  }
}
无效serialComms :: UpdateUser两个(字符T [])
{
    Serial.write(T,5);
}

serialComms.h

 的#ifndef serialComms_h
#定义serialComms_h
/ * serialComms类* /
类serialComms
{
  上市:
       serialComms(){};
无效的init();
无效readAllBytes(); //将用于创建阵列 - >两个变量现在...
无效assignBytes();
无效UpdateUser两个(字符T []);
    };
#万一

下面是我收到的错误...
     - serialComms.cpp:28:错误:初始化的参数1虚拟打印为size_t写::(常量uint8_t有*,为size_t)

   - 
- serialComms.cpp:28:错误:从'字符*'到'常量uint8_t有*'无效的转换
- serialComms.cpp:在成员函数'无效serialComms :: UpdateUser两个(字符*)':
- serialComms.cpp:27:错误:预期主要-EX $ P $之前']'令牌pssion


解决方案

例如:

 无效设置()
{  Serial.begin(9600);
  CHAR string_array [] =你好;
  烧焦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;
  //注意到,sizeof的()是precompile命令每个地方的地方数量/大小。  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));
}
无效updateUserPrint(字符* S)
{//注意一个字符串又名成炭的是使用空结束数组。
  Serial.print(多个); //这个可以检测。
  Serial.println();
}
无效updateUserWrite(字符* T,为size_t LEN)
{//注意INT的阵列不与空结束。所以你需要知道它有多长。
  对于(INT N = 0; N< LEN; N ++){
    Serial.print(T [N],DEC);
    Serial.print(,);
  }
  Serial.println();
}
无效updateUserWriteUchar(无符号字符* T,为size_t LEN)
{//注意INT的阵列不与空结束。所以你需要知道它有多长。
  对于(INT N = 0; N< LEN; N ++){
    Serial.print(T [N],DEC);
    Serial.print(,);
  }
  Serial.println();
}
无效updateUserWriteUint(uint8_t有* T,为size_t LEN)
{//注意INT的阵列不与空结束。所以你需要知道它有多长。
  对于(INT N = 0; N< LEN; N ++){
    Serial.print(T [N],DEC);
    Serial.print(,);
  }
  Serial.println();
}
无效updateUserAlpha(字符* T,INT LEN)
{//注意INT的阵列不与空结束。所以你需要知道它有多长。
  对于(INT N = 0; N< LEN; N ++){
    Serial.write(T [N]);
  }
  Serial.println();
}

产生以下内容:

 你好
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

I am trying to write a library that reads 5 variables, then sends them through the serial port to a bluetooth reciever, I am getting a number of errors and I am not sure where to go from here, do I need to implement pointers?

Here is the Arduino code....

#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

Here are the errors that I am getting... - serialComms.cpp:28: error: initializing argument 1 of 'virtual size_t Print::write(const uint8_t*, size_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
解决方案

Example:

    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();
}  

produces the following:

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

这篇关于传递数组在图书馆的Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:33