Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

added RS232 example #68

Merged
merged 3 commits into from
Sep 8, 2021
Merged
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
88 changes: 88 additions & 0 deletions examples/RS232/RS232.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
RS232 communication

This sketch shows how to use the SP335ECR1 on the Machine
Control as a RS232 interface, how to periodically send
a string on the RS232 TX channel and how to receive data
from the interface RX channel.

Circuit:
- Arduino Portenta Machine Control (PMC)
- Device with RS232 interface
- Connect PMC TXN to RS232 Device RXD
- Connect PMC RXP to RS232 Device TXD
- Connect PMC GND to RS232 Device GND

*/

#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 RS232 initialization");

// Set the PMC Communication Protocols to default config
comm_protocols.init();

// RS485/RS232 default config is:
// - RS485/RS232 system disabled
// - RS485 mode
// - Half Duplex
// - No A/B and Y/Z 120 Ohm termination enabled

// Enable the RS485/RS232 system
comm_protocols.rs485Enable(true);
// Enable the RS232 mode
comm_protocols.rs485ModeRS232(true);

// Specify baudrate for RS232 communication
comm_protocols.rs485.begin(115200);
// 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) {
String log = "[";
log += sendNow;
log += "] ";

String msg = "hello ";
msg += counter++;

log += msg;
Serial.println(log);

// Disable receive mode before transmission
comm_protocols.rs485.noReceive();

comm_protocols.rs485.beginTransmission();
comm_protocols.rs485.println(msg);
comm_protocols.rs485.endTransmission();

// Re-enable receive mode after transmission
comm_protocols.rs485.receive();

sendNow = millis() + sendInterval;
}
}