Go Down

Topic: [MERGED] Master/Slave communication (Read 2183 times) previous topic - next topic

GoForSmoke

I need to send every time 1 txt file of max size 30 KB. How can it be done with command Wire.write () or Wire.send() ?
Can anyone help me with code?
What Wire.send() command? Is this some other wire library? I see a write() command.....

https://www.arduino.cc/en/Reference/Wire

The write() command sends bytes where (byte *) is a pointer to byte variable, not a bye value.

The IDE Examples in the Wire Library section has working examples of basic Wire operations.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

GoForSmoke

I am fully aware about this! This is a demonstrative setup; a real field application will spend minimum amount of time in the ISR; there will be no question of re-enabling the interrupt structure for calling the 1-sec time delay SUR.
It demonstratively demonstrates multiple idiocies to try and fob a "crafty" trick.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

GolamMostafa

Quote
It demonstratively demonstrates multiple idiocies to try and fob a "crafty" trick.
That's why the French people say that the rules are for monkeys.  These are the monkeys that are going to design/build commercial circuits based on the demonstrative examples?

GoForSmoke

That's why the French people say that the rules are for monkeys. 
And "the French people" who say this apply that to commercial circuit design?
 
Or are they saying that about something else?

Hey, France is the home of postmodernists, the biggest intellectual FRAUDS in the world.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

pipertzisdimitris

My question was how can i send a txt file from master to a slave with specific ID location of a slave Arduiono. I want to send a txt file from my pc, using COM port, to master Arduino and then the master to send it in the right ID.My sample code is
Code: [Select]

#include <Wire.h>


byte data ;

int slaveAdd ;

  void setup()
 {
   
Serial.begin(9600);
Wire.begin();
delay(3000);
}
void loop()
{



if (slaveAdd == 0x01)
{
  Wire.beginTransmission(0x01);
  Wire.write(data);
  Wire.endTransmission();
}




I am a new programmer and i would appreciate which corrections.

TolpuddleSartre

#20
May 16, 2018, 09:17 am Last Edit: May 16, 2018, 09:17 am by TolpuddleSartre
(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.

UKHeliBob

Quote
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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

pipertzisdimitris

#22
May 16, 2018, 09:27 am Last Edit: May 16, 2018, 10:11 am by pipertzisdimitris
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?

MorganS

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?
"The problem is in the code you didn't post."

pipertzisdimitris

Example :

Code: [Select]
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.

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.
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?

MorganS

"The problem is in the code you didn't post."

pipertzisdimitris

 My master code:

Code: [Select]

#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:


Code: [Select]

#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

PaulMurrayCbr

#27
May 17, 2018, 11:24 am Last Edit: May 17, 2018, 11:28 am by PaulMurrayCbr
A slight refactoring of the slave, with interrupt handling via a volatile mutexe. Also changed x1/x2 to cmd/data.

Code: [Select]

#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.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

MorganS

Quote
My master code:
Code: [Select]
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.

Quote
And my slave code:
3. Why do you attach the receive event handler 6 times in setup? That only needs to be done once.

4. 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.
"The problem is in the code you didn't post."

pipertzisdimitris

my slave code


Code: [Select]


#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

Code: [Select]
#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?

Go Up