Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update RS485 example
  • Loading branch information
manchoz committed May 4, 2021
commit cbd56924591bab1d4352e1817c4adeed5cea501f
78 changes: 42 additions & 36 deletions examples/RS485_fullduplex/RS485_fullduplex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,49 @@ using namespace machinecontrol;

unsigned long counter = 0;

void setup() {

Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.println("Start RS485 initialization");

// Initialize the serial interface, enable the
// RS485 on SP335ECR1 and set as full duplex
comm_protocols.rs485.begin(9600);
comm_protocols.rs485.enable = 1;
comm_protocols.rs485.sel_485 = 1;
comm_protocols.rs485.half_duplex = 0;

Serial.println("Initialization done!");
void setup()
{

Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}
delay(1000);
Serial.println("Start RS485 initialization");

comm_protocols.rs485.begin(115200);
comm_protocols.rs485.enable = 1; // SDHN_N
comm_protocols.rs485.sel_485 = 1; // RS485_RS232_N
comm_protocols.rs485.half_duplex = 0; // HALF_FULL_N
comm_protocols.rs485.receive(); // RE_N
comm_protocols.rs485.fd_tx_term = 1; // FD_TX_TERM - 120 Ohm Y-Z termination enabled when both TERM and FD_TX_TERM are high
comm_protocols.rs485.term = 1; // TERM - 120 Ohm A-B termination enabled when high

Serial.println("Initialization done!");
}

constexpr unsigned long sendInterval { 1000 };
unsigned long sendNow { 0 };

constexpr unsigned long halfFullInterval { 5000 };
unsigned long halfFull { 0 };
byte halfFullStatus { 0 };

void loop()
{
while (comm_protocols.rs485.available())
Serial.write(comm_protocols.rs485.read());

if (millis() > sendNow) {
comm_protocols.rs485.noReceive();
comm_protocols.rs485.beginTransmission();

comm_protocols.rs485.print("hello ");
comm_protocols.rs485.println(counter++);

comm_protocols.rs485.endTransmission();
comm_protocols.rs485.receive();

void loop() {
// Call receive(); sets the flux control pins properly
// and allows receiving data if available
comm_protocols.rs485.receive();
if (comm_protocols.rs485.available()) {
Serial.print("read byte: ");
Serial.write(comm_protocols.rs485.read());
Serial.println();
}
// Call beginTransmission(); sets the flux control pins properly
// and allows starting a trasnsmission.
// If instead of a string, you want
// to send bytes, use the API write();
comm_protocols.rs485.beginTransmission();
comm_protocols.rs485.print("hello ");
comm_protocols.rs485.println(counter);
comm_protocols.rs485.endTransmission();
counter++;

delay(1000);
sendNow = millis() + sendInterval;
}
}