need help for serial communication

hi, i am trying to serially communicate between two arduino ex- one should send character(hello) and receive (cool) and other should send (cool) and receive(hello) and cont..

in serial monitor i am expecting the output as

arduino 1(hello cool) & arduino 2(cool hello)

i am getting helloc helloo helloo hellol as my output can some one help me to program

thankyou,

Post your code.

The serial input basics tutorial might be of interest.

sterretje: Post your code.

//master

char tx[] = "Hello"; //String data char rx[10]; //Initialized variable to store recieved data

void setup() { // Begin the Serial at 9600 Baud Serial.begin(9600); }

void loop() { if (Serial.available()) { Serial.println(Serial.write(tx)); //Write the serial data delay(100); } { Serial.write(Serial.read()); //Read the serial data and store in var delay(100); } }

//slave

char tx[] = "cool1"; //String data char rx[10]; //Initialized variable to store recieved data

void setup() { // Begin the Serial at 9600 Baud Serial.begin(9600); }

void loop() { if (Serial.available()) { Serial.println(Serial.write(Serial.read())); //Read the serial data and store in var delay(100); } { Serial.write(tx); //Write the serial data delay(100); } }

groundFungus: The serial input basics tutorial might be of interest.

hey thank you for your response since i am new to arduino , can u say some more clearly about serial transmit and receive with example so i could easily understand ....

divyakumar: since i am new to arduino , can u say some more clearly about serial transmit and receive with example so i could easily understand ....

I doubt if you will find it explained and demonstrated more simply and clearly than in that link.

When I needed to find out about comms, I worked through the examples in that tutorial very carefully, pretty much line by line. And today I still open them up for help when I can't remember how it all works.

Have you actually worked through those examples? (And I mean worked through not just skimmed over?)

In the original post, you have said that you want to exchange data between two Arduinos (say, UNO) using Serial Communication Link; but, you have not said which one of the following three serial communication links:

(a) UART Port based Serial Communication (b) I2C Bus Based Serial Communication (c) SPI Port Based Serial Communication

From your Post#3, we have been able to understand that you are using UART Port based Serial Communication Link for data exchange between two UNOs.

As a reference, I am posting below a diagram (though not much related with your post) which may be helpful for you to know about all possible ways for the UNO/MCU to exchange data with external IO devices. This diagram will also help you to understand that there is a difficulty in using the UART Port (called hardware UART Port) to communicate with the UNO-2; instead we have to use another kind of artificial UART Port called Software Serial Port (SUART). |500x442 Figure-1: Block Diagram containing the hardware resources of ATmega328P microcontroller of UNO

In Fig-1, UART Port is permanently connected with the PC/Serial Monitor via USB/Virtual COM Port. Let us first see, how serial data communication takes place between PC and UNO-1.

1. During transmission from UNO to PC, one character is transferred at one time. If we want to transfer the message, Hello, the UNO will take 5 rounds to transfer this message as there are 5 characters in the message.

2. Similarly, during reception of message from PC/Serial Monitor, one character is received at a time.

Now, write a UART related small program and execute it; open the Serial Monitor and practice various UART related commands -- Serial.read();, Serial.write();, Serial.available(),;, Serial.println();, and etc. //---------------------------------------------------------------------------------------------------------------

BTW: Kindly reformat the program codes of your Post#3 in accordance with the following instructions: (a) Open your post in the Edit/Modify Mode.

(b) Bring the cursor at the very beginning of your code. Now, type this word: [ c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.

(c) Bring the cursor at the very end of your code. Now, type this word: [ / c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.

(d) Press the save button.

Now, your codes/post looks so beautiful!

juma_yetu:
I doubt if you will find it explained and demonstrated more simply and clearly than in that link.

When I needed to find out about comms, I worked through the examples in that tutorial very carefully, pretty much line by line. And today I still open them up for help when I can’t remember how it all works.

Have you actually worked through those examples? (And I mean worked through not just skimmed over?)

thank you for your suggestion i ll do as suggested…

   Serial.println(Serial.write(tx)); //Write the serial data

What the heck do you think this is doing?

PaulS:   Serial.println(Serial.write(tx)); //Write the serial data

What the heck do you think this is doing?

serially print on serial monitor as well as send "Hello" to second arduino

divyakumar:
serially print on serial monitor as well as send “Hello” to second arduino

try like this:

constexpr size_t MAX_MESSAGE_LENGTH = 16;

template <class T> inline Print& operator<<(Print& obj, T arg) {
  obj.print(arg);
  return obj;
}

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  Serial << (F("let's go!\n"));
}

void loop() {
  if (const char* newMessage = checkForNewMessage(Serial, '\n')) {
    Serial << (F("Just Recieved:\t")) << newMessage << (F("\n"));
    if (strstr(newMessage, "hello")) {
      Serial << (F("cool\n"));
    } else {
      Serial << (F("what?\n"));
    }
  }
}

const char* checkForNewMessage(Stream& stream, const char endMarker) {
  static char incomingMessage[MAX_MESSAGE_LENGTH] = "";
  static byte idx = 0;
  if (stream.available()) {
    incomingMessage[idx] = stream.read();
    if (incomingMessage[idx] == endMarker) {
      incomingMessage[idx] = '\0';
      idx = 0;
      return incomingMessage;
    } else {
      idx++;
      if (idx > MAX_MESSAGE_LENGTH - 1) {
        //stream.print(F(" {\"error\":\"message too long\"}\n")); //you can send an error to sender here
        idx = 0;
        incomingMessage[idx] = '\0';
      }
    }
  }
  return nullptr;
}

serially print on serial monitor as well as send "Hello" to second arduino

Serially print what? What does Serial.write() return that you are printing? It does NOT return the string it wrote.

Why is printing what Serial.write() return of ANY benefit?

Why are you using ONE serial port to talk to TWO devices?