@dobot-plus/template 1.3.0 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lua/daemon.lua +1 -1
- package/lua/userAPI.lua +1 -1
- package/lua/utils/modbus.lua +50 -0
- package/package.json +1 -1
- package/lua/utils/await485.lua +0 -55
- package/lua/utils/num_convert.lua +0 -41
- package/lua/utils/tcp.lua +0 -31
- package/lua/utils/util.lua +0 -31
package/lua/daemon.lua
CHANGED
package/lua/userAPI.lua
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require('$PLUGIN_NAME$.variables')
|
|
2
|
+
require("$PLUGIN_NAME$.util")
|
|
3
|
+
local lockerName = "$PLUGIN_NAME$_Locker"
|
|
4
|
+
|
|
5
|
+
--- SetTool485(baudrate, parity, stop, identify)
|
|
6
|
+
--- err, id = ModbusCreate(ip, port, slaveId, isRTU): use endTool for 485 communication
|
|
7
|
+
--- err:int, 0: success, 1: over max master num, 2: master init failed, 3: master connect to poll failed
|
|
8
|
+
--- err, id = ModbusRTUCreate(slave_id, baud, parity, data_bit, stop_bit): use controller for 485 communication
|
|
9
|
+
function createModbusMasterClient(slaveID)
|
|
10
|
+
SetTool485(115200, 'E', 1, 1)
|
|
11
|
+
local err, id = ModbusCreate("127.0.0.1", 60000, tonumber(slaveID), true)
|
|
12
|
+
--- local err, id = ModbusRTUCreate(slave_id, baud, parity, data_bit, stop_bit)
|
|
13
|
+
if err ~= 0 then
|
|
14
|
+
print("Schunk ModbusCreate error:"..err)
|
|
15
|
+
return nil
|
|
16
|
+
end
|
|
17
|
+
return id
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
--- @param reg table {addrDec: number, length: number}
|
|
21
|
+
function getRegsData(reg, slaveID)
|
|
22
|
+
local clientID = createModbusMasterClient(slaveID)
|
|
23
|
+
if clientID == nil then
|
|
24
|
+
return {}
|
|
25
|
+
end
|
|
26
|
+
local result = Lock(lockerName, 10000, 10000)
|
|
27
|
+
Wait(10)
|
|
28
|
+
local data = GetHoldRegs(clientID, reg.addrDec, reg.length)
|
|
29
|
+
Wait(10)
|
|
30
|
+
UnLock(lockerName)
|
|
31
|
+
ModbusClose(clientID)
|
|
32
|
+
return data
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
--- @param reg table {addrDec: number, length: number}
|
|
36
|
+
--- @param values table length should be the same as reg.length
|
|
37
|
+
--- @return err int, 0: success, -1: failed
|
|
38
|
+
function setRegsData(reg, values, slaveID)
|
|
39
|
+
local clientID = createModbusMasterClient(slaveID)
|
|
40
|
+
if clientID == nil then
|
|
41
|
+
return -1
|
|
42
|
+
end
|
|
43
|
+
local result = Lock(lockerName, 10000, 10000)
|
|
44
|
+
Wait(10)
|
|
45
|
+
local err = SetHoldRegs(clientID, reg.addrDec, reg.length, values, 'U16')
|
|
46
|
+
Wait(10)
|
|
47
|
+
UnLock(lockerName)
|
|
48
|
+
ModbusClose(clientID)
|
|
49
|
+
return err
|
|
50
|
+
end
|
package/package.json
CHANGED
package/lua/utils/await485.lua
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
require('utils.variables')
|
|
2
|
-
require("utils.util")
|
|
3
|
-
local lockerName = "dobot_485_lock"
|
|
4
|
-
|
|
5
|
-
--- 配置并锁定 485 接口。
|
|
6
|
-
--- @param duration number|nil 锁定持续时间(毫秒),默认为 10000 毫秒。
|
|
7
|
-
--- @param waitTime number|nil 等待锁定的最大时间(毫秒),默认为 10000 毫秒。
|
|
8
|
-
--- @return boolean 返回是否成功锁定。
|
|
9
|
-
function Use485(duration, waitTime)
|
|
10
|
-
local _duration = 10000
|
|
11
|
-
if duration then
|
|
12
|
-
_duration = duration
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
local result = Lock(lockerName, _duration, waitTime == nil and 10000 or waitTime)
|
|
16
|
-
Wait(10)
|
|
17
|
-
return result
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
--- 使用 485 接口写入 Modbus 寄存器。
|
|
21
|
-
--- @param modbusID number Modbus 从机地址。
|
|
22
|
-
--- @param address number 寄存器地址。
|
|
23
|
-
--- @param datas table 要写入的数据列表。
|
|
24
|
-
--- @return boolean 返回是否成功写入。
|
|
25
|
-
function WriteBy485(modbusID, address, datas)
|
|
26
|
-
local result = Lock(lockerName, 10000, 10000)
|
|
27
|
-
Wait(10)
|
|
28
|
-
|
|
29
|
-
SetHoldRegs(modbusID, address, #datas, datas)
|
|
30
|
-
|
|
31
|
-
Wait(10)
|
|
32
|
-
UnLock485()
|
|
33
|
-
return result
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
--- 使用 485 接口读取 Modbus 寄存器。
|
|
37
|
-
--- @param modbusID number Modbus 从机地址。
|
|
38
|
-
--- @param address number 寄存器地址。
|
|
39
|
-
--- @param num number 要读取的寄存器数量。
|
|
40
|
-
--- @return table 返回读取的数据列表。
|
|
41
|
-
function ReadBy485(modbusID, address, num)
|
|
42
|
-
local result = Lock(lockerName, 10000, 10000)
|
|
43
|
-
Wait(10)
|
|
44
|
-
|
|
45
|
-
local registerData = GetHoldRegs(modbusID, address, num)
|
|
46
|
-
|
|
47
|
-
Wait(10)
|
|
48
|
-
UnLock485()
|
|
49
|
-
return registerData
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
--- 解锁 485 接口。
|
|
53
|
-
function UnLock485()
|
|
54
|
-
UnLock(lockerName)
|
|
55
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
local NumberConversion = {}
|
|
2
|
-
|
|
3
|
-
-- 将二进制字符串转换为十进制数
|
|
4
|
-
function NumberConversion.binaryToDecimal(binaryStr)
|
|
5
|
-
return tonumber(binaryStr, 2)
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
-- 将十进制数转换为二进制字符串
|
|
9
|
-
function NumberConversion.decimalToBinary(dec)
|
|
10
|
-
local binaryStr = ""
|
|
11
|
-
while dec > 0 do
|
|
12
|
-
local remainder = dec % 2
|
|
13
|
-
binaryStr = remainder .. binaryStr
|
|
14
|
-
dec = math.floor(dec / 2)
|
|
15
|
-
end
|
|
16
|
-
return binaryStr == "" and "0" or binaryStr
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
-- 将十六进制字符串转换为十进制数
|
|
20
|
-
function NumberConversion.hexToDecimal(hexStr)
|
|
21
|
-
return tonumber(hexStr, 16)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
-- 将十进制数转换为十六进制字符串
|
|
25
|
-
function NumberConversion.decimalToHex(dec)
|
|
26
|
-
return string.format("%X", dec)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
-- 将二进制字符串转换为十六进制字符串
|
|
30
|
-
function NumberConversion.binaryToHex(binaryStr)
|
|
31
|
-
local dec = NumberConversion.binaryToDecimal(binaryStr)
|
|
32
|
-
return NumberConversion.decimalToHex(dec)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
-- 将十六进制字符串转换为二进制字符串
|
|
36
|
-
function NumberConversion.hexToBinary(hexStr)
|
|
37
|
-
local dec = NumberConversion.hexToDecimal(hexStr)
|
|
38
|
-
return NumberConversion.decimalToBinary(dec)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
return NumberConversion
|
package/lua/utils/tcp.lua
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
--- 通用TCP连接函数
|
|
2
|
-
---@param ip string
|
|
3
|
-
---@param port number
|
|
4
|
-
---@param timeout number
|
|
5
|
-
---@return table result
|
|
6
|
-
function CreateTCPConnection(ip, port, timeout)
|
|
7
|
-
-- 初始化错误码和socket对象
|
|
8
|
-
local SUCCESS = 0
|
|
9
|
-
local FAIL = 1
|
|
10
|
-
local err = SUCCESS
|
|
11
|
-
local Socket = nil
|
|
12
|
-
|
|
13
|
-
-- 创建TCP客户端
|
|
14
|
-
err, Socket = TCPCreate(false, ip, port)
|
|
15
|
-
if err ~= SUCCESS then
|
|
16
|
-
print("Create TCP Client failed, code:", err)
|
|
17
|
-
return { errorCode = err, socket = nil }
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
-- 尝试建立TCP连接
|
|
21
|
-
err = TCPStart(Socket, timeout or 10) -- 使用传入的timeout,默认10秒
|
|
22
|
-
if err ~= SUCCESS then
|
|
23
|
-
print("Connect TCP Client failed, code:", err)
|
|
24
|
-
TCPDestroy(Socket)
|
|
25
|
-
return { errorCode = err, socket = nil }
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
-- 连接成功,返回成功状态和socket对象
|
|
29
|
-
print("Connect TCP Client Success!")
|
|
30
|
-
return { errorCode = nil, socket = Socket }
|
|
31
|
-
end
|
package/lua/utils/util.lua
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
local modbusIDS = {}
|
|
2
|
-
--- 使用modbus协议修改或读取数据
|
|
3
|
-
---@param cb function 创建modbus成功后执行的回调
|
|
4
|
-
---@param id number modebus链接的id
|
|
5
|
-
function HandleByModbus(cb, id, baudRate)
|
|
6
|
-
local DEFAULT_BAUDRATE = 115200
|
|
7
|
-
local MODBUS_IP = "127.0.0.1"
|
|
8
|
-
local MODBUS_PORT = 60000
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if type(id) ~= "number" then
|
|
12
|
-
return nil
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
if modbusIDS[id] == nil then
|
|
16
|
-
local err, _modbus = ModbusCreate(MODBUS_IP, MODBUS_PORT, id, true)
|
|
17
|
-
if err ~= 0 then
|
|
18
|
-
return nil
|
|
19
|
-
else
|
|
20
|
-
modbusIDS[tonumber(id)] = _modbus
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
local res = nil
|
|
25
|
-
|
|
26
|
-
if type(cb) == "function" then
|
|
27
|
-
res = cb(modbusIDS[id])
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
return res
|
|
31
|
-
end
|