【ECEN004】Robot Read/Write other device data through ModBus

Introduction

Elibot support both ModBus Master and slave function. Elibot can use Modbus TCP and Modbus RTU.

Robot can read/ write device data through Modbus master.

To use Modbus TCP, user need to connect lan wire to device/hub through robot controller etherner port;

to use Modbus RTU, user can use RS485 port on robot controller and RS485 port on robot flange.

User need to use LUA script(instruction can be refered in Elibot Lua manual) to achieve Modbus function.

ModBus Master Lua instruction

Acquire Modbus RTU handle

ctx = modbus_new_rtu(choose, baud, parity, data_bit, stop_bit)

Parameter: choose: 0-serial port on core board; 2-serial port on flange IO

Baud: baud rate (up to 460800 for port on core board and 9600 for port on flange IO),

default 4800 if not defined.

Parity: ASCII code of “E”(69),”N”(78) or “O”(79). Default 78 if not defined.

Data_bit: 7/8. Default 8 if not defined.

Sto_bit:1/2. Default 1 if not defined.

Return: ctx-modbus handle

Note: This command is applicable to v2.9.3 and above.

example:

ctx = modbus_new_rtu(2, 9600, 78, 8, 1)

Acquire Modbus TCP handle

ctx = modbus_new_tcp(ip, port)

Parameter: IP: IP address of Modbus slave

                  Port: port number of Modbus slave

                  Return: ctx-modbus handle

example:

ctx = modbus_new_tcp("192.168.1.15", 502)

Open Modbus Handle

ret = modbus_connect(ctx)

Parameter: ctx created by modbus_new_rtu or modbus_new_tcp

Return: if TCP connection is failed to be established, return -1. Otherwise, returns any positive integer. (Fot TCP connection only, cannot be used to evaluate RTU connection.)

example:

modbus_connect(ctx)

Close Modbus Handle

modbus_close(ctx)

Parameter: ctx created by modbus_new_rtu or modbus_new_tcp

Return: None

example:

modbus_close(ctx)

Set slave address

ret = modbus_set_slave(ctx, slave_id)

Parameter: ctx: ctx created by modbus_new_rtu or modbus_new_tcp

slave_id: slave address

Return: if set failed, returns -1. Otherwise, returns any positive integer.

example:

modbus_set_slave(ctx ,0x2)


Write single register Func06

ret = modbus_write_register(ctx, reg, value)

Parameter: ctx: ctx created by modbus_new_rtu or modbus_new_tcp

                  reg: register address (in decimal)

                  value: data to be written

Return: if write failed, returns -1. Otherwise, returns any positive integer.

example:

modbus_write_register(ctx , 771, 1)

Read Multiple Coil Func01

reg_value = modbus_read_bits(ctx, reg, len)

Parameter: ctx: ctx created by modbus_new_rtu or modbus_new_tcp

                  reg: coli adress

                  len: number of coils(<128)

Return: ret

点击显示全文
赞同0
发表评论
分享

手机扫码分享
0
404
收藏
举报
收起
登录
  • 密码登录
  • 验证码登录
还没有账号,立即注册
还没有账号,立即注册
注册
已有账号,立即登录
选择发帖板块
举报
请选择举报理由
举报
举报说明