Modbus TCP enc28j60 communication problem

SIlvesterrr
Posts: 4
Joined: Sun Jan 23, 2022 4:01 pm

Modbus TCP enc28j60 communication problem

Postby SIlvesterrr » Tue Aug 30, 2022 9:51 am

Hi, Im trying to setup Modbus TCP Slave on my esp32s3 using enc28j60 module.
Ive combined enc28j60 example with modbus slave tcp example.
Device gets address from dhcp without any problem.
.
.
When I want to read some data from device I do not get any response. Console clearly indicates that it recieved frame and accepted client communication but nothing else happends.
this is log on eth starup:

Code: Untitled.c Select all


I (3563) eth_example: Ethernet Got IP Address
I (3573) eth_example: ~~~~~~~~~~~
I (3573) eth_example: ETHIP:192.168.1.34
I (3583) eth_example: ETHMASK:255.255.255.0
I (3583) eth_example: ETHGW:192.168.1.1
I (3593) eth_example: ~~~~~~~~~~~
D (5673) MB_PORT_COMMON: vMBPortSetMode: Port enter critical.
D (5673) MB_PORT_COMMON: vMBPortSetMode: Port exit critical
W (5673) eth_example: Input reg. set to 000000
I (5673) MB_TCP_SLAVE_PORT: Protocol stack initialized.
D (5683) MB_PORT_COMMON: TCP Slave port enable.
I (5683) MB_TCP_SLAVE_PORT: Socket (#54), listener 192.168.1.34 on port: 502, errno=0
.
.
As you can see there seems to be no problem but after trying to communicate with device I do not get any response.
qmodubs_sc.png
qmodubs_sc.png (164.16 KiB) Viewed 2426 times
.
.
My console looks like this while trying to commuincate:

Code: Untitled.c Select all

I (165093) MB_TCP_SLAVE_PORT: Socket (#55), accept client connection from address: 192.168.1.12
D (165093) MB_TCP_SLAVE_PORT: | TID = 0001 | PID = 0000 | LEN = 0006 | UID = 01 | FUNC = 04 | DATA = 00000001 |
D (165103) MB_TCP_SLAVE_PORT: Socket (#55)(192.168.1.12), get packet TID=0x1, 12 bytes.
D (165103) MB_PORT_COMMON: EV_FRAME_RECEIVED
D (165113) MB_PORT_COMMON: 04 00 00 00 01
D (165123) MB_PORT_COMMON: eMBPoll:EV_EXECUTE
D (165123) MB_TCP_SLAVE_PORT: Client 0, Socket(#55), processing time = 22852 (us).
.
.
So it clearly recives frame but it doesn't do anything with it.
next data recieve attempt just drops the connection:

Code: Untitled.c Select all

E (163033) MB_TCP_SLAVE_PORT: Socket (#55)(192.168.1.12), connection closed by peer.
.
.
Here is my Code:

Code: Untitled.c Select all

#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "eth_enc28j60/esp_eth_enc28j60.h"

#include "mbcontroller.h"
void modbus_tcp(void);
void modbus_slave_task(void *pvParameter);

static const char *TAG = "eth_example";

/** Event handler for Ethernet events */
static void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
uint8_t mac_addr[6] = {0};
/* we can get the ethernet driver handle from event data */
esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;

switch (event_id)
{
case ETHERNET_EVENT_CONNECTED:
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
ESP_LOGI(TAG, "Ethernet Link Up");
ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down");
break;
case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet Stopped");
break;
default:
break;
}
}
// char *ip;
/** Event handler for IP_EVENT_ETH_GOT_IP */
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
const esp_netif_ip_info_t *ip_info = &event->ip_info;

ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
// modbus_tcp();
}
esp_netif_t *eth_netif;

void init_eth(void)
{

ESP_ERROR_CHECK(gpio_install_isr_service(0));
// Initialize TCP/IP network interface (should be called only once in application)
ESP_ERROR_CHECK(esp_netif_init());
// Create default event loop that running in background
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
eth_netif = esp_netif_new(&netif_cfg);

spi_bus_config_t buscfg = {
.miso_io_num = 39,
.mosi_io_num = 40,
.sclk_io_num = 41,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
/* ENC28J60 ethernet driver is based on spi driver */
spi_device_interface_config_t devcfg = {
.command_bits = 3,
.address_bits = 5,
.mode = 0,
.clock_speed_hz = 8 * 1000 * 1000,
.spics_io_num = 42,
.queue_size = 20,
.cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time(8),
};

spi_device_handle_t spi_handle = NULL;
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &devcfg, &spi_handle));

eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(spi_handle);
enc28j60_config.int_gpio_num = 38;

eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
mac_config.smi_mdc_gpio_num = -1; // ENC28J60 doesn't have SMI interface
mac_config.smi_mdio_gpio_num = -1;
esp_eth_mac_t *mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);

eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.autonego_timeout_ms = 0; // ENC28J60 doesn't support auto-negotiation
phy_config.reset_gpio_num = -1; // ENC28J60 doesn't have a pin to reset internal PHY
esp_eth_phy_t *phy = esp_eth_phy_new_enc28j60(&phy_config);

esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
esp_eth_handle_t eth_handle = NULL;
ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));

/* ENC28J60 doesn't burn any factory MAC address, we need to set it manually.
02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
*/
mac->set_addr(mac, (uint8_t[]){
0x02, 0x00, 0x00, 0x12, 0x34, 0x56});

// ENC28J60 Errata #1 check
if (emac_enc28j60_get_chip_info(mac) < ENC28J60_REV_B5)
{
ESP_LOGE(TAG, "SPI frequency must be at least 8 MHz for chip revision less than 5");
ESP_ERROR_CHECK(ESP_FAIL);
}

/* attach Ethernet driver to TCP/IP stack */
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
// Register user defined event handers
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
/* start Ethernet driver state machine */
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
}

mb_register_area_descriptor_t reg_area;
mb_param_info_t reg_info;
void setup_input_register(uint16_t offset, void *values, int size)
{
reg_area.type = MB_PARAM_INPUT;
reg_area.start_offset = offset;
reg_area.address = values;
reg_area.size = size;
mbc_slave_set_descriptor(reg_area);
ESP_LOGW(TAG, "Input reg. set to %#06x", offset);
}
void modbus_tcp(void)
{
mb_communication_info_t comm_info = {0};

comm_info.ip_addr_type = MB_IPV4;
comm_info.ip_mode = MB_MODE_TCP;

comm_info.ip_port = 502;

void *slave_handler = NULL;

// Initialization of Modbus controller
esp_err_t err = mbc_slave_init_tcp(&slave_handler);
MB_RETURN_ON_FALSE((err == ESP_OK && slave_handler != NULL), ESP_ERR_INVALID_STATE,
TAG,
"mb controller initialization fail.");

comm_info.ip_addr = "192.168.1.34"; // Bind to any address
comm_info.ip_netif_ptr = eth_netif;

// Setup communication parameters and start stack
err = mbc_slave_setup(&comm_info);
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE,
TAG,
"mbc_slave_setup fail, returns(0x%x).",
(uint32_t)err);
uint16_t test = 5;
setup_input_register(0x0, &test, 1);

mbc_slave_start();
}
#define MB_PAR_INFO_GET_TIME_OUT 20
#define MB_READ_MASK (MB_EVENT_INPUT_REG_RD | MB_EVENT_HOLDING_REG_RD | MB_EVENT_DISCRETE_RD | MB_EVENT_COILS_RD)
#define MB_WRITE_MASK (MB_EVENT_HOLDING_REG_WR | MB_EVENT_COILS_WR)
#define MB_READ_WRITE_MASK (MB_READ_MASK | MB_WRITE_MASK)

void modbus_slave_task(void *pvParameter)
{
esp_err_t rc;
while (1)
{

rc = ESP_OK;
vTaskDelay(pdMS_TO_TICKS(100));
mb_event_group_t event = mbc_slave_check_event(MB_READ_WRITE_MASK);
ESP_LOGW(TAG, "0x%x", event);

// Check for read/write events of Modbus master for certain events

/* input register read */
if (event & MB_EVENT_INPUT_REG_RD)
{
rc += mbc_slave_get_param_info(&reg_info, MB_PAR_INFO_GET_TIME_OUT);
ESP_LOGI(TAG, "INPUT READ:, ADDR:%u, SIZE:%u",
(uint32_t)reg_info.address,
(uint32_t)reg_info.size);

/* discrete register read */
}
}
// Destroy of Modbus controller on alarm
ESP_LOGE(TAG, "Modbus controller destroyed.");
vTaskDelay(100);
}
void app_main(void)
{
init_eth();
vTaskDelay(pdMS_TO_TICKS(5000));
modbus_tcp();
modbus_slave_task(NULL);
}
.
.
I've looked all internet through and could not find anything helpfull. Waiting for response :roll: .

ESP_alisitsyn
Posts: 221
Joined: Fri Feb 01, 2019 4:02 pm
Contact:

Re: Modbus TCP enc28j60 communication problem

Postby ESP_alisitsyn » Mon Sep 05, 2022 9:11 am

Hi @Silvesterrr,

The question is answered here: https://github.com/espressif/esp-idf/issues/9714

Who is online

Users browsing this forum: No registered users and 4 guests