22 RS485 Full duplex communication
33
44 This sketch shows how to use the SP335ECR1 on the Machine
5- Control as a full duplex RS485 interface, how to periodically
5+ Control as a full duplex (AB and YZ) RS485 interface, how to periodically
66 send a string on the RS485 TX channel and how to receive data
77 from the interface RX channel.
88
99 Circuit:
1010 - Portenta H7
1111 - Machine Control
1212 - A Slave device with RS485 interface
13+ - Connect TXP to A(+) and TXN to B(-)
14+ - Connect RXP to Y(+) and RXN to Z(-)
1315
1416*/
1517
1618#include " Arduino_MachineControl.h"
1719
1820using namespace machinecontrol ;
1921
22+ constexpr unsigned long sendInterval { 1000 };
23+ unsigned long sendNow { 0 };
2024unsigned long counter = 0 ;
2125
2226void setup ()
@@ -29,37 +33,47 @@ void setup()
2933 delay (1000 );
3034 Serial.println (" Start RS485 initialization" );
3135
32- comm_protocols.rs485 .begin (115200 );
33- comm_protocols.rs485 .enable = 1 ; // SDHN_N
34- comm_protocols.rs485 .sel_485 = 1 ; // RS485_RS232_N
35- comm_protocols.rs485 .half_duplex = 0 ; // HALF_FULL_N
36- comm_protocols.rs485 .receive (); // RE_N
37- 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
38- comm_protocols.rs485 .term = 1 ; // TERM - 120 ohm A-B termination enabled when high
36+ // Set the PMC Communication Protocols to default config
37+ comm_protocols.init ();
38+ // RS485/RS232 default config is:
39+ // - RS485 mode
40+ // - Half Duplex
41+ // - No A/B and Y/Z 120 Ohm termination enabled
42+
43+ // Enable the RS485/RS232 system
44+ comm_protocols.rs485Enable (true );
45+
46+ // Enable Full Duplex mode
47+ // This will also enable A/B and Y/Z 120 Ohm termination resistors
48+ comm_protocols.rs485FullDuplex (true );
49+
50+ // Specify baudrate, and preamble and postamble times for RS485 communication
51+ comm_protocols.rs485 .begin (115200 , 0 , 500 );
52+
53+ // Start in receive mode
54+ comm_protocols.rs485 .receive ();
55+
3956
4057 Serial.println (" Initialization done!" );
4158}
4259
43- constexpr unsigned long sendInterval { 1000 };
44- unsigned long sendNow { 0 };
45-
46- constexpr unsigned long halfFullInterval { 5000 };
47- unsigned long halfFull { 0 };
48- byte halfFullStatus { 0 };
49-
5060void loop ()
5161{
52- while (comm_protocols.rs485 .available ())
62+ if (comm_protocols.rs485 .available ())
5363 Serial.write (comm_protocols.rs485 .read ());
5464
5565 if (millis () > sendNow) {
66+ // Disable receive mode before transmission
5667 comm_protocols.rs485 .noReceive ();
68+
5769 comm_protocols.rs485 .beginTransmission ();
5870
5971 comm_protocols.rs485 .print (" hello " );
6072 comm_protocols.rs485 .println (counter++);
6173
6274 comm_protocols.rs485 .endTransmission ();
75+
76+ // Re-enable receive mode after transmission
6377 comm_protocols.rs485 .receive ();
6478
6579 sendNow = millis () + sendInterval;
0 commit comments