Hello Guest it is November 15, 2019, 05:37:51 PM

Author Topic: Making a connection through rs232 in Mach4  (Read 1791 times)

0 Members and 1 Guest are viewing this topic.

Making a connection through rs232 in Mach4
« on: July 24, 2015, 05:42:29 PM »
I posted this in another thread but wanted to bring it to the top to see if anyone could help.
We have a need to have Mach make a serial connection and send and receive serial data.

I'm new to Lua but have done a little vb developmet. I've done only a couple of things that required serial com.

I searched through as much documentation as I could find in the forums with no luck (Special thanks to Scott “Poppa Bear” Shafer and Ya-Nvr-No)
McLua mc Scripting Reference SpacedOut
McLua Work Offsets and Parameters1
Pound Var List-revised1-2
Mach4 Scripting Manual
Mach4 ENUM list for mc1
Mach4 ENUM list for mc1

I installed the "Lua for windows" package which comes with the luars232.dll. I ran the example for the luars232.dll and was able to make a connection to my com ports on my machine.

I tried to do the same thing in mach4 by placing the dll in the Mach4Hobby folder (the root folder is in the PATH). I then created a new Lua script (tim.lua) and wrote the following:
rs232 = require("luars232")

 This was the result:

Compilation successful!
Output: "C:\Mach4Hobby\LuaExamples\Tims.mcc"
Waiting for client connection...
Client connected ok.
At Breakpoint line: 1 file: C:\Mach4Hobby\LuaExamples\Tims.lua
mcLua ERROR: Lua: Error while running chunk
error loading module 'luars232' from file 'C:\Mach4Hobby\luars232.dll':
   The specified module could not be found.

stack traceback:
   [C]: in ?
   [C]: in function 'require'
   [string "C:\Mach4Hobby\LuaExamples\Tims.lua"]:1: in main chunk

mcLua ERROR: Lua: Error while running chunk

Debug session finished.

It "Seems" to want to load but gets hung up somewhere. It could definitely be pilot error on my part as I do have some coding experience but far from an expert...

A simple, documented example would be the greatest help but I am more than happy to dive in to any doc that has the answer.

Offline BR549

  •  6,916 6,916
    • View Profile
Re: Making a connection through rs232 in Mach4
« Reply #1 on: July 24, 2015, 06:49:57 PM »
I had tried the same type thing with using Lua sockets and got the same results. It seems there is something MISSING in Mach4 that allows it to USE the outside functions. OR none of us know the secret handshake to make it work(;-). I could be a simple PATH problem as the Sockets are LOOKING for a Lua install . It may NOT know that Mach4 IS LUA..

I was trying to get Mach4 to be able to TEXT and email. Something Mach3 does quiet well with Cypress Basic.  The ONLY way I could get it to work was to Os.execute out and from WINDOS RUN a CB/vb script that I used with Mach3 . From there it worked.  Just a wee bit clumsy to use.

(;-) TP 
Re: Making a connection through rs232 in Mach4
« Reply #2 on: July 25, 2015, 08:34:07 AM »

i had the exact same problem trying to run luacom.dll

mcLua ERROR: Lua: Error while running chunk
error loading module 'luacom.dll' from file 'C:\Mach4Hobby\luacom.dll':
   The specified module could not be found.

Re: Making a connection through rs232 in Mach4
« Reply #3 on: May 16, 2019, 10:41:06 AM »
I've successfully written scripts to communicate over RS232 using ASCII protocol (not Modbus). There is essentially no documentation on it, but the coding isn't too hard.

Start with the following code placed near the top of your Screen Load script:

--Place this code at the top of the screen load script
--Open the COM port immediately upon loading Mach 4. Com port remains open until Mach 4 closes

Code: [Select]
rs232 = require("luars232")
port_name = "COM1"
local out = io.stderr

-- Initialize Communications --

--Open Serial Port 8N1 38400 baud
local e, p = rs232.open(port_name)
if e ~= rs232.RS232_ERR_NOERROR then
mc.mcCntlSetLastError(inst, "Cannot Open Serial Port")

-- set port settings
assert(p:set_baud_rate(rs232.RS232_BAUD_38400) == rs232.RS232_ERR_NOERROR)
assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
assert(p:set_flow_control(rs232.RS232_FLOW_OFF)  == rs232.RS232_ERR_NOERROR)

This opens a serial port on your computer and allows you to read and write.

The serial write function takes a string and transmits it over the serial port opened above. It returns an error if applicable, and the length of the string written on the port.

Code: [Select]
err, len_written = p:write(string, timeout)
The Serial Read function pulls the characters from Windows' serial buffer (default 4096 bytes/characters) which keeps track of the incoming data behind the scenes. Once you read a character from the buffer, it is gone forever so reading empties the buffer. It returns an error if applicable, the data (as a string), and the length/size of the string.

Code: [Select]
err, data_read, size = p:read(read_length, timeout)

This is a very high level overview. I'd be happy explain more if this is what you are looking for.

I should add that no special packages, libraries, dll's, or anything else need to be added for this to work. Just a clean install of a recent version of Mach 4.
« Last Edit: May 16, 2019, 10:43:29 AM by mcardoso »
Re: Making a connection through rs232 in Mach4
« Reply #4 on: May 16, 2019, 12:20:54 PM »
You see, if you just sit at your computer and wait long enough, the answer will arrive...

Thanks for responding with a solution mcardoso!!! Even when these threads are years old, it is soo nice to have the solution, not only for myself (I don't need it now but YOU KNOW I've got to try it) and for others who run into this.

Re: Making a connection through rs232 in Mach4
« Reply #5 on: May 16, 2019, 12:47:10 PM »
Lol, didn't even see this was 4 years old when I replied. Glad to help. Been using the serial communications to pull data from my servo drives and it is very reliable. I have a lot more info on it if anyone needs it, but it is a niche application.