I would like to pass an array of bytes to a function but I got an error when compiling.
I need the array in order to use CRC16.h because it wants uint8_t type data.
I already tried to put byte; array byte; byte* in void calcCRC() but I always got this error:
variable or field 'calcCRC' declared void
comm_modbus.cpp:
#include <Crc16.h>
Crc16 crc;
#include "Arduino_MachineControl.h"
using namespace machinecontrol;
#include "comm_modbus.h"
byte SlaveID = 0x01;
byte FunctionID;
byte addressHI;
byte addressLO;
byte quantityHI = 0x00;
byte quantityLO = 0x02;
byte totalByteWrite = 0x04;
byte Value24;
byte Value16;
byte Value8;
byte Value0;
byte crcHI;
byte crcLO;
int trameRecus[9];
int trameTraiter [9];
byte check[2];
void initModbus() {
Serial.begin(9600);
// Wait for Serial or start after 2.5s
for (auto const timeout = millis() + 2500; !Serial && timeout < millis(); delay(500))
;
delay(2500);
Serial.println("Start RS485 initialization");
// Set the PMC Communication Protocols to default config
comm_protocols.init();
// RS485/RS232 default config is:
// - RS485 mode
// - Half Duplex
// - No A/B and Y/Z 120 Ohm termination enabled
// Enable the RS485/RS232 system
comm_protocols.rs485Enable(true);
// Specify baudrate, and preamble and postamble times for RS485 communication
comm_protocols.rs485.begin(19200,SERIAL_8E1 ,0, 500);
// Start in receive mode
comm_protocols.rs485.receive();
Serial.println("Initialization done!");
}
long ModbusRead(unsigned int addrMB) {
FunctionID = 0x03;
addrModbus(addrMB);
byte trame [] = {SlaveID,FunctionID,addressHI,addressLO,quantityHI,quantityLO};
calcCRC(trame);
byte trameComplet []={SlaveID,FunctionID,addressHI,addressLO,quantityHI,quantityLO,crcHI,crcLO};
// Disable receive mode before transmission
comm_protocols.rs485.noReceive();
comm_protocols.rs485.beginTransmission();
comm_protocols.rs485.write(trameComplet[0]);
comm_protocols.rs485.write(trameComplet[1]);
comm_protocols.rs485.write(trameComplet[2]);
comm_protocols.rs485.write(trameComplet[3]);
comm_protocols.rs485.write(trameComplet[4]);
comm_protocols.rs485.write(trameComplet[5]);
comm_protocols.rs485.write(trameComplet[6]);
comm_protocols.rs485.write(trameComplet[7]);
comm_protocols.rs485.endTransmission();
// Re-enable receive mode after transmission
comm_protocols.rs485.receive();
auto const timeout = millis() + 500;
while (comm_protocols.rs485.available() < 9) {
if (millis()> timeout) {
Serial.println("communication timeout!");
Serial.println("erreur : ");
while(comm_protocols.rs485.available()) {
Serial.println(comm_protocols.rs485.read());
}
break;
}
}
if (comm_protocols.rs485.available()==9) {
long value;
reception();
value = VARvalue();
return value;
}
}
void ModbusWrite(unsigned int addrMB, long ValueMB) {
FunctionID = 0x10;
addrModbus(addrMB);
ValueModbus(ValueMB);
byte trame [] = {SlaveID,FunctionID,addressHI,addressLO,quantityHI,quantityLO,totalByteWrite,Value24,Value16,Value8,Value0};
calcCRC(trame);
byte trameComplet []={SlaveID,FunctionID,addressHI,addressLO,quantityHI,quantityLO,totalByteWrite,Value24,Value16,Value8,Value0,crcHI,crcLO};
comm_protocols.rs485.noReceive();
comm_protocols.rs485.beginTransmission();
comm_protocols.rs485.write(trameComplet[0]);
comm_protocols.rs485.write(trameComplet[1]);
comm_protocols.rs485.write(trameComplet[2]);
comm_protocols.rs485.write(trameComplet[3]);
comm_protocols.rs485.write(trameComplet[4]);
comm_protocols.rs485.write(trameComplet[5]);
comm_protocols.rs485.write(trameComplet[6]);
comm_protocols.rs485.write(trameComplet[7]);
comm_protocols.rs485.write(trameComplet[8]);
comm_protocols.rs485.write(trameComplet[9]);
comm_protocols.rs485.write(trameComplet[10]);
comm_protocols.rs485.endTransmission();
// Re-enable receive mode after transmission
comm_protocols.rs485.receive();
}
void addrModbus(unsigned int addresseMB) {
addressHI = (addresseMB >> 8) & 0xFF; //highbyte
addressLO = (addresseMB & 0xFF); //lowbyte
}
void ValueModbus(long valMB) {
Value24 = (valMB & 0xFF000000) >> 24;
Value16 = (valMB & 0x00FF0000) >> 16;
Value8 = (valMB & 0x0000FF00) >> 8;
Value0 = valMB & 0xFF;
}
void calcCRC(byte *trame) {
unsigned short crcTrame;
int sizeTrame= sizeof(trame);
crc.clearCrc();
for(int i=0;i< sizeTrame;i++)
{
Serial.print("byte ");
Serial.print(i);
Serial.print(" = ");
Serial.println(trame[i],HEX);
crc.updateCrc(trame[i]);
}
crcTrame = crc.Modbus(trame,0,sizeTrame);
check[1] = (crcTrame >> 8) & 0xFF; //highbyte
check[0] = (crcTrame & 0xFF); //lowbyte
crcHI = check[0],HEX;
crcLO = check[1],HEX;
Serial.print("Modbus crcHI = 0x");
Serial.println(crcHI,HEX);
Serial.print("Modbus crcLO = 0x");
Serial.println(crcLO,HEX);
}
long VARvalue() {
long Value = 0;
int expo = 0;
int multi = 0;
for( int i = 3 ;i <= trameRecus[2]+2;i++) {
expo = 6-i;
multi = pow(256,expo);
trameRecus[i]= trameRecus[i] * multi;
Value = Value + trameRecus[i];
}
Serial.print("valeur reel = ");
Serial.println(Value);
return Value;
}
void reception() {
int compteur = 0;
byte ValueModbus;
while (comm_protocols.rs485.available()> 0) {
ValueModbus = comm_protocols.rs485.read();
Serial.println("Reception :");
Serial.println(ValueModbus);
trameRecus[compteur] = ValueModbus;
compteur++;
}
}
comm_modbus.h:
void initModbus();
long ModbusRead(unsigned int);
void ModbusWrite(unsigned int, long);
void addrModbus(unsigned int);
void ValueModbus(long);
void calcCRC(byte*);
long VARvalue();
void reception();
The error happend at this line: void calcCRC(byte*);
UPDATE:
I commented out the function //void calcCRC(byte*); and did the same for the call function //calcCRC(trame);.
After that, the error was no longer there, so I concluded this error is in the function calcCRC(trame) and I think the problem comes from data type void calcCRC(byte*); in comm_modbus.h brcause when I replace it with int the error disappears.
So why header file can't get byte into argument function?
ModbusWrite. Everything. Delete its code. Try compiling it now, do you get the same compilation error? If yes: take the next function you've shown, delete all of its code, repeat. Still get the same compilation error? Remove more code. Keep removing code until you end up with something that can't be removed without affecting the error. Now, take what you have left, edit your question, and replace this massive code dump with your minimal reproducible example. Unfortunately, Stackoverflow is not a debugging service. If you're asking a specifc error, everything else is irrelevant.#include <Arduino.h>at the top of the cpp