Spi and ethernet shield connect modbus tcp/ip

i try to connect modbus tcp and using two arduino with spi communication.i send data master to slave with spi that' s ok then i try connect modbus over slave its gives read or write error.but if not connected mosi pin to slave then its working but mosi pin connect gives me error i dont know how can i do this ?? please help me !! slave is arduino mega master is uno. slave is has ethernet shield to connect modbus.

the Arduino can't be a SPI slave for other Arduino if it is SPI master for the Ethernet shield on the same bus

I already told you in the other thread you opened about the same issue that you should eliminate the double Arduino setup and simply use the Mega for the complete medium converter. Connect the Ethernet Shield to the Mega, connect the RS-485 adapter to Serial1 of the Mega and use one library for ModBus RTU and one library for ModBus TCP to forward the messages. You're loosing the addressability but that's because of the ModBus concept and not your software or hardware.

1 Like

i already did this one arduino tcp and rtu for your help . thanks again. but i am trying two arduinos and spi communication together and connect ethernet. so you guys tell me there is no way to connect ethernet shield to modbus with spi devices??

farazel:
. so you guys tell me there is no way to connect ethernet shield to modbus with spi devices??

what? "connect ethernet shield to modbus with spi devices". this doesn't make sense.
you do not connect the shield to modbus. you communicate over the shield with modbus server. you can have as many devices as the SPI bus allows. but they must be all slaves to arduino.

I wrote that the arduino can't be slave and master on the same bus interface, which is understandable or not?

because for the Ethernet shield it is a master and you want to make it a slave to other Arduino. but it could be a master for the other arduino.

ok i ask that way.i am reading a sensor rtu one arduino as master.and i am sending to other arduino with spi that i am reading data from sensor. finally i am trying to modbus communication over ethernet shield to that spi receiver to data and write any register. i already do all but not communicated modbus over tcp. last part i cant do it. receiver or slave to spi arduino you cant say its not slave but that second arduino has ethernet shield communicate with modbus but i cant do this. how can i do ?? thanks for reply. maybe i dont good explain myself but its the issue. thanks again.

master or sender arduino uno

#include <SimpleModbusMaster.h>
#include <SPI.h>
#include "SPI_anything.h"
/*
The example will use packet1 to read a register from address 0 (the adc ch0 value)
from the arduino slave (id=1). It will then use this value to adjust the brightness
of an led on pin 9 using PWM.
It will then use packet2 to write a register (its own adc ch0 value) to address 1
on the arduino slave (id=1) adjusting the brightness of an led on pin 9 using PWM.
*/

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2

int c;

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 20

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,

TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
typedef struct myStruct
{

int b;

};

myStruct foo;

void setup()
{
// Initialize each packet
modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 0, 20, 0);
Serial.begin(9600);

// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
SPI.begin ();
digitalWrite(MOSI,OUTPUT);
pinMode(10, OUTPUT);

// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);

}

void loop()
{
modbus_update();

Serial.println(regs[12]);

delay(100);
digitalWrite(SS, LOW);
SPI_writeAnything (regs[12]);
digitalWrite(SS, HIGH);
delay(200);

}

and the slave or receiver

// slave

#include <SPI.h>
#include "SPI_anything.h"
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;

int c;
// create a structure to store the different data values:
typedef struct myStruct
{
//byte a;
int b;
//ong c;
};

volatile myStruct foo;
volatile bool haveData = false;

void setup ()
{

Serial.begin (9600); // debugging

// have to send on master in, slave out
pinMode(MISO, OUTPUT);
pinMode(MOSI,INPUT);
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
pinMode(53,OUTPUT);
digitalWrite(53,HIGH);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);
pinMode(SCK ,OUTPUT);
digitalWrite(SCK,HIGH);

// do radio setup
// disable w5100 SPI

// turn on SPI in slave mode
SPCR |= _BV(SPE);

// now turn on interrupts
SPI.attachInterrupt();
uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5 };
uint8_t ip[] = { 192, 168, 1, 10 };
//uint8_t gateway[] = { 192, 168, 1, 1 };
//uint8_t subnet[] = { 255, 255, 255, 0 };
Ethernet.begin(mac, ip);
} // end of setup

void loop ()
{
Mb.Run();
Mb.R[0]=1;
if (haveData)
{
//Serial.println ((int) foo.a);
Serial.println (c);
//Serial.println (foo.c);
Serial.println ();
haveData = false;
}

} // end of loop

// SPI interrupt routine
ISR (SPI_STC_vect)
{
SPI_readAnything_ISR (c);
haveData = true;

} // end of interrupt routine SPI_STC_vect

where is the wrong ???

You either didn't read Juraj's answer #1 or you didn't understand it. If it's the later you should tell us.
An Arduino cannot be SPI slave and control an Ethernet shield at the same time. So if you ultimately want or must do that split-up setup (which is quite stupid in my opinion) you have to define the Arduino with the Ethernet Shield as the master and the other Arduino as the (SPI) slave.

I have great doubts that you will get such a setup working reliably. You still didn't tell us why you insist in doing it the overly complex way. A short explanation would be appropriate, I think.

thanks for reply. i understand you said. normally i did one arduino read rtu and tcp. but it's max485 is half duplex i need full duplex system. it is that i am trying that two arduino system. but i'll try you said.so thank you again.

i did all . it's worked. the arduino with ethernet shield is master and the other one is slave and i did. thank you guys you are the best :smiley:

Congratulations that it's working.

but it's max485 is half duplex i need full duplex system. it is that i am trying that two arduino system.

That's completely unrelated. You can have a full duplex system on one Arduino, it's even easier to build and program.

yes you are right. thanks to you now i have 2 systems that is the good part :smiley:

hi guys
can i ask one question?? i want to implant wemos d1 mini to my spi connection system. how can i do ?? mega is master with ethernet and uno rtu slave and wemos d1 is another slave how can i do this ?? is it possible ?? thanks all.

you have network connection with Ethernet. what would the Wemos do?

for wireless network connection. ethernet and wireless same time

you connected the wemos as spi slave? you can use this firmware and networking library

thank you.yes i think it's slave cause master is the mega with ethernet shield.i hope i can do it and i'll try that library. let's see what happens. one question. can i connect tcp/ip to modbus with wemos as spi slave right? thanks again

farazel:
can i connect tcp/ip to modbus with wemos as spi slave right? thanks again

yes. flash the firmware, try the library examples and than we can talk about modbus tcp

ok thank you man you are the best :smiley:

hii
when i tried the examples i get this error error; "espcomm_upload_mem failed" ?? i did everything you said .