-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestModbus
More file actions
30 lines (25 loc) · 1.18 KB
/
TestModbus
File metadata and controls
30 lines (25 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
This code was used to test modbus on the UR5 robot-arm.
This code was written and last updated by Trym Falkenhaug 05/03/2024
We "borrowed" this code 21/11/2024
"""
# This code is written with AI and help, it might not work (Kommer ann på hvem du er)
from pymodbus.client import ModbusTcpClient
# Define the IP address and port of the UR5e robot
ROBOT_IP = '192.168.0.102' # Example IP address, replace with the actual IP address of your robot
PORT = 502 # Default Modbus port for UR robots
# Connect to the robot
client = ModbusTcpClient(ROBOT_IP, port=PORT)
client.connect()
# Read from 16-bit registers
register_address = 128 # Starting address of the general-purpose 16-bit registers
num_registers_to_read = 1 # Number of 16-bit registers to read (addresses 128 to 255)
registers = client.read_holding_registers(register_address, num_registers_to_read)
print("16-bit Registers:", registers.registers)
# Write to 16-bit registers
register_to_write = 129 # Address of the register to write
value_to_write = 123 # Value to write
client.write_register(register_to_write, value_to_write)
print("Wrote value", value_to_write, "to register", register_to_write)
# Close the connection
client.close()