Solving “redeclared as different kind of symbol” e

2019-08-20 19:14发布

问题:

I'm currently working on Arduino. I'm working for Lamp using Atmega1284. I saw an example code, ModbusIP_ENC28J60 -> Lamp. I first compiled it without adding anything, it compiled properly. Now, I'm adding WebSocketServer, since I want this to work on websocket too. I added few necessary lines, but I ended up with this error: error: 'EthernetClass Ethernet' redeclared as different kind of symbol

I don't understand what's wrong with the code or what I should change. Can someone help me with this?

I'm pasting my code here for reference:

#include <EtherCard.h>
#include <Modbus.h>
#include <ModbusIP_ENC28J60.h>

#include <WebSocketsServer.h>

WebSocketsServer webSocketServer = WebSocketsServer(8080);

//Modbus Registers Offsets (0-9999)
const int LAMP1_COIL = 100;
//Used Pins
const int ledPin = 9;

//ModbusIP object
ModbusIP mb;

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
    switch(type) {
        case WStype_DISCONNECTED:
            Serial.println("[%u] Disconnected!\n");
            break;
        case WStype_CONNECTED:
            {
                //IPAddress ip = webSocket.remoteIP(num);
            Serial.println("[%u] Disconnected!\n");

        // send message to client
        //webSocket.sendTXT(num, "Connected");
            }
            break;
        case WStype_TEXT:
            Serial.println("[%u] got text!\n");

            // send message to client
            // webSocket.sendTXT(num, "message here");

            // send data to all connected clients
            // webSocket.broadcastTXT("message here");
            break;
        case WStype_BIN:
            Serial.println("[%u] get binary ");
            //hexdump(payload, lenght);

            // send message to client
            // webSocket.sendBIN(num, payload, lenght);
            break;
    }
}

void setup() {
    // The media access control (ethernet hardware) address for the shield
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    // The IP address for the shield
    byte ip[] = { 192, 168, 0, 120 };
    //Config Modbus IP
    mb.config(mac, ip);
    //Set ledPin mode
    pinMode(ledPin, OUTPUT);
    // Add LAMP1_COIL register - Use addCoil() for digital outputs
    mb.addCoil(LAMP1_COIL);

    webSocketServer.begin();
    webSocketServer.onEvent(webSocketEvent); 
}

void loop() {
   //Call once inside loop() - all magic here
   mb.task();

   //Attach ledPin to LAMP1_COIL register
   digitalWrite(ledPin, mb.Coil(LAMP1_COIL));

   webSocketServer.loop();
}

Help me to make it work.

回答1:

You are declaring Ethernet twice. And they are different.

First is probably in the include file Ethercard.h Second is Modbus.h

In the ModbusIP_ENC28J60 I found in github via Google they declare Ethernet as an array.

Either rename one declaration (e.g. ether vs Ethernet) or eliminate the use of one. Also, considering the include files in your source I would be surprised if there are only two conflicts.

C lesson: Declaring a variable for use by a function, very straightforward. When adding additional modules any name conflicts will cause problems. If you get two variables to agree but are still in the program you will suffer massive debugging headaches because one function will access its variable while the other will have its own, resulting in nothing actually working.

Go back and look at the source files (*.h). Search for "Ethernet" variables. See how they are declared and how they are used. The simplest solution is to pick the latest addition and change Ethernet to ether (as I suggested above).

Good Luck.