Hello Guest it is April 18, 2024, 07:54:39 AM

Author Topic: Making a connection through rs232 in Mach4  (Read 8273 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.
Thanks
-Tim

Offline BR549

*
  •  6,965 6,965
    • 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 »
FYI

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")
return
end

-- 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.

-Tim
KN6BJG
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.
Re: Making a connection through rs232 in Mach4
« Reply #6 on: November 25, 2019, 07:50:33 PM »
@mcardoso,
I sent a message to you earlier today. I'd really like to get this working on my machine. I did as you said but keep getting the "Cannot Open Serial Port" Error along with three "Lua:Error while running chunkattempt to call a nil valuestack traceback". I'm not a coder for a living but I can usually figure things out with minimal guidance. If you could maybe help me just get connected I'd greatly appreciate it.
Re: Making a connection through rs232 in Mach4
« Reply #7 on: November 25, 2019, 08:12:39 PM »
OK. Lets see here. You're missing part of your error code so I'll need to do some guessing. When LUA gives you the stack traceback errors they are a roadmap to how your code failed. Try to read and understand them as they are very valuable once you learn them. The "attempt to call a nil value" usually means you forgot to define or assign a value to something. All undefined variables have the value of nil and you can even assign nil to a variable if you would like. For example the code:

Code: [Select]
print(string)
Would create the "attempt to call a nil value" error since string doesn't exist. However, the code:

Code: [Select]
string = "Hello World"
print(string)

Would run perfectly fine.

I have recently been helping another Mach 4 user with their serial code. You might find this thread useful: https://www.machsupport.com/forum/index.php?topic=42084.0


First guess is that you did not assign a global value to "port_name" up at the top. This is the string value of the name of the COM port that windows assigns. When in doubt, open Windows->Control Panel->Device Manager to see the name of the COM port. Make sure you typed the name exactly.

Second guess is that you typed the name correctly but the port doesn't exist or couldn't be opened. I like to use a terminal emulator called PuTTY to test my serial communications. Make sure you can open a port and communicate through the terminal before you bother trying with Mach 4.

Third guess, once the port is open, it has the name "p" based on the example code above. All the methods in the serial library should be called using the name "p". For example:

Code: [Select]
p:close()
p:flush()
p:read()
p:write()

Only the function call to open the port should use "port_name".

If you post the entirety of your stack traceback then we can troubleshoot further. Please make sure to compile your code in ZeroBrane studio before trying to run it in Mach 4.

Mike
Re: Making a connection through rs232 in Mach4
« Reply #8 on: November 25, 2019, 08:19:19 PM »
Ah, OK,  I'll double check everything and try to get to know / understand the error messaging in Mach. I am familiar with nil and I have worked with Putty so I'll review and try to figure it out. I'll also review the link in your response as well. I was just parsing through other posts you've made earlier regarding RS232.  If I could just get connected the rest will fall into place :)

THANK YOU SOOO MUCH for taking your time tonight and responding. It's rare... But really appreciated!
Re: Making a connection through rs232 in Mach4
« Reply #9 on: November 25, 2019, 08:48:43 PM »
Hi Mike,
So I did review everything and double checeked that I was using the right port and speed with putty. I took some screenshots just so you could see.
I also tend to use a button and try to test out code so I took a screen shot of what I was doing there.
I did shut off Putty and rebooted Mach just to be safe. I built a simple app on the Arduino to test with (See the picture)

Did I miss something
-Tim