Please bear with me, I’m new at C and I’m trying to program an Arduino. I want to write a program that spits out a data frame of specific length with byte values ranging from 0-255. The minimum code to reproduce the error is in the code block below. When compiling I receive the following error:
sketch_apr09b.cpp: In function ‘char assembleFrame()’: sketch_apr09b.cpp:9:10: error: invalid conversion from ‘char*’ to ‘char’
Now my impression is that I’m mistreating the ‘return frame’, but I just can’t figure out what’s wrong.
char assembleFrame() {
char frame[] = {
0x61 , 0x62 , 0x63
};
return frame;
}
void setup() {
Serial.begin( 115200 );
};
void loop() {
char frame = assembleFrame();
Serial.print( frame );
}
When I run a hexdump on the receiving PC, I want to see:
00000000 61 62 63 |abc|
00000003
I’ve found a lot of similar questions, wasn’t able to figure out what I’m doing wrong.
EDIT:
This is what I came up with so far, but receiving wrong data. I think I’m sending the pointer to the actual data with this.
byte *assembleFrame() {
byte frame[] = { 4 , 'a' , 'b' , 'c' };
return frame;
}
void setup() {
Serial.begin( 115200 );
};
void loop() {
byte *frame = assembleFrame();
Serial.write( frame , frame[ 0 ] );
}
The
chartype is used to store a singlechar(a byte).Your function definition
char assembleFramespecifies the function will only be returning a singlechar, so when you try to return achar[]/char *(chararray/pointer), it fails.Looks like
Serial.print()can handle achar *, which will potentially need to be null-terminated (because it asks for no length specifier).