Hey, I'm currently doing some project and facing some error that I don't know how to fix it. I'm making some crc 16 Modbus program that calculate the crc from array of data frame, I want the highbyte and lowbyte of the crc output separated. the output will automatically fill the crc data frame that required to request data from energy meter connected to Arduino using rs485.
here's the code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
//byte data[5];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
//CRC16 Function
byte* crc(byte data[]) {
byte check[2];
uint16_t reg_crc = 0xFFFF;
for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++)
{
reg_crc ^= data[i];
for (int j = 0; j < 8; j++);
{
if (reg_crc & 0x01) {
reg_crc = (reg_crc >> 1) ^ 0xA001;
}
else
{
reg_crc >>= 1;
}
}
}
check[1] = (reg_crc >> 8) & 0xFF; //highbyte
check[0] = (reg_crc & 0xFF); //lowbyte
return *check;
void readData( byte data[]) // requesting data to energy meter
{
byte *buffer = crc((byte)data[]);// calling crc function to calculate crc 16 and fill the output to buffer
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(10);
mySerial.write((byte)data[0]);
mySerial.write((byte)data[1]);
mySerial.write((byte)data[2]);
mySerial.write((byte)data[3]);
mySerial.write((byte)data[4]);
mySerial.write((byte)data[5]);
mySerial.write((byte)buffer[1]);// crc lowbyte automatically filled here
mySerial.write((byte)buffer[0]);// crc highbyte automatically filled here
delay(10);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
void loop() { // run over and over
byte test[]={0x00, 0x03, 0x00, 0x00, 0x00, 0x02}; //------> data frame
readData(test);
delay(5000);
while (mySerial.available()) {
Serial.write(mySerial.read());
}
while (Serial.available()) {
mySerial.write(Serial.read());
}
}
}
here's the error message from arduino, but i'm not sure the crc output will work properly even if the error solved
C:\Users\Control\Desktop\sketch_jul09a\sketch_jul09a.ino: In function 'void readData(byte*)':
sketch_jul09a:99:33: error: expected primary-expression before ']' token
byte *buffer = crc((byte)data[]);
^
exit status 1
expected primary-expression before ']' token
could you help me fix this problem, thanks for your help, and sorry if my english not good enough.