0

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

这是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: 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
4

2 回答 2

1

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

Serial.write(“hello”);

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

用于

char temp[max_length];
sprintf(temp,"%s",t);
Serial.write(temp);
于 2013-03-12T10:33:13.253 回答
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
于 2013-03-12T13:22:35.080 回答