Hello Guest it is April 17, 2021, 08:21:32 AM

Author Topic: Mach4 3713 + Lua Socket + IPG Laser = Problems  (Read 969 times)

0 Members and 1 Guest are viewing this topic.

Mach4 3713 + Lua Socket + IPG Laser = Problems
« on: March 23, 2018, 08:44:00 AM »
Hello.
I use Mach4 3713, the computer is connected to an Ethernet network, IPG Laser also connected to it. The laser accepts text commands in one line.
For example: "ABN" - turn on Beam Guide (return response "ABN"), "ABF" - turn off Beam Guide (return response "ABF"). If the command is accepted by the laser, but is not implemented, it returns "BCMD"
I made a module IPGEthernet.lua , put it in a folder "Modules".
Code: [Select]
local IPGEthernet = {}
local inst = mc.mcGetInstance()

package.path = package.path .. ";./Modules/?.lua;"
package.cpath = package.cpath .. ";./Modules/?.dll;"
--package.cpath = "C:/src/Mach4/Modules/?.dll;"
rs232 = require("luars232")
socket = require("socket")

---------------------------------------------------------------
-- IPG -- Ethernet -- TCP/IP
---------------------------------------------------------------
function IPGEthernet.Ethernet(IP, message, len)
local port = 10001
local tcp = assert(socket.tcp())
local data
tcp:connect(IP, port)
tcp:send(message .. "\n")
data = tcp:receive(len)
tcp:close()
data = tostring(data)
if data == message then
wx.wxMessageBox("Command "..message.." executed!")
else
wx.wxMessageBox("Command "..message.." unfulfilled!")
end
--return data
end
--
return IPGEthernet
I made a button on the screen:
Code: [Select]
local A, B, C
A = scr.GetProperty('txtIp', 'Value')
A = tostring(A)
--wx.wxMessageBox(A)

B = "ABN"
--wx.wxMessageBox(tostring(B))

C = string.len(B)
--wx.wxMessageBox(tostring(C))

IPGEthernet.Ethernet(A, B, C)
The script works.
Problem: If I specify the wrong length in "data = tcp:receive(len)", then the script hangs.
Command "STA" (Read Device Status) returns "STA: 4100" (32-bit word), can return "STA: 4294967295", can return "STA: 1".
Question: how to write the script correctly to calculate any length (without knowing it)?
Thanks for the help. Sorry for my English.
Re: Mach4 3713 + Lua Socket + IPG Laser = Problems
« Reply #1 on: March 26, 2018, 01:23:57 AM »
This script helped me.
socket = require("socket")
client = socket.connect("10.9.9.10",10001)
--client:send("GET /monitor.cgx HTTP/1.1\n")
client:send("SDC 105.0\n")
client:settimeout(0.1)
repeat
  --print "read"
  line,err,rest = client:receive()
  --print "read done"
  --if line then print(line) end
  if rest then print(rest) end
until err

print "all done"
Re: Mach4 3713 + Lua Socket + IPG Laser = Problems
« Reply #2 on: March 26, 2018, 01:32:51 AM »
I get this line:
{"ver":"1","RIMON":"0.2","RMT":"CW","ROP":"Off","RPP":"Off","RCT":"32.0","RBT":"33.0","RET":"31","STA":"67108864","RID":"YLR-1500-MM-WC","RFV":"32.7.101;2.95;3.7.1","RSN":"R18020185","RCS":"0.0","RPW":"10.000","RDC":"10.0","RDCmax":"99.0","RPRR":"10.0","REC":"1","RMEC":"0","RLHN":"IPG-A15-0044","RDHCP":"OFF","RIP":"10.9.9.10","RMASK":"255.255.255.0","RDGW":"10.9.9.5","RMAC":"D8-80-39-59-D8-95","FST":"10","RPRRL":"1.0","RPRRH":"50000.0","RBAUD":"8","RCFG":"141","WFCFG":"0","WFID":"0"}
It can be of different lengths, but the number of parameters does not change. How do I remove brackets (for unknown string length), divide the string by the symbol "," and move it to the table?
t[ver] = "1"
tRIMON] = "0.2"
t[RMT] = "CW"
?
Re: Mach4 3713 + Lua Socket + IPG Laser = Problems
« Reply #3 on: March 28, 2018, 01:30:55 AM »
This script helped me.
local JSON = require ("json")
local a = '{строка Json}'
local b = JSON:decode(a)
print(tostring(b["..."]))