将字节数组传递到函数头文件中



我想把一个字节数组传递给一个函数,但在编译时出错了。

我需要数组才能使用CRC16.h,因为它需要uint8_t类型的数据。

我已经尝试过把byte; array byte; byte*放在void calcCRC()中,但我总是得到这个错误:

变量或字段'calcCRC'声明为无效

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_mobus.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();

错误发生在这一行:void calcCRC(byte*);

更新:

我注释掉了函数//void calcCRC(byte*);,并对调用函数//calcCRC(trame);做了同样的操作。

在那之后,错误不再存在,所以我得出结论,这个错误在函数calcCRC(trame)中,我认为问题来自comm_modbus.h br中的数据类型void calcCRC(byte*);,因为当我用int替换它时,错误消失了。

那个么,为什么头文件不能将字节放入参数函数中呢?

我不知道为什么会出现这个错误,但这是:

void calcCRC(byte *trame){

unsigned short crcTrame;
int sizeTrame= sizeof(trame);

毫无意义。trame只是一个指针,不包含它所指向内容的长度信息。sizeof(trame)为您提供指针的大小,该大小将始终相同(2、4或8,具体取决于您的架构(。你需要沿着电车走一段路。

此外,在那之后你的代码也毫无意义。您正在计算transe字节的默认xmodem CRC,然后忽略它,而是计算modbus CRC。

相关内容

  • 没有找到相关文章

最新更新