Hello Guest it is March 28, 2024, 05:47:51 PM

Author Topic: Sending Info to a Database from a Script  (Read 5674 times)

0 Members and 1 Guest are viewing this topic.

Sending Info to a Database from a Script
« on: February 05, 2019, 06:42:23 PM »
I am trying to send data to a data base using the Lua Script - one of the issues I have is I am not familiar with Mach4 or Lua . Can I do this another way ? My Gcode programmer has Gcode writen where he can call macros , I want to be able to have him call M150 with 3 maybe 4 parameters that I can use a stored procedure in my macro to store the 4 parameters he sends. I would also like to use Script to return a value to his GCode .

I tried installing Lua-luasql-mysql drivers with no success , I tried installing sqlite drivers with no success (it seems to want the source code .h files)  so I am going to try to install the odbc drivers .

Does anyone have any process or method whereby I can send data to a database via Mach4 while the GCode is running - because this is where his data is generated and I want to capture that. I do not want to get to hacky with it - would like something in KISS format.
Re: Sending Info to a Database from a Script
« Reply #1 on: February 05, 2019, 06:54:27 PM »
Hi,
I'm not familiar with sending data to a database but I am familiar with having Lua scripts open files
to either read or write data. Would that work?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Sending Info to a Database from a Script
« Reply #2 on: February 05, 2019, 07:17:54 PM »
I will take that if you have it ..

And thank you very much .. I can use a hotfolder and update from there - if you would like I could send back a copy to you.
Re: Sending Info to a Database from a Script
« Reply #3 on: February 05, 2019, 08:03:33 PM »
Hi,
the simplest is an example.....most if not all of my file opening/closing has started life as an example which ships with Mach4.

Mach4Hobby/LuaExamples/ProbeToFile/M401.mcs

That provides a simple file dialog using wxLua to browse to either an existing file or create a new one.
Study it and see how it works. I've always struggled with wxLua but in recent weeks have done a bit more work on wxLua,
the wxWidgets reference manual and most recently wxFormBuilder. I may be able to help with some of the simplest of
questions.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Sending Info to a Database from a Script
« Reply #4 on: February 05, 2019, 08:47:22 PM »
I don't have Mach4Hobby I do not see those folders LuaExamples or that file . Could you post that file for me here?

I will not be using wxLua that I know of ...at least for the moment - I intend to automate sending the data to the data base as the process runs ..without human intervention.

I have Mach4 folder but nothing like LuaExamples
Re: Sending Info to a Database from a Script
« Reply #5 on: February 05, 2019, 08:50:58 PM »
Hi Ken,
there are some differing styles of syntax when dealing with files, I often get confused and end up writing faulty code.
This is the style of syntax that I now use which seems to work best, or at least consistently:

local fileHandle=io.open(pathfilename,"w+")       --This opens a file with the path and file name =pathfilename in append write mode.

I can now refer to that file (as a first class value) as fileHandle. thus to write a line to the file would look like this:

fileHandle:write(offset,'\n')                              --This appends the string offset to the open file

And these statements flush the buffer and close the file when I've finished writing to it:

fileHandle:flush()
fileHandle:close()

There is a somewhat simpler syntax, syntactic sugar, I think its called but I get better results using the longer more formal approach.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Sending Info to a Database from a Script
« Reply #6 on: February 05, 2019, 08:56:00 PM »
Thank you Craig.

I like your meme in your signature - reminds me of a Clint Eastwood movie quote (at least I think it was). He was telling someone about what happened with his wife. He said His wife cheated with his best friend - so he shot her, and the man asked well what about your friend. Eastwood responds well he was my best friend.
Re: Sending Info to a Database from a Script
« Reply #7 on: February 05, 2019, 09:02:51 PM »
Hi,
the example is:

Code: [Select]
function m401()
inst = mc.mcGetInstance();
    -- create the wxFrame window
    mainframe = wx.wxFrame( wx.NULL,          -- no parent
                        wx.wxID_ANY,          -- whatever for wxWindow ID
                        "DummyFrame", -- frame caption
                        wx.wxDefaultPosition, -- place the frame in default position
                        wx.wxDefaultSize,     -- default frame size
                        wx.wxDEFAULT_FRAME_STYLE ) -- use default frame styles

    -- create a panel in the frame
    panel = wx.wxPanel(mainframe, wx.wxID_ANY)--We are not going to show it but we need to have this to use the File dialog

local file = wx.wxFileDialog(panel, "Select Probe File", "", "", "Text files (*.txt)|*.txt|Tap files (*.tap)|*.tap",
                             wx.wxFD_SAVE,wx.wxDefaultPosition,wx.wxDefaultSize, "File Dialog" );
        if(file:ShowModal() == wx.wxID_OK)then
            local path = file:GetPath()   
--wx.wxMessageBox(tostring(path))
--[[
Set the output of the probe points with the format String
Example:
X%.3AXIS_X
will output X<xprobevalue>
]]--
mc.mcCntlProbeFileOpen(inst, path, "X%.3AXIS_X Y%.3AXIS_Y Z%.3AXIS_Z A%.3AXIS_A\r\n", true);
        end

end

if (mc.mcInEditor() == 1) then
dofile ("load_modules.mcs")
    m401()
end

Hi you may not be particularly interested in wxLua but it is in fact an integral part of Mach, the GUI especially.
In this instance you will have m150 in your Gcode job and a function would run which among other things opens
a file to store results. You could have it run automatically but it would save to the same file on each and every occasion.
You could also have it run with this dialog so that you could choose the directory and filename of the file and the dialog
would dismiss itself thereafter for the remainder of the session.

One thing is almost certain that when you start manipulating files or databases I would guess then having wxLua dialogs
to chose the correct files and/or directories is going to be mandatory.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Sending Info to a Database from a Script
« Reply #8 on: February 05, 2019, 09:35:08 PM »
I have an Automated process that will run operator less continually hours on end - the process as it runs will need to update the database each and every time it finishes an item.

wx Components are usually gui elements as in python and they require operator intervention.

The file name I don't need to be concerned with - I can name the files randomly as long as I use the same extension. 


'One thing is almost certain that when you start manipulating files or databases I would guess then having wxLua dialogs
to chose the correct files and/or directories is going to be mandatory.'

No this is not accurate - typically I write code in .NET and other languages - however when recording data in a database it just involves passing the right parameters to the sql server or even a data file. Storing in a properly structured database is somewhat as easy as your code - if I could get LuaSql driver to install. basically like this:
connection.Open()
connection.parameters = x,y,z ,
connection.query ="myquery"
connection.execute
connection.close()

I do not write in LUA - first time working with it, and Mach4 I am very new to it as well - so this is a learning curve..


Re: Sending Info to a Database from a Script
« Reply #9 on: February 05, 2019, 10:00:32 PM »
Hi,
well I suspect you will have no trouble with Lua, it is deceptively simple. The syntax is "syntax that only a mother could
love' but the rest of it is fine.

I have this open when I am coding:

https://www.lua.org/manual/5.2/

Smurph tells us that the most recent builds of Mach now use Lua 5.3, there are a few subtle differences.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'