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 https://create.arduino.cc/projecthub/maurizfa-13216008-arthur-jogy-13216037-agha-maretha-13216095/modbus-rs-485-using-arduino-c055b5). 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?