Serial.available using RS485 not working inside class

Hello everyone,

I currently have a problem understanding how to work with external classes. My setup is two Arduino Nanos connected to two RS485 modules (similar to Modbus (RS-485) Using Arduino - Arduino Project Hub). The original sketches are

Master:

#include <Arduino.h>
#include<RS485.h>

const int EnableTX = 2;

void setup() {
  Serial.begin(9600);
  rs485.setup(EnableTX);
}

void loop() {
  
  digitalWrite(EnableTX,HIGH);
  Serial.println("x");
  delay(1);
  digitalWrite(EnableTX,LOW);

  delay(3500);
}

Slave:

#include <Arduino.h>

#include <RS485.h>

const int EnableTX = 2;

void setup() {
  Serial.begin(9600);
  rs485.setup(EnableTX);
}

void loop() {

  if(Serial.available()>0) {
    char x1 = Serial.read();

    digitalWrite(LED_BUILTIN, HIGH);
    delay(250);
    digitalWrite(LED_BUILTIN, LOW); 
    delay(250);  

  }

  delay(5);
}

With the helper class
RS485.h

#ifndef rs485_h
#define rs485_h

#include <Arduino.h>

class RS485 {
public:
    RS485();
    void test();

    void setup(uint8_t pin);
    void send(char* c);
    void test(char* c);

    uint8_t exPin;
};

extern RS485 rs485;

#endif

RS485.cpp

#include "RS485.h"

RS485::RS485() {
}

void RS485::setup(uint8_t pin) {
    exPin = pin;
    pinMode(exPin, OUTPUT);

    digitalWrite(exPin,HIGH);
    digitalWrite(exPin,LOW);

    Serial.flush();
}

void RS485::send(char* c) {
  digitalWrite(exPin,HIGH);
  delay(1);
  Serial.println(c);
  digitalWrite(exPin,LOW);
}

void RS485::test(char* c) {
  Serial.println(c);
}

RS485 rs485;

In this case the communication works and is recognized on the slave. Moving the 'sending' portion inside the RS485-class the sending on the RS485 busline still happens, but is not detected on the slave

Master_new:

#include <Arduino.h>
#include <RS485.h>

const int EnableTX = 2;

void setup() {
  Serial.begin(9600);
  rs485.setup(EnableTX);
}

void loop() {
  
  rs485.send("x");

  delay(3500);
}

Why does the position of the code has influence on the serial output? Does it? Is there something I am missing?

I tried different declarations for the passing character. Rather than 'directly' passing the character I defined it before and passed the variable

Working:

char* c = "X";
rs485.send(c);

Not Working:

rs485.send("X");

I only have assumptions why this way works. When declaring the variable the address can be passed to other functions. Passing it directly only one instance of the character exists.

Is this correct?

Hello

Try change send(char* c) to send(const char* c)

1 Like

It works and I may found an explanation here

void f(char*);
void f(const char*);
f("Hello"); // which f gets called?

If the above example is compiled in compatibility mode (or with the 4.2 compiler), function f(char*) is called. If compiled in standard mode, function f(const char*) is called.

In standard mode, the compiler will put literal strings in read-only memory by default. If you then attempt to modify the string (which might happen due to automatic conversion to char*) the program aborts with a memory violation.

With the following example, the 4.2 compiler and the 5.0 compiler in compatibility mode put the string literal in writable memory. The program will run, although it technically has undefined behavior. The 5.0 compiler in standard mode puts the string literal in read-only memory by default, and the program aborts with a memory fault. You should therefore heed all warnings about conversion of string literals, and try to fix your program so the conversions do not occur. Such changes will ensure your program is correct for every C++ implementation.

Thank you @guix

Good :slight_smile: