0

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
3
  • Where is line 28 of serialComms.cpp? Commented Mar 12, 2013 at 4:05
  • +1 cos i see nothing wrong in ur question and stackoverflow is filled with persons who are keen on putting -1 and i dont know what purpose it solves Commented Mar 12, 2013 at 10:36
  • I have posted all of serialComms.cpp, I'm not sure why this is throwing this error Commented Mar 12, 2013 at 13:20

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

At Serial.write(t); how does that work exactly, I understand that it's pointing to a value held there, but dosen't it need to increment?
Almost none of this works properly, you cannot pass alphaArray to updateUserAlpha, or at least that is what arduino is telling me...It says that is cannot convert int8_t* to char*....any suggestions?
Actually most of it worked. It was the basic examples of how to do pointers. Admittently there were a few typo's. But the basic principals are all there. You your self identified the errors correctly. So you got the principals. I am not sure why char and uint8_t are not interchangeable. So I add another example along with the typo fixes. This runs fine. Note if your arrays element types are not single byte, as with int16_t and larger then actual length will be such multiples of number of elements.
Added the char vs uint8_t. I always forget char is signed.
the moral to this. If you are going to use pointers. pay attention to type.
1

Serial.write can only send constant strings like

Serial.write(“hello”);

That is why the error error: invalid conversion from 'char*' to 'const uint8_t*'

use as

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

3 Comments

So, if I wanted to receive an array of data from an arduino, what would be the best method?
@VRKnight Serial.readBytesUntil() reads characters from the serial buffer into an array. The function terminates if the terminator character is detected, the determined length has been read, or it times out (see Serial.setTimeout()).
Thanks! that is what I was thinking also!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.