Last active
October 26, 2021 14:17
-
-
Save jenschr/9b11bcd96b57ad861dfc5967b4bcd4fd to your computer and use it in GitHub Desktop.
RS485 simple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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