Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active Oct 26, 2021
Embed
What would you like to do?
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