[MERGED] Master/Slave communication

GolamMostafa:
(Bad things are always rejected by the readers; it does not require 3rd party's advocacy.)

That's just bollocks, isn't it?

It might take years before the reader is capable of deciding that what s/he has read is a bad thing.

Look at how long it took, and how badly things went, for the readers of one of the 20th century's bestsellers, Mein Kampf.

I want to send a txt file from my pc, using COM port, to master Arduino

What program are you gong to use to send the text file from the PC ?

UKHeliBob:
What program are you gong to use to send the text file from the PC ?

That i have in mind is to send it by a hyperterminal program. My code sample is corect?

pipertzisdimitris:
That i have in mind is to send it by a hyperterminal program. My code sample is corect?

No your code is not correct but it is a good start. The variables for address and data never get filled in before sending.

Can you show an example of the text file? How does the master know which slave to send it to? Is that part of the file or is there some other method of determining the destination?

Example :

G90 
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3
G0 Y1 X1
g0 x2 y2
go x-3 y-3

Somthing like that.

pipertzisdimitris:
How does the master know which slave to send it to? Is that part of the file or is there some other method of determining the destination?

I am not sure on how can it be done and thats why i asked for your help.

MorganS:
The variables for address and data never get filled in before sending.

What do you mean by that. Can someone sent me an example of code cause my programming skills are low?

Here is a good example: Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

My master code:

#include <Wire.h>

const byte SLAVE_ADD1 = 1;
const byte SLAVE_ADD2 = 2;
const byte SLAVE_ADD3 = 3;
const byte SLAVE_ADD4 = 4;
const byte MY_ADD = 10;
int Address ;




void setup() {
Wire.begin(MY_ADD);

}

void loop() {
 for (byte x = 2; x <= 7; x++)
    {  
      if (Address == 1 )
      {
          Wire.beginTransmission (SLAVE_ADD1);
          Wire.write (x);
           Wire.endTransmission();
        }
          else if (Address == 2)
            { 
                 Wire.beginTransmission (SLAVE_ADD2);
                 Wire.write (x);
                 Wire.endTransmission();
    
              }
                else if (Address == 3)
                 {
                      Wire.beginTransmission (SLAVE_ADD3);
                      Wire.write (x);
                      Wire.endTransmission();
                      }
                        else if (Address == 4)
                            {
                                   Wire.beginTransmission (SLAVE_ADD4);
                                   Wire.write (x);
                                   Wire.endTransmission();
         
                                           }
                                           else 
                                            { 
                                              Serial.println("ERROR");
                                            }    
                                            delay (2000);
                                            return  0 ;
    }  // end of for loop
  }  // end of loop

And my slave code:

#include <grbl.h>
#include <Wire.h>
const byte MY_ADD = 1;
const byte OTHER_ADD = 10;

void setup()
{
   Wire.begin (MY_ADD);
  for (byte i = 2; i <= 7; i++)
     {
  Wire.onReceive (receiveEvent);
  }  // end of setup
 
}

void loop()
{

}
void receiveEvent (int howMany)
  {
  for (int i = 0; i < howMany; i++)
    {
    byte c = Wire.read ();
   
}  // end of receiveEvent
  }

Please check this out and help me to finish my University project because my teachers
are pressing me.If something is wrong i’ll be happy with any corrections. Best regard

A slight refactoring of the slave, with interrupt handling via a volatile mutexe. Also changed x1/x2 to cmd/data.

#include<Wire.h>

struct Transmission {
  byte cmd;
  byte data;
};

volatile boolean tx_available = false;
volatile Transmission tx_incoming;

void setup()
{
  Wire.begin(0x21);  //UNO-2's slave address
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Wire.onReceive(receiveEvent); //MCU enters into this SUR once a data byte arrives in TWI Logic
}

void loop()
{
  Transmission tx;
  tx.cmd = 0;

  // start of mutex block
  noInterrupts();
  if (tx_available) {
    memcpy((void*)&tx, (void*)&tx_incoming, sizeof(Transmission));
    tx_available = false;
  }
  interrupts();

  // start of working block
  if (tx.cmd == 0x02) {
    do
    {
      digitalWrite(13, HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      delay(1000);
      tx.data--;
    }
    while (tx.data != 0);
  }
}

void receiveEvent(int howmany)
{
  /* we could test tx_available here, but I will just
   unconditionally overwrite the previous unconsumed transmission, if any */
  tx_incoming.cmd = Wire.read();
  tx_incoming.data = Wire.read();
  tx_available = true;
}

The Transmission struct would normally be in a shared .h file, and it would be read and written to the wire as a single block.

My master code:

Wire.begin(MY_ADD);
  1. Don't give the master an address. If you pass an address to Wire.begin() it puts it in slave mode.

  2. You still never assign any value to Address. How does the master know where to send the data?

In your text file, does "G90" identify the slave number to send this to? So read the incoming data looking for the first "G" and then put the "90" into Address.

And my slave code:

  1. Why do you attach the receive event handler 6 times in setup? That only needs to be done once.

  2. The slave never does anything with the received data? How woild you know if it worked? Just get it to send it to the PC on Serial, to start.

my slave code

#include <Wire.h>
#include <I2C_Anything.h>
#define SLAVE_ADDRESS 0x1


void setup() 
{
Serial.begin(9600);
  Wire.begin (SLAVE_ADDRESS);

  Wire.onReceive (receiveEvent);
}  // end of setup

volatile boolean haveData = false;
volatile float fnum;
volatile long foo;

void loop() 
{


}  // end of loop

// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
 {
 if (howMany >= (sizeof fnum) + (sizeof foo))
   {
   I2C_readAnything (fnum);   
   I2C_readAnything (foo);   
   haveData = true;     
   }  // end if have enough data
 }  // end of receiveEvent

my master code

#include <Wire.h>
#include <I2C_Anything.h>

const byte SLAVE_ADDRESS = 0x1;
void setup() 
{
   Serial.begin(9600);
  Wire.begin ();
  
}  // end of setup

void loop() 
{

 long foo = 42;
  float bar = 123.456;
 
  Wire.beginTransmission (SLAVE_ADDRESS);
  I2C_writeAnything (foo);
  I2C_writeAnything (bar);
  Wire.endTransmission();
      

   

}  // end of loop

i am using hyperterminal to send the files and the only arduino that receives data is the master.my connection is Masters TX to Slaves TX and Masters RX to Slaves RX. Any recommendations?

Presumably the idea is for the master to receive the data from the PC and for its contents to be passed on to the appropriate slave.

Hold on. This sounds very familiar.

No wonder when I read http://forum.arduino.cc/index.php?topic=547517.0

Why have you started a new thread when you have not answered the questions posed in the old one ?

I am so sorry but a friend of mine had posted the last one and i didn't know that. We are in the same project. He had not informed me of the previous one. So i am gonna continue the fisrt one.

This i my new try of sending file from master to a slave.
Master :

#include <Wire.h>
#include <I2C_Anything.h>

const byte SLAVE_ADDRESS = 0x1;
void setup() 
{
   Serial.begin(9600);
  Wire.begin ();
  
}  // end of setup

void loop() 
{

long foo = 42;
  float bar = 123.456;
 
  Wire.beginTransmission (SLAVE_ADDRESS);
  I2C_writeAnything (foo);
  I2C_writeAnything (bar);
  Wire.endTransmission();
}  // end of loop

Slave:

#include <Wire.h>
#include <I2C_Anything.h>
#define SLAVE_ADDRESS 0x1


void setup() 
{
Serial.begin(9600);
  Wire.begin (SLAVE_ADDRESS);

 Wire.onReceive (receiveEvent);  // interrupt handler for incoming messages





}  // end of setup


void loop() 
{


}  // end of loop
volatile boolean haveData = false;
volatile long foo;
volatile float bar;

// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
 {
 if (howMany >= (sizeof foo) + (sizeof bar))
   {
   I2C_readAnything (foo);   
   I2C_readAnything (bar);   
   haveData = true;     
   }  // end if have enough data
 }  // end of receiveEvent

It looks like the Master receives the file but it can’t transmit it to the slave.

It looks like the Master receives the file

In your code for the Master I see nothing that receives a file, or anything else for that matter.

UKHeliBob:
In your code for the Master I see nothing that receives a file, or anything else for that matter.

You mean that i need to include code to the Master Arduino in case to receive files from pc?

pipertzisdimitris:
You mean that i need to include code to the Master Arduino in case to receive files from pc?

Yes

The right connection between Maste and slave is : Master Rx to Slave Tx and Master TX to Slave RX ?(in case you want to transmit data)

pipertzisdimitris:
The right connection between Maste and slave is : Master Rx to Slave Tx and Master TX to Slave RX ?(in case you want to transmit data)

But you are using I2C which does not use the Tx and Rx pins

I can't help feeling that you are a little confused.

"Using" is a strong word: the slave programs shown so far still don't do anything with the data so it's a Shroedinger's Cat type of problem.

I think the original advice (from elsewhere) was flawed: this appears to be a distributed manufacturing project and I2C won't work at all if the distance between the slaves and masters is more than a meter. Maybe it's for very small 3D printers?

So far the OP seems to have a Master Arduino with no means of receiving data from the PC, the Master Arduino has no way of sending the data to the Slave Arduinos and the Slave Arduinos have no code to do anything with the data even if they did receive it.

It's not looking goo, is it ?

@pipertzisdimitris

Please explain how the PC is connected to the Master Arduino and how the Master is in turn connected to the Slaves