I Need a help in this code
I am trying to assign the serial.write(OutData[i]) to byte data[]. Can anyone help me assign it.
Here is my code
#define ECHOPIN 11// Pin to receive echo pulse
#define TRIGPIN 12// Pin to send trigger pulse
int distance=0;
byte OutData[] = {0x05, 0x03, 0x04, 0x43, 0x65, 0xBA, 0x7D, 0x04, 0xE8};
#include <Crc16.h>
Crc16 crc;
void setup()\
{
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
}
void loop() {
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
distance= distance/58;
Serial.print(distance, HEX);
OutData[3] = 0x00;
OutData[4] = 0x00;
OutData[5] = 0x41;
OutData[6] = distance;
for (int i = 0; i <= sizeof(OutData); i++) {
Serial.write(OutData[i]);
byte data[] = OutData[i];
crc.clearCrc();
for(byte i=0;i<9;i++)
{
Serial.print("byte ");
Serial.print(i);
Serial.print(" = ");
Serial.println(data[i]);
crc.updateCrc(data[i]);
}
unsigned short value = crc.getCrc();
Serial.print("crc = 0x");
Serial.println(value, HEX);
Serial.println("The crc Check of the byte array");
//Modbus
value = crc.Modbus(data,0,9);
Serial.print("Modbus crc = 0x");
Serial.println(value, HEX);
while(true);
}
int calcrc(char *ptr, int count)
{
int crc;
char i;
crc = 0;
while (--count >= 0)
{
crc = crc ^ (int) *ptr++ << 8;
i = 8;
do
{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
} while(--i);
}
return (crc);
}
My Error is :
C:\Users\system3\AppData\Local\Temp\arduino_modified_sketch_307616\sketch_dec13a.ino: In function 'void loop()':
sketch_dec13a:34:25: error: initializer fails to determine size of 'data'
byte data[] = sizeof(OutData);
^
sketch_dec13a:34:25: error: array must be initialized with a brace-enclosed initializer
sketch_dec13a:60:1: error: a function-definition is not allowed here before '{' token
{
^
sketch_dec13a:77:1: error: expected '}' at end of input
}
^
Multiple libraries were found for "Crc16.h"
Used: C:\Users\system3\Documents\Arduino\libraries\Crc16-master
exit status 1
initializer fails to determine size of 'data'
2 Answers 2
The following code block does not make sense:
for (int i = 0; i <= sizeof(OutData); i++) {
Serial.write(OutData[i]);
byte data[] = OutData[i];
crc.clearCrc();
} // I added this bracket for you.
It is formally wrong to create an array this way. The compiler does not have any way to know, how big the array should be. I think the declaration with
[]
works only, when you initialize the array, but you are simply assigning 1 byte value to it.byte
is not the same as array ofbyte
. So this cannot work. To make it formally correct, you can either changedata
to a simplebyte
variable (not an array), or define the size of the array viabyte data[3];
and then assigning theOutData[i]
value to one of it's elements in the next line;Above I added the closing curly bracket for you after the first for loop. It seems, that you forgot it, thus you would get more errors due to imbalanced brackets.
You are create a variable/array inside your for loop, that is not used anywhere. As soon as the current for loop iteration is finished, the variable/array is out of scope and will vanish. You have to declare it outside of the for loop (either inside the
loop()
function or globally), so that you can use it in the following code.It seems, that you try to copy the data from the
OutData
array into thedata
array to output the values in the later code and calculate the CRC value. I don't see, why you are doing this. Is there a reason, why you could not do this directly with theOutData
array? I don't see the need for copying the data in an extra array.The following line can easily be a problem:
OutData[6] = distance;
distance
is defined as anint
, which is a 16bit/2byte signed number.OutData
is defined as an array ofbyte
, which is a 8bit/1byte unsigned number.pulseIn()
returns anunsigned long
(32bit/4byte unsigned number). Since you have configured a timeout of 26000, you won't get in conflict there, but easily, when trying to assign thedistance
value to an element ofOutData
. You can get distance values up to 448, but abyte
can only hold numbers between 0 and 255. So you will get an overflow and thus garbled data. You will have to send both bytes ofdistance
to always get the full value. (You can easily google how to get the individual bytes of an int; The Arduino framework even has defines for this,lowByte
andhighByte
, I think).
-
Thank you so muchJosh Earnest– Josh Earnest2019年12月13日 10:01:55 +00:00Commented Dec 13, 2019 at 10:01
-
@JoshEarnest If you think, that one of the answers is correct, you can mark it as the correct answer. That way others know, that this question was successfully answered and which of the answers helped you the mostchrisl– chrisl2019年12月14日 10:16:45 +00:00Commented Dec 14, 2019 at 10:16
byte data[] = OutData[i];
is an invalid statement.
You try to initialize an array with unknown size []
with a single element.
you must use an array to initialize an array and not a single element.
byte data[] = { 0xca, 0xfe, 0xba, 0xbe };
would initialize data with an array of four elements.
-
Thanks for your valuable commentJosh Earnest– Josh Earnest2019年12月13日 10:01:45 +00:00Commented Dec 13, 2019 at 10:01
Serial.read()
call in your code (and this method does not have a parameter, I think). Also you should include the complete error message, including information about the problematic line.