You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/05.pro-solutions/solutions-and-kits/portenta-machine-control/tutorials/user-manual/content.md
+218Lines changed: 218 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1434,6 +1434,224 @@ You should see the four onboard LEDs of the Opta™ device turn on and off, as s
1434
1434
1435
1435

1436
1436
1437
+
The next example shows how to establish Modbus RTU communication between two Portenta Machine Control devices. Since Portenta Machine Control supports half-duplex and full-duplex mode, each mode requires different wiring setup.
1438
+
1439
+
We will begin showing full-duplex mode example following the connection diagram below between two Portenta Machine Control devices:
1440
+
1441
+
TODO Update image with two PMC in full-duplex wiring
1442
+
1443
+

1444
+
1445
+
The following script defines a Portenta Machine Control as a Client device, which sends 4 coils to the Server Portenta Machine Control.
1446
+
1447
+
```arduino
1448
+
/*
1449
+
Portenta's Machine Control Modbus RTU Client Example
// Check for successful transmission and report errors if any
1519
+
// Print error code if transmission failed
1520
+
// Or confirm successful coil value writing
1521
+
if (!ModbusRTUClient.endTransmission()) {
1522
+
Serial.print("- Failed! Error code: ");
1523
+
Serial.println(ModbusRTUClient.lastError());
1524
+
} else {
1525
+
Serial.println("- Success!");
1526
+
}
1527
+
1528
+
// Delay before next operation to simulate periodic control
1529
+
delay(1000);
1530
+
}
1531
+
```
1532
+
1533
+
Because the Portenta Machine Control is operating in Full-Duplex mode, the following line is important to enable Full-Duplex mode:
1534
+
1535
+
```arduino
1536
+
MachineControl_RS485Comm.setFullDuplex(true);
1537
+
```
1538
+
1539
+
The Server Portenta Machine Control uses the script below, which translates received coils into corresponding Digital Outputs. It will blink four Digital Outputs accordingly in timely manner.
1540
+
1541
+
```arduino
1542
+
// Include the necessary libraries
1543
+
#include <ArduinoRS485.h>
1544
+
#include <ArduinoModbus.h>
1545
+
#include <Arduino_PortentaMachineControl.h>
1546
+
1547
+
// Define the number of coils to control LEDs
1548
+
const int numCoils = 4;
1549
+
1550
+
// Define the baud rate for Modbus communication
1551
+
constexpr auto baudrate{ 38400 };
1552
+
1553
+
// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
1554
+
// Modbus over serial line specification and implementation guide V1.02
// Enable full duplex mode and 120 Ohm termination resistors
1572
+
MachineControl_RS485Comm.setFullDuplex(true);
1573
+
1574
+
// Set the RS-485 interface in receive mode initially
1575
+
MachineControl_RS485Comm.receive();
1576
+
1577
+
// Start the Modbus RTU server with a specific slave ID and baud rate
1578
+
// Halt execution if the server fails to start
1579
+
if (!ModbusRTUServer.begin(MachineControl_RS485Comm, 1, baudrate, SERIAL_8N1)) {
1580
+
Serial.println("- Failed to start Modbus RTU Server!");
1581
+
while (1)
1582
+
;
1583
+
}
1584
+
1585
+
//Set over current behavior of all channels to latch mode (true)
1586
+
MachineControl_DigitalOutputs.begin(true);
1587
+
1588
+
//At startup set all channels to OPEN
1589
+
MachineControl_DigitalOutputs.writeAll(0);
1590
+
1591
+
MachineControl_DigitalOutputs.write(7, HIGH);
1592
+
1593
+
// Configure coils for controlling the onboard LEDs
1594
+
ModbusRTUServer.configureCoils(0x00, numCoils);
1595
+
}
1596
+
1597
+
void loop() {
1598
+
// Poll for Modbus RTU requests and process them
1599
+
int packetReceived = ModbusRTUServer.poll();
1600
+
Serial.println(packetReceived);
1601
+
if (packetReceived) {
1602
+
// Process each coil's state and control LEDs accordingly
1603
+
for (int i = 0; i < numCoils; i++) {
1604
+
// Read coil value
1605
+
// Update discrete input with the coil's state
1606
+
int coilValue = ModbusRTUServer.coilRead(i);
1607
+
ModbusRTUServer.discreteInputWrite(i, coilValue);
1608
+
1609
+
// Debug output to the IDE's serial monitor
1610
+
Serial.print("LED ");
1611
+
Serial.print(i);
1612
+
Serial.print(" = ");
1613
+
Serial.println(coilValue);
1614
+
1615
+
// Control the onboard LEDs based on the coil values
1616
+
switch (i) {
1617
+
case 0:
1618
+
MachineControl_DigitalOutputs.write(0, coilValue ? HIGH : LOW);
1619
+
break;
1620
+
case 1:
1621
+
MachineControl_DigitalOutputs.write(1, coilValue ? HIGH : LOW);
1622
+
break;
1623
+
case 2:
1624
+
MachineControl_DigitalOutputs.write(2, coilValue ? HIGH : LOW);
1625
+
break;
1626
+
case 3:
1627
+
MachineControl_DigitalOutputs.write(3, coilValue ? HIGH : LOW);
1628
+
// New line for better readability
1629
+
Serial.println();
1630
+
break;
1631
+
default:
1632
+
// Error handling for unexpected coil addresses
1633
+
Serial.println("- Output out of scope!");
1634
+
break;
1635
+
}
1636
+
}
1637
+
}
1638
+
}
1639
+
```
1640
+
1641
+
To establish communication between two Portenta Machine Control with Modbus RTU in Half-Duplex mode, following wiring setup is required:
1642
+
1643
+
TODO Update image with two PMC in half-duplex wiring
1644
+
1645
+

1646
+
1647
+
Previous examples can be used in Half-Duplex mode and it requires only one minor change in the following line:
1648
+
1649
+
```arduino
1650
+
MachineControl_RS485Comm.setFullDuplex(false);
1651
+
```
1652
+
1653
+
The line can be updated to disable *Full-Duplex* mode or by commenting the line to ignore the corresponding process.
1654
+
1437
1655
#### Modbus TCP
1438
1656
1439
1657
Modbus TCP, taking advantage of Ethernet connectivity, allows easy integration with existing computer networks and facilitates data communication over long distances using the existing network infrastructure. It operates in full-duplex mode, allowing simultaneous sending and receiving of data.
0 commit comments