global variable

Hello,

there is a lot of material about global variables, still I don't get this Code to compile. Maybe I'm stupid or blind, but I'm asking for your help in the matter. So please let me know, how to do this.

I want a Buffer variable to be global, means to be available in main/loop and all subroutines.

// global variables
//--------------------------------------------------
union {uint32_t buff32[6]; uint16_t buff16[13]; uint8_t buff8[26];} buff; // tx and rx buffer

No matter, how I declare it, the Compiler either says it is not declared in the subroutines or it is ambigous or double.

Please find the complete Code in the ZIP.

Thanks in Advance.

BR

Here the ZIP

Arduino_EffMeter_wCOMV4.zip (10.5 KB)

Full code not in the post
No details of the error

Not a good start

Please post a small but complete example of the problem

Klausi1967:
Here the ZIP

Just post the .ino file. I'm not downloading a strange ZIP file - goodness knows what's in it.

...R

Thanks for your quick answers.

OK, sorry, a ZIP is Always dangerous.

Attached is the *.ino now.

The Buffer variable is declared and defined in PZEMglobals.h as follows:
// global variables
//--------------------------------------------------
union {uint32_t buff32[6]; uint16_t buff16[13]; uint8_t buff8[26];} buff; // tx and rx Buffer

When done in that way, the Compiler spits:
buff was not declared in this scope for PZEM014.cpp and PZEM017.cpp

When I use the extern directive
extern union { … } buff;
in PZEM014.h and PZEM017.h,
then the Compiler spits
conflicting declaration ' buff'

Hope that explains the Problem?

BR

Arduino_EffMeter_wCOMV4.ino (23 KB)

Where did you get PZEM014.h and PZEM017.h ?

Klausi1967:
Attached is the *.ino now.

That's definitely an improvement. But, sorry, I have no intention of wading through a 23k file 90% of which is irrelevant to the question at hand. This sounds like a problem that could be illustrated in a program of about 20 lines.

Whenever I come across a technique I don't understand I write a very short program for learning purposes and I only incorporate the technique in a bigger program when I understand it properly.

...R

The PZEM014 and PZEM017, I wrote myself.

They work seperately from each other. However as I merged them into the bigger context they won't compile anymore.

The PZEM014 and PZEM017, I wrote myself.

The error message, or at least the little of it that you posted, refers to the error occurring in those files

Please post those files and the full error messages

OK, I tried to squeeze the *.ino together but still makeing sense in the complete context.

These are the error Messages:

sketch\PZEM014.cpp: In function 'void readPZEM014(uint8_t)':

PZEM014.cpp:44:3: error: 'buff' was not declared in this scope

buff.buff8[Addr] = PZEM014DAddr;

^

sketch\PZEM014.cpp: In function 'void readPZEM014Values(float&, float&, float&, float&, float&, float&, uint8_t&)':

PZEM014.cpp:110:10: error: 'buff' was not declared in this scope

if (buff.buff8[COM] != readDATA)

^

sketch\PZEM014.cpp: In function 'void readPZEM014Holding(uint16_t&, uint8_t&, uint8_t&)':

sketch\PZEM014.cpp:176:7: warning: 'readPZEM014Holding(uint16_t&, uint8_t&, uint8_t&):: buff', declared using anonymous type, is used but never defined [-fpermissive]

} buff;

^

sketch\PZEM014.cpp: In function 'void writePZEM014Holding(uint8_t, uint16_t)':

sketch\PZEM014.cpp:204:5: warning: 'writePZEM014Holding(uint8_t, uint16_t):: buff', declared using anonymous type, is used but never defined [-fpermissive]

} buff;

^

PZEM014.cpp:231:2: error: expected primary-expression before '/' token

*/

^

sketch\PZEM017.cpp: In function 'uint16_t ModRTU_CRC(uint8_t)':

PZEM017.cpp:39:3: error: 'buff' was not declared in this scope

buff.buff16[11] = 0xFFFF;

^

sketch\PZEM017.cpp: In function 'void readPZEM017(uint8_t)':

PZEM017.cpp:78:3: error: 'buff' was not declared in this scope

buff.buff8[Addr] = PZEM017DAddr;

^

sketch\PZEM017.cpp: In function 'void readPZEM017Values(float&, float&, float&, float&, uint8_t&)':

PZEM017.cpp:142:10: error: 'buff' was not declared in this scope

if (buff.buff8[COM] != readDATA)

^

PZEM017.cpp:179:7: error: 'buff' was not declared in this scope

if (buff.buff8[HVAL] == 0xFF) // High or Over Voltage Alarm

^

PZEM017.cpp:185:7: error: 'buff' was not declared in this scope

if (buff.buff8[LVAL] == 0xFF) // Low or Under Voltage Alarm

^

sketch\PZEM017.cpp: In function 'void readPZEM017Holding(float&, float&, uint8_t&, uint8_t&, uint8_t&)':

PZEM017.cpp:200:11: error: 'buff' was not declared in this scope

if (buff.buff8[COM] != readHOLDING)

^

sketch\PZEM017.cpp: In function 'void writePZEM017Holding(uint8_t, uint16_t)':

PZEM017.cpp:230:3: error: 'buff' was not declared in this scope

buff.buff8[Addr] = PZEM017HAddr;

^

sketch\PZEM017.cpp: In function 'void resetEnergy(uint8_t)':

PZEM017.cpp:335:7: error: 'buff' was not declared in this scope

buff.buff8[Addr] = PZEM017HAddr;

^

PZEM017.cpp:337:7: error: 'buff' was not declared in this scope

buff.buff8[Addr] = PZEM014HAddr;

^

PZEM017.cpp:338:5: error: 'buff' was not declared in this scope

buff.buff8[COM] = 0x42;

^

exit status 1
'buff' was not declared in this scope

Obviously, they Change, when I uncomment the delarations in the PZEM014.h and PZEM017.h

PZEM014.cpp (5.18 KB)

PZEM014.h (2.08 KB)

PZEM014RegDef.h (504 Bytes)

PZEM017.cpp (7.61 KB)

PZEM017.h (2.32 KB)

PZEM017RegDef.h (554 Bytes)

PZEMglobals.h (192 Bytes)

Test_PZEM.ino (1.22 KB)

PZEMglobals.h (192 Bytes)

PZEM017RegDef.h (554 Bytes)

PZEM017.h (2.32 KB)

PZEM017.cpp (7.61 KB)

Sorry, now files are in there double. My PC is a nerv wrecking Desaster. My Company IT Department put too much security on it...
It does not allow you to do Things and when you finally circumnavigated the security, it is double...

In any case, just omit the files after the *.ino

sketch\PZEM014.cpp: In function 'void readPZEM014(uint8_t)':

PZEM014.cpp:44:3: error: 'buff' was not declared in this scope

   buff.buff8[Addr] = PZEM014DAddr;

To save me wading through multiple files, where exactly is the buff object created ?

the union buff is created in PZEMglobals.h

I got it to compile now, but with warnings.

In globals.h I made the following change:

#ifndef PZEMglobals
#define PZEMglobals
union { // tx and rx buffer type
** uint32_t buff32[6];**
** uint16_t buff16[13];**
** uint8_t buff8[26];**
** } buff;**
#endif

in PZEM017.h I do this:

#include "PZEMglobals.h"

and in PZEM014.h I do this:
#define common extern
#include "PZEMglobals.h"
#undef common

As said, now it compiles, but a number of similar warnings are generated:
sketch\PZEM014.cpp: In function 'void writePZEM014Holding(uint8_t, uint16_t)':

sketch\PZEM014.cpp:245:5: warning: 'writePZEM014Holding(uint8_t, uint16_t):: buff', declared using anonymous type, is used but never defined [-fpermissive]

} buff;

Is that warning a problem?
How can I get rid of it?