RS485 Master Example

From Public Wiki
Revision as of 17:44, 16 November 2020 by Falgsc-al (talk | contribs) (Created page with "<syntaxhighlight lang="C++"> //#include <SoftwareSerial.h> //const int SSERIAL_RX_PIN = 10; //Soft Serial Receive pin //const int SSERIAL_TX_PIN = 11; //Soft Serial Transmit...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
//#include <SoftwareSerial.h>
//const int SSERIAL_RX_PIN = 10;  //Soft Serial Receive pin
//const int SSERIAL_TX_PIN = 11;  //Soft Serial Transmit pin

// Create Soft Serial Port object and define pins to use
//SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN); // RX, TX

int byteReceived;
//===============================================================================
//  Initialization
//===============================================================================
void setup()
{
  Serial.begin(115200);           // Start the built-in serial port
  Serial.println("Master Device");
  Serial.println("Type in upper window, press ENTER");
   
  Serial1.begin(115200);   // Start the RS485 soft serial port 
}
//===============================================================================
//  Main
//===============================================================================
void loop() 
{
  if (Serial.available())         // A char(byte) has been entered in the Serial Monitor
  {
    byteReceived = Serial.read();                   // Read the byte
    Serial1.write(byteReceived);                 // Send byte to Remote Arduino 
  }
  
  if (Serial1.available())            //Data from the Slave is available
   {
    byteReceived = Serial1.read();    // Read received byte
 //coomented for diagnostics
 //Serial.write(byteReceived);           // Show on Serial Monitor
 Serial.print(micros());
 Serial.print(' ');
 Serial.println(byteReceived,HEX);
   }  
}