1

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'
  • I don't see any 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. – chrisl Dec 13 '19 at 9:07
  • I am really sorry its Serial.write(). And the complete error message is initializer fails to determine size of 'data' – Josh Earnest Dec 13 '19 at 9:21
  • With complete error message I meant the complete output of the compiler or at least the complete section, where the error is described. When the compiler finds an error, it also outputs, where it found the error. That makes it easier for us to find the problem. Also sometimes the error is caused by a previous error, so the complete compiler output is the best way to include all information, that might be relevant. – chrisl Dec 13 '19 at 10:07
  • 1
    I have added the additional error message which is showing now. – Josh Earnest Dec 13 '19 at 10:12
0

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.
  1. 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 of byte. So this cannot work. To make it formally correct, you can either change data to a simple byte variable (not an array), or define the size of the array via byte data[3]; and then assigning the OutData[i] value to one of it's elements in the next line;

  2. 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.

  3. 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.

  4. It seems, that you try to copy the data from the OutData array into the data 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 the OutData array? I don't see the need for copying the data in an extra array.

  5. The following line can easily be a problem:

    OutData[6] = distance;
    

    distance is defined as an int, which is a 16bit/2byte signed number. OutData is defined as an array of byte, which is a 8bit/1byte unsigned number. pulseIn() returns an unsigned 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 the distance value to an element of OutData. You can get distance values up to 448, but a byte 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 of distance 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 and highByte, I think).

| improve this answer | |
  • Thank you so much – Josh Earnest Dec 13 '19 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 most – chrisl Dec 14 '19 at 10:16
0

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.

| improve this answer | |
  • Thanks for your valuable comment – Josh Earnest Dec 13 '19 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.