2017-09-15 137 views
0

我目前正在研究Arduino。我正在使用Atmega1284为Lamp工作。我看到一个示例代码,ModbusIP_ENC28J60 - > Lamp。我第一次编译它时没有添加任何内容,它编译正确。现在,我添加了WebSocketServer,因为我希望它也可以在websocket上工作。我添加了一些必要的行,但我结束了这个错误: error: 'EthernetClass Ethernet' redeclared as different kind of symbol解决“重新宣布为不同类型的符号”错误

我不明白什么是错的代码或我应该改变。有人可以帮我弄这个吗?

我在这里贴上我的代码以供参考:

#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(); 
} 

帮助我,使工作。

+0

您正在使用最新的库吗? – Sathiya

+0

是的。这些图书馆是新的。 –

回答

0

您声明以太网两次。他们是不同的。

首先可能是在包含文件Ethercard.h 二是Modbus.h

在ModbusIP_ENC28J60我通过谷歌在github上发现,他们声明以太网作为一个数组。

要么重命名一个声明(例如ether vs Ethernet),要么取消使用一个声明。另外,考虑到源文件中的包含文件,如果只有两个冲突,我会感到惊讶。

C课:声明变量供函数使用,非常简单。添加附加模块时,任何名称冲突都会导致问题。如果你有两个变量同意,但仍然在程序中,你将面临大量的调试头痛,因为一个函数将访问它的变量,而另一个函数将拥有它自己的变量,导致实际上没有任何工作。

回去看看源文件(* .h)。搜索“以太网”变量。看看他们是如何申报的,以及他们是如何使用的。最简单的解决方案是选择最新的添加并将以太网更改为以太网(如上所述)。

祝你好运。

+0

感谢您的回复。我实际上试图将名称从以太网更改为diffEthernet,但它使过程变得繁忙。因为,这个名字在很多地方都有使用。因此,改变标准库会使其变得困难。无论如何改变我的代码中的别名'以太网',而不改变它在标准库,以便我可以摆脱这个错误? –

+0

定义忙碌?您正在与两个与网络接口连接的不同库进行竞争。一个必须赢,这意味着另一个失去。 – dcy665

+0

你为什么要添加WebSocketServer?这还不清楚,尽管它可能是有效的。 – dcy665

相关问题