Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active October 26, 2021 14:17
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 jenschr/9b11bcd96b57ad861dfc5967b4bcd4fd to your computer and use it in GitHub Desktop.
Save jenschr/9b11bcd96b57ad861dfc5967b4bcd4fd to your computer and use it in GitHub Desktop.
RS485 simple
#include <SoftwareSerial.h>
const byte receivePin = 8; // green cable (RO)
const byte transmitPin = 9; // orange cable (DI)
const byte receiveEnablePin = 2; // blue cable (RE)
const byte RECEIVER_ENABLED = 0;
const byte RECEIVER_DISABLED = 1;
const byte LED_PIN = 13;
SoftwareSerial rs485(receivePin, transmitPin); // receive pin, transmit pin
void fWrite( const byte * what, byte len )
{
int i;
for(i=0;i<len;i++)
{
rs485.write(what[i]);
}
}
void fWrite( const byte what )
{
rs485.write(what);
}
int fAvailable()
{
return rs485.available();
}
int fRead()
{
return rs485.read();
}
void setup() {
Serial.begin( 9600 );
rs485.begin( 9600 );
pinMode( LED_PIN, OUTPUT ); // built-in LED
pinMode( transmitPin, OUTPUT );
pinMode( receivePin, INPUT );
pinMode( receiveEnablePin, OUTPUT);
readMode();
}
void loop() {
while( fAvailable() ){
char c = fRead();
Serial.print( c );
}
if( Serial.available() ){
write();
}
}
void write()
{
writeMode();
//delay(1);
while( Serial.available() ){
char ch = Serial.read();
Serial.print(ch);
fWrite(ch);
}
//delay(1);
readMode();
}
void readMode()
{
digitalWrite(receiveEnablePin, RECEIVER_ENABLED); // RE = 0
}
void writeMode()
{
digitalWrite(receiveEnablePin, RECEIVER_DISABLED); // RE = 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment