#include <ModbusMaster.h>
#include <SoftwareSerial.h>

// Define software serial pins (use any digital pins if not using hardware serial)
#define RX_PIN 13
#define TX_PIN 12
#define DE_PIN 3
#define RE_PIN 4

SoftwareSerial s1(RX_PIN, TX_PIN);

void setup() {
  // Start the software serial port
  s1.begin(9600 );
 
  //pinMode(RX_PIN, INPUT);
  //pinMode(TX_PIN, OUTPUT);
  pinMode(DE_PIN, OUTPUT);
  pinMode(RE_PIN, OUTPUT);

  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);

  //node.setTimeout(2000);
  // Optionally, start the Serial monitor for debugging
  Serial.begin(9600);

  while (!s1) {
  }
  Serial.println("Setup done A");
  

}



void loop() {
  static unsigned long lastRefreshTime = 0;
  s1.listen();
  while (s1.available() > 0) {
    int inByte = s1.read();
    Serial.write(inByte);
  }





  //delay(10); // Wait a second before the next loop

  // digitalWrite(RE_PIN, HIGH);
  // s1.print("Hello");   
  // digitalWrite(RE_PIN, LOW);

  if(millis() - lastRefreshTime >= 1000)
	{
		lastRefreshTime += 1000;
    Serial.println("Cycle\n\n");

    digitalWrite(RE_PIN, HIGH);
    digitalWrite(DE_PIN, HIGH);
    s1.print("Ping..");      
    s1.flush();
    //delay(1000);
    digitalWrite(RE_PIN, LOW);
    digitalWrite(DE_PIN, LOW);

	}
  
}
