Skip to content

Instantly share code, notes, and snippets.

@funvill
Last active April 19, 2018 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funvill/8eb78a0d3c06c76f2867a37f8ff8e3ad to your computer and use it in GitHub Desktop.
Save funvill/8eb78a0d3c06c76f2867a37f8ff8e3ad to your computer and use it in GitHub Desktop.
#include "HardwareSerial.h"
HardwareSerial SerialTwo(2);
const char PIN_RS485 = 14;
const unsigned short lineDriveOff = 10;
const unsigned short lineDriveOn = 3;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
SerialTwo.begin(115200);
pinMode(PIN_RS485, OUTPUT);
SetRX();
}
void SetTX() {
digitalWrite(PIN_RS485, HIGH);
delay(lineDriveOn);
}
void SetRX() {
delay(lineDriveOff);
digitalWrite(PIN_RS485, LOW);
delay(lineDriveOn);
}
void loop() {
static unsigned int serialOneRX = 0 ;
static unsigned int serialOneTX = 0 ;
static unsigned int serialTwoRX = 0 ;
static unsigned int serialTwoTX = 0 ;
// Send anything from RS485 port to USB serial
while (SerialTwo.available()) {
char rc = SerialTwo.read();
Serial.print(rc);
serialTwoRX++;
serialOneTX++; }
// Send anything from USB port to RS485 serial
while (Serial.available()) {
char rc = Serial.read();
SetTX();
SerialTwo.print(rc);
serialOneRX++;
serialTwoTX++;
}
SetRX();
static unsigned long lastUpdate = 0 ;
if( lastUpdate < millis() ) {
lastUpdate = millis() + 5000 ;
SetTX();
Serial.print( "\r\n" + String(millis()) + " FYI: Serial One TX: " + String(serialOneTX) + ", RX: "+ String(serialOneRX) + " Serial Two TX: " + String(serialTwoTX) + ", RX: "+ String(serialTwoRX) );
SerialTwo.print( "\r\n" + String(millis()) + " FYI: Serial One TX: " + String(serialOneTX) + ", RX: "+ String(serialOneRX) + " Serial Two TX: " + String(serialTwoTX) + ", RX: "+ String(serialTwoRX) );
SetRX();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment