1
Mach4 General Discussion / Test-button works but macro stalls - using luars232
« on: February 04, 2024, 11:37:48 PM »
Hello!
I'm currently working on what should be a pretty simple macro, and it seems very close to functioning (the test-button works!), but the macro is locking up when I run it, and I would appreciate any insight that could help me get it running properly.
A few basics:
- The function of the macro is to send a short string (a "serial number") to a printer over the serial port (using luars232).
- The serial number is a string of characters that are placed in the first line of my g-code. The macro grabs it and sends it over serial.
- I have made a test-button that successfully sends the short string to the printer using a "write" command.
- When I put the "write" command from the button into the macro, the macro locks up when I try to run it.
- The macro includes commands to stop cycle, sleep 1s, write the string, sleep 1s, set the next line, and start the cycle.
- The macro runs fine when I comment out the write command.
For reference, here is the code I'm currently using:
luars232 setup - This is installed near the top of my load-screen script and appears to be working great. I adjusted the port name and baud rate to match my setup, but otherwise this is exactly as shared by mcardoso.
Test-button code - This works just fine. I load up my gcode, click the button, and the button grabs the serial number and sends to my printer just fine.
Macro code "M104" - This seems to be where my problem is. When I run the macro it stalls out, and does not send any information to the serial port. However, if I comment out the write command, the macro runs just fine.
Sample G-code - Here's an example of the G-code I'm running. The first line is the serial number that will be sent to the printer by macro M104.
Does anyone have any thoughts as to why the write command works just fine when run from the test-button, but not in the macro? I'm hoping it's just me missing something simple, and not opening a big can of worms.
Thanks in advance for your thoughts and assistance.
Also, I need to mention that I would have had no idea where to start on this project if it weren't for a number of extremely helpful comments from user mcardoso regarding setup and use of luars232 in this post.
https://www.machsupport.com/forum/index.php?topic=30553.0
I'm currently working on what should be a pretty simple macro, and it seems very close to functioning (the test-button works!), but the macro is locking up when I run it, and I would appreciate any insight that could help me get it running properly.
A few basics:
- The function of the macro is to send a short string (a "serial number") to a printer over the serial port (using luars232).
- The serial number is a string of characters that are placed in the first line of my g-code. The macro grabs it and sends it over serial.
- I have made a test-button that successfully sends the short string to the printer using a "write" command.
- When I put the "write" command from the button into the macro, the macro locks up when I try to run it.
- The macro includes commands to stop cycle, sleep 1s, write the string, sleep 1s, set the next line, and start the cycle.
- The macro runs fine when I comment out the write command.
For reference, here is the code I'm currently using:
luars232 setup - This is installed near the top of my load-screen script and appears to be working great. I adjusted the port name and baud rate to match my setup, but otherwise this is exactly as shared by mcardoso.
Code: [Select]
rs232 = require("luars232")
port_name = "COM3"
local out = io.stderr
---------------------------------------------------------------
-- Initialize Communications --
---------------------------------------------------------------
--Open Serial Port 8N1 115200 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_115200) == 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)
Test-button code - This works just fine. I load up my gcode, click the button, and the button grabs the serial number and sends to my printer just fine.
Code: [Select]
local inst = mc.mcGetInstance()
local LabelParen = mc.mcCntlGetGcodeLine (inst, 0)
--gets name of next Gcode file MUST BE COMMENTED INTO LINE 0 OF GCODE
local Label = string.gsub(LabelParen, "[()%c%s]", "") --strips parentheses and hidden characters
p:write("\02\00\u{01B}\u{050}\u{001}\u{070}\u{017}"..Label, 5) --writes the label to serial port
Macro code "M104" - This seems to be where my problem is. When I run the macro it stalls out, and does not send any information to the serial port. However, if I comment out the write command, the macro runs just fine.
Code: [Select]
function M104 ()
local inst = mc.mcGetInstance()
mc.mcCntlCycleStop(inst)
wx.wxSleep(1)
local LabelParen = mc.mcCntlGetGcodeLine (inst, 0)
--grabs the label from gcode. MUST BE COMMENTED INTO LINE 0 OF GCODE
local Label = string.gsub(LabelParen, "[()%c%s]", "") --strips parentheses and hidden characters
p:write("\02\00\u{01B}\u{050}\u{001}\u{070}\u{017}"..Label, 5) --writes the label to serial port. MACRO WORKS IF THIS LINE IS COMMENTED OUT
wx.wxSleep(1)
local LineNum, rc = (mc.mcCntlGetGcodeLineNbr(inst) + 1) --look up current line number and add 1
mc.mcCntlSetGcodeLineNbr(inst,LineNum) --set next line number
mc.mcCntlCycleStart(inst) --start the cycle!
end
Sample G-code - Here's an example of the G-code I'm running. The first line is the serial number that will be sent to the printer by macro M104.
Code: [Select]
(#0001-06__A0-00__19.32")
M104
G00
S15000
F100
X0 Y2.8125
G01
X-9.6596 Y2.8006
X-14.4894 Y2.7788
X-19.319 Y2.7404
X-19.396 Y2.7561
X-19.4597 Y2.8019
X-19.4992 Y2.8698
X-19.5073 Y2.9479
X-19.4827 Y3.0225
X-19.4297 Y3.0805
X-19.3576 Y3.1117
G00
X0.6424 Y7.1117
Does anyone have any thoughts as to why the write command works just fine when run from the test-button, but not in the macro? I'm hoping it's just me missing something simple, and not opening a big can of worms.
Thanks in advance for your thoughts and assistance.
Also, I need to mention that I would have had no idea where to start on this project if it weren't for a number of extremely helpful comments from user mcardoso regarding setup and use of luars232 in this post.
https://www.machsupport.com/forum/index.php?topic=30553.0