|
| 1 | +/* |
| 2 | + RS232 communication |
| 3 | +
|
| 4 | + This sketch shows how to use the SP335ECR1 on the Machine |
| 5 | + Control as a RS232 interface, how to periodically send |
| 6 | + a string on the RS232 TX channel and how to receive data |
| 7 | + from the interface RX channel. |
| 8 | +
|
| 9 | + Circuit: |
| 10 | + - Arduino Portenta Machine Control (PMC) |
| 11 | + - Device with RS232 interface |
| 12 | + - Connect PMC TXN to RS232 Device RXD |
| 13 | + - Connect PMC RXP to RS232 Device TXD |
| 14 | + - Connect PMC GND to RS232 Device GND |
| 15 | +
|
| 16 | +*/ |
| 17 | + |
| 18 | +#include <Arduino_MachineControl.h> |
| 19 | + |
| 20 | +using namespace machinecontrol; |
| 21 | + |
| 22 | +constexpr unsigned long sendInterval { 1000 }; |
| 23 | +unsigned long sendNow { 0 }; |
| 24 | + |
| 25 | +unsigned long counter { 0 }; |
| 26 | + |
| 27 | +void setup() |
| 28 | +{ |
| 29 | + |
| 30 | + Serial.begin(115200); |
| 31 | + // Wait for Serial or start after 2.5s |
| 32 | + for (auto const timeout = millis() + 2500; !Serial && timeout < millis(); delay(500)) |
| 33 | + ; |
| 34 | + |
| 35 | + delay(2500); |
| 36 | + Serial.println("Start RS232 initialization"); |
| 37 | + |
| 38 | + // Set the PMC Communication Protocols to default config |
| 39 | + comm_protocols.init(); |
| 40 | + |
| 41 | + // RS485/RS232 default config is: |
| 42 | + // - RS485/RS232 system disabled |
| 43 | + // - RS485 mode |
| 44 | + // - Half Duplex |
| 45 | + // - No A/B and Y/Z 120 Ohm termination enabled |
| 46 | + |
| 47 | + // Enable the RS485/RS232 system |
| 48 | + comm_protocols.rs485Enable(true); |
| 49 | + // Enable the RS232 mode |
| 50 | + comm_protocols.rs485ModeRS232(true); |
| 51 | + |
| 52 | + // Specify baudrate for RS232 communication |
| 53 | + comm_protocols.rs485.begin(115200); |
| 54 | + // Start in receive mode |
| 55 | + comm_protocols.rs485.receive(); |
| 56 | + |
| 57 | + Serial.println("Initialization done!"); |
| 58 | +} |
| 59 | + |
| 60 | +void loop() |
| 61 | +{ |
| 62 | + if (comm_protocols.rs485.available()) |
| 63 | + Serial.write(comm_protocols.rs485.read()); |
| 64 | + |
| 65 | + if (millis() > sendNow) { |
| 66 | + String log = "["; |
| 67 | + log += sendNow; |
| 68 | + log += "] "; |
| 69 | + |
| 70 | + String msg = "hello "; |
| 71 | + msg += counter++; |
| 72 | + |
| 73 | + log += msg; |
| 74 | + Serial.println(log); |
| 75 | + |
| 76 | + // Disable receive mode before transmission |
| 77 | + comm_protocols.rs485.noReceive(); |
| 78 | + |
| 79 | + comm_protocols.rs485.beginTransmission(); |
| 80 | + comm_protocols.rs485.println(msg); |
| 81 | + comm_protocols.rs485.endTransmission(); |
| 82 | + |
| 83 | + // Re-enable receive mode after transmission |
| 84 | + comm_protocols.rs485.receive(); |
| 85 | + |
| 86 | + sendNow = millis() + sendInterval; |
| 87 | + } |
| 88 | +} |
0 commit comments