Why Serial.write() instead of Serial.print()

Yep - exactly

GolamMostafa:
Practice the following sketches to see the mechanism of extracting decimal number at the receiver side after collecting the data bytes transferred by the sender using .write() methods.

UNO-1 (Sender Codes)

#include<SoftwareSerial.h>

SoftwareSerial SUART(2, 3); //SRX/STX pin of UNO

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop()
{
  SUART.write('<'); //Start Mark of Message
  SUART.write(0x31);    //sends 1
  SUART.write(0x32);    //sends 2
  SUART.write(0x33);    //sends 3
  SUART.write(0x34);    //send 4
  SUART.write('>'); //End Mark of Message
  delay(1000);

}




**UNO-2** (Receiver Codes)


#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX/STX pin of NANO
byte myData[10];
bool flag1 = false;
int i = 0;
int x1, x2, x;

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop()
{
  byte n = SUART.available(); //checking if a data byte has arrived
  {
    if (n != 0)
    {
      if (flag1 == false)  //Start Mark has not arrived
      {
        byte x = SUART.read();
        if (x == '<')
        {
          flag1 = true;  //Start Mark has arrived
        }
      }
      else    //Start Mark has already arrived
      {
        byte y = SUART.read();
      // Serial.println(y, HEX);
        if (y != '>')
        {
          myData[i] = y;
          i++;
        }
        else
        {
          x = extractDecimal();    //extract decimal number for myData array
          Serial.println(x, HEX);    //shows received decimal number: 1234
          flag1 = false;
          i = 0; //reset array
        }
      }
    }
  }
}

int extractDecimal()
{
  x1 = (int)((myData[0] - 0x30)<< 4)| (int)(myData[1] - 0x30); //12
  x2 = (int)((myData[2] - 0x30) << 4) | (int)(myData[3] - 0x30);  //34
  x = (x1 << 8) | x2;
  return x;
}




**BTW:** Give a try to re-write the receiver codes using the following method:


SUART.readBytesUntil('>', myArray, 10); //what would be the type of myArray -- byte or char?

Hey I have tried it and it didn't work however I have some questions. The SUART(2,3) means I have to connect the TX from the BT module to digital 2 and RX to digital 3 from the arduino uno? Also, should I be able to read the information from one Serial monitor to another? Eg: I send info from Arduino 1 (COM8) by writing on the Serial monitor and read it on Arduino 2 (COM9) in the serial monitor?

are you sure your BT module supports 5V for Rx ?

TEAC:
Hey I have tried it and it didn't work however I have some questions. The SUART(2,3) means I have to connect the TX from the BT module to digital 2 and RX to digital 3 from the arduino uno? Also, should I be able to read the information from one Serial monitor to another? Eg: I send info from Arduino 1 (COM8) by writing on the Serial monitor and read it on Arduino 2 (COM9) in the serial monitor?

The posted sketches have been tested on two UNOs being connected solidly via SUART (Software UART) Ports where the connections are to be made as follows:
DPin-3 of UNO-1 (STX) ----> DPin-2 of UNO-2 (SRX)
DPin-2 of UNO-1 (STX) <---- DPin-3 of UNO-2 (STX)
GND of UNO-1 <------------> GND of UNO-2

If you are using BT, the connections would be as follows:
hc5-1.png

hc5-1.png