📖 Scenario: You are setting up a simple Modbus TCP/IP communication between a PLC and a Modbus server. The PLC will read holding registers from the server.
🎯 Goal: Build a basic Modbus TCP/IP client script that connects to a Modbus server, reads holding registers, and displays the values.
📋 What You'll Learn
Create a dictionary called modbus_config with keys 'ip', 'port', and 'unit_id' with exact values.
Create a list called registers_to_read with the exact register addresses 100, 101, and 102.
Write a function called read_registers that takes ip, port, unit_id, and registers as parameters and returns a dictionary with register addresses as keys and dummy values as values.
Print the dictionary returned by read_registers.
💡 Why This Matters
🌍 Real World
Modbus TCP/IP is widely used in industrial automation to communicate between PLCs and devices like sensors and actuators over a network.
💼 Career
Understanding how to configure and read Modbus registers is essential for automation engineers and technicians working with industrial control systems.
Progress0 / 4 steps
1
Create Modbus connection configuration
Create a dictionary called modbus_config with these exact entries: 'ip': '192.168.0.10', 'port': 502, and 'unit_id': 1.
PLC Programming
Hint
Use curly braces to create a dictionary with the keys and values exactly as shown.
2
Specify registers to read
Create a list called registers_to_read with the exact register addresses 100, 101, and 102.
PLC Programming
Hint
Use square brackets to create a list with the three register numbers.
3
Write function to read registers
Write a function called read_registers that takes parameters ip, port, unit_id, and registers. It should return a dictionary with each register address as key and a dummy value equal to the register address multiplied by 10.
PLC Programming
Hint
Use a for loop to go through each register and create a dictionary with dummy values.
4
Display the read register values
Call the read_registers function with modbus_config['ip'], modbus_config['port'], modbus_config['unit_id'], and registers_to_read. Print the returned dictionary.
PLC Programming
Hint
Use the print function to show the dictionary returned by read_registers.
Practice
(1/5)
1. What is the main purpose of Modbus TCP/IP in PLC programming?
easy
A. To convert analog signals to digital
B. To program PLCs using ladder logic
C. To allow devices to communicate over Ethernet using simple commands
D. To create graphical user interfaces for PLCs
Solution
Step 1: Understand Modbus TCP/IP function
Modbus TCP/IP is a communication protocol that enables devices to exchange data over Ethernet networks.
Step 2: Identify the main use
It uses simple commands to read and write data between devices, facilitating communication.
Final Answer:
To allow devices to communicate over Ethernet using simple commands -> Option C
Quick Check:
Modbus TCP/IP = Ethernet communication protocol [OK]
Hint: Modbus TCP/IP is about device communication over Ethernet [OK]
Common Mistakes:
Confusing Modbus TCP/IP with PLC programming languages
Thinking it converts signals instead of communicating data
Assuming it creates user interfaces
2. Which of the following is the correct syntax to read holding registers using Modbus TCP/IP function code in a PLC script?
easy
A. ReadHoldingRegisters(address, quantity)
B. WriteCoils(address, quantity)
C. ReadInputStatus(address, quantity)
D. WriteHoldingRegisters(address, quantity)
Solution
Step 1: Identify function codes for reading registers
Modbus function code 03 reads holding registers, typically called ReadHoldingRegisters.
Step 2: Match syntax to function
ReadHoldingRegisters(address, quantity) correctly represents reading holding registers by specifying start address and number of registers.
Final Answer:
ReadHoldingRegisters(address, quantity) -> Option A
Hint: Read holding registers uses ReadHoldingRegisters() function [OK]
Common Mistakes:
Using Write functions instead of Read
Confusing coils with registers
Using incorrect function names
3. Given the following PLC script snippet using Modbus TCP/IP:
response = ReadCoils(10, 5)
print(response)
What is the expected output if coils 10 to 14 are ON, OFF, ON, OFF, ON respectively?
medium
A. [1, 0, 1, 0, 1]
B. Error: Invalid function
C. [ON, OFF, ON, OFF, ON]
D. [True, False, True, False, True]
Solution
Step 1: Understand ReadCoils function output
ReadCoils returns a list of boolean values representing coil states: True for ON, False for OFF.
Step 2: Map coil states to boolean list
Coils 10 to 14 states ON, OFF, ON, OFF, ON map to [True, False, True, False, True].
Final Answer:
[True, False, True, False, True] -> Option D
Quick Check:
Coil states as booleans = [True, False, True, False, True] [OK]
Hint: ReadCoils returns booleans True/False, not strings or numbers [OK]
Common Mistakes:
Expecting numeric 1/0 instead of True/False
Using string 'ON'/'OFF' instead of booleans
Assuming function returns error
4. A PLC script using Modbus TCP/IP to write to coils is:
WriteCoils(5, [True, False, True])
Which of the following correctly describes the WriteCoils function?
medium
A. The function WriteCoils requires a starting address and a list of boolean values
B. The list should contain integers 1 and 0 instead of booleans
C. The function WriteCoils only writes single coil, not multiple
D. The address 5 is out of range for coils
Solution
Step 1: Check WriteCoils function parameters
WriteCoils expects a starting address and a list of boolean values to write multiple coils.
Step 2: Verify parameter usage
The given call correctly provides the starting address and list of boolean values.
Step 3: Analyze options
The accurate statement is that the function requires a starting address and a list of boolean values. Other statements are incorrect: address range is not specified as invalid, booleans are appropriate (not integers), and the function supports multiple coils.
Final Answer:
The function WriteCoils requires a starting address and a list of boolean values -> Option A
Quick Check:
WriteCoils(address, bool list) is correct syntax [OK]
Hint: WriteCoils needs address plus boolean list, not integers [OK]
Common Mistakes:
Using integers instead of booleans
Assuming WriteCoils writes only one coil
Ignoring address range validity
5. You want to read 10 holding registers starting at address 100 and write the sum of their values to register 200 using Modbus TCP/IP in a PLC script. Which approach correctly combines reading, summing, and writing?