0

I am not expert in Arduino programming but i can understand lit bit, while doing review i came across two sets of programs one using serialEvent and one with RS485 library, Now my problem is with RS485 library it always gives me ? (Reverse question mark) where as SerialEvent gives me correct output. Both program works on same connection and pins, so definitely i don't think its wiring issue. I just want understand are there any disadvantage of using SerialEvent over RS485 library? I am planning to use it for around 50-60ft of distance with 6-8 arduino nano communicating each other.

enter image description here

Code with RS485, it works but gives reverse ? mark in serial monitor print -

    #include "Arduino.h"

// Library to allow a Serial port on arbitrary pins
#include <SoftwareSerial.h>

// These are the pins we'll be talking to the RS485 device on
#define RS485rx     0 // RS485 Receive pin
#define RS485Tx     1 // RS485 Transmit pin
#define RS485inout  2 // RS485 Transmit or Receive status

#define RS485Transmit   HIGH
#define RS485Receive    LOW
#define ledPin          13
#define baudRate        115200

// Define the RS485 object
SoftwareSerial RS485(RS485rx, RS485Tx);

// The data bytes we're sending or receiving
byte rxValue;
byte txValue;

// -----------------------------------------------------------------
// SETUP         SETUP         SETUP         SETUP         SETUP
// -----------------------------------------------------------------
void setup()
{
  // Debugging window
  Serial.begin(9600);

  // Set modes for pins
  pinMode(ledPin, OUTPUT);
  pinMode(RS485inout, OUTPUT);

  // Set RS485 device to read initially
  digitalWrite(RS485inout, RS485Receive);

  // Set the baud rate. The longer the wire the slower you should
  // set the transmission rate. Anything here:
  // 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250,
  // 38400, 57600, and 115200
  // MUST BE THE SAME AS THE SENDER UNIT!
  RS485.begin(baudRate);
}

// -----------------------------------------------------------------
// LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP
// -----------------------------------------------------------------
void loop()
{
  // Is there something on the serial pin?

  if (RS485.available())
  {
    delay(1000);
    Serial.print("I am Receiver and current state of RS485 availability is:" +RS485.available());
    txValue = RS485.read();
    delay(2000);
    Serial.println("Received:");
    Serial.println(txValue);
    delay(1000);
    // Turn on the on-board LED pin to show activity
    digitalWrite(ledPin, HIGH);

    // Set the direction pin to xmit (HIGH)
    digitalWrite(RS485inout, RS485Transmit);

    // This delay is REQUIRED for things to settle before transmission


    // Send back a modified value
    RS485.write(txValue);

    // Wait for the transmission to complete.
    // This delay is dependent on the baud rate. To calculate:
    // delay = (baud rate / 10) * 2
    int bufferDelay = (baudRate / 10) * 2 + 100;
    delayMicroseconds(bufferDelay);

    // Set the RS485 back to receive
    digitalWrite(RS485inout, RS485Receive);

    // Turn off built in LED pin
    digitalWrite(ledPin, LOW);
  } else {

    Serial.println("Nothing Received");
    delay(1000);
//    Serial.print("I am Receiver and current state of RS485 not availabile :" +RS485.available());
  }
}

Code with Serial Event -

#include <SoftwareSerial.h>

#define RS485rx    0 // RS485 Receive pin
#define RS485Tx   1 // RS485 Transmit pin


int enablePin = 2; // ebanble pin for slave
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
long BAUD = 19200;

void setup() {
  // initialize serial:
  Serial.begin(BAUD);
  inputString.reserve(200);
  // setup enable pin for slave (always low)
  pinMode(enablePin, OUTPUT);
  delay(10);
  digitalWrite(enablePin, LOW);
  // set digital pin 8 - 11 for output
  for (int i = 8; i < 12; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    // make sure this message for slave "A"
    if (inputString.charAt(0) == 'A') {  
      for (int i = 1; i < 5 ; i++) {
        if (inputString.charAt(i) == '1')
          digitalWrite(7 + i, HIGH);
        else
          digitalWrite(7 + i, LOW);
      }

      // response back to server
      digitalWrite(enablePin, HIGH);
      Serial.print("-");            // response header
      Serial.print(inputString);
      Serial.flush();
      digitalWrite(enablePin, LOW);
    }     

    // clear the string:
    inputString = "";
    stringComplete = false;

  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    if (inChar == '*')  // check start char
      inputString = "";
    else
      inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      Serial.println(inputString);
      stringComplete = true;
    } 
  }
}
  • why do you use SoftwareSerial on hardware Serial pins? RX, TX pins of Nano are pins of Serial – Juraj May 11 at 16:42
  • 1
    SerialEvent is nothing more then if (Serial.available()) { serialEvent() }; called after every run of loop() function – Juraj May 11 at 16:44
  • The UART interface of an arduino is not the same as RS485 – chrisl May 11 at 16:46
  • did you wire a RS485 adapter to Nano? – Juraj May 11 at 16:48
  • @Juraj i am not quite sure, i just used online program which had that. If possible could you please explain me. I understood from code that SerialEvent is called after every loop. My point is, can i use that with RS485 for distance of 50ft. Because the other program i saw seems to be using RS485 library and i am not sure why would they build it if SerialEvent can handle that.. So just trying to understand so i can get more clear picture. I did read document but didn't understood it that clearly.. And about your last question..yes i did wire RS485 adapter to Nano. – Akshay May 11 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.