Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
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;
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ sentence=Arduino Library for Arduino Machine Control
paragraph=
category=Communication
url=https://github.com/arduino-libraries/MachineControl
architectures=mbed
architectures=mbed, mbed_portenta
includes=MachineControl.h
4 changes: 3 additions & 1 deletion src/utility/RS485/RS485.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void RS485Class::begin(unsigned long baudrate, uint16_t config)

_transmisionBegun = false;

half_duplex.output();

_serial->begin(baudrate, config);
}

Expand Down Expand Up @@ -123,7 +125,7 @@ void RS485Class::endTransmission()
_serial->flush();

if (_dePin != NC) {
delayMicroseconds(50);
delayMicroseconds(500);
digitalWrite(_dePin, LOW);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utility/RS485/RS485.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RS485Class : public HardwareSerial {

void setPins(PinName txPin, PinName dePin, PinName rePin);

mbed::DigitalOut half_duplex = mbed::DigitalOut(PA_9);
mbed::DigitalInOut half_duplex = mbed::DigitalInOut(PA_9);
mbed::DigitalOut sel_485 = mbed::DigitalOut(PA_10);
mbed::DigitalOut slew_n = mbed::DigitalOut(PG_14);
mbed::DigitalOut fd_tx_term = mbed::DigitalOut(PI_15);
Expand Down