This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from manchoz/rs485_refactor
Refactor RS485 support
- Loading branch information
Showing
7 changed files
with
143 additions
and
274 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
RS485 Half Duplex communication | ||
This sketch shows how to use the SP335ECR1 on the Machine | ||
Control as a half duplex (AB) RS485 interface, how to periodically | ||
send a string on the RS485 TX channel and how to receive data | ||
from the interface RX channel. | ||
Circuit: | ||
- Portenta H7 | ||
- Machine Control | ||
- A Slave device with RS485 interface | ||
- Connect TXP to A(+) and TXN to B(-) | ||
*/ | ||
|
||
#include "Arduino_MachineControl.h" | ||
|
||
using namespace machinecontrol; | ||
|
||
constexpr unsigned long sendInterval { 1000 }; | ||
unsigned long sendNow { 0 }; | ||
|
||
unsigned long counter { 0 }; | ||
|
||
void setup() | ||
{ | ||
|
||
Serial.begin(115200); | ||
// Wait for Serial or start after 2.5s | ||
for (auto const timeout = millis() + 2500; !Serial && timeout < millis(); delay(500)) | ||
; | ||
|
||
delay(2500); | ||
Serial.println("Start RS485 initialization"); | ||
|
||
// Set the PMC Communication Protocols to default config | ||
comm_protocols.init(); | ||
|
||
// RS485/RS232 default config is: | ||
// - RS485 mode | ||
// - Half Duplex | ||
// - No A/B and Y/Z 120 Ohm termination enabled | ||
|
||
// Enable the RS485/RS232 system | ||
comm_protocols.rs485Enable(true); | ||
|
||
// Specify baudrate, and preamble and postamble times for RS485 communication | ||
comm_protocols.rs485.begin(115200, 0, 500); | ||
// Start in receive mode | ||
comm_protocols.rs485.receive(); | ||
|
||
Serial.println("Initialization done!"); | ||
} | ||
|
||
void loop() | ||
{ | ||
if (comm_protocols.rs485.available()) | ||
Serial.write(comm_protocols.rs485.read()); | ||
|
||
if (millis() > sendNow) { | ||
// Disable receive mode before transmission | ||
comm_protocols.rs485.noReceive(); | ||
|
||
comm_protocols.rs485.beginTransmission(); | ||
|
||
comm_protocols.rs485.print("hello "); | ||
comm_protocols.rs485.println(counter++); | ||
|
||
comm_protocols.rs485.endTransmission(); | ||
|
||
// Re-enable receive mode after transmission | ||
comm_protocols.rs485.receive(); | ||
|
||
sendNow = millis() + sendInterval; | ||
} | ||
} |
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
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
Oops, something went wrong.