RS485 Master Example

From Public Wiki
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);
   }  
}