Mach Modules Documentation
Lua Module API Documentation
Loading...
Searching...
No Matches
ModbusModule.lua File Reference

Go to the source code of this file.

Functions

ModbusModule RunSequenceWait (sequence, user_message, device)
 Run a Modbus PLC sequence and wait for it to complete.
ModbusModule SetSeqData (Device, Index, Val)
 Send a data value to a PLC sequence data register on a Modbus device.
ModbusModule GetSeqData (Index)
 Get sequence data from a Modbus PLC device by index.
ModbusModule SeqCall (Device, PrintToStatus,...)
 Call and wait for PLC sequences to start on a Modbus device.
ModbusModule SeqCallWrapper (Device, seq_numbers)
 Protected wrapper for starting PLC sequences with error recovery.
ModbusModule IsSeqRunning (Device, seq_number)
 Check if a specific PLC sequence is currently running.
ModbusModule SeqWait (Device, PrintToStatus,...)
 Wait for specified PLC sequences to complete on a device.
ModbusModule SeqWaitWrapper (Device, seq_numbers)
 Protected wrapper for the internal sequence wait implementation.
ModbusModule SeqAllWait (Device, PrintToStatus)
 Wait for all 64 PLC sequences on a device to complete.
ModbusModule GetBit (Device, BitWanted, Reg)
 Get the boolean state of a specific bit in a Modbus register.
ModbusModule SetBit (Device, BitWanted, Reg)
 Set a specific bit in a Modbus register for a device.
ModbusModule ClearBit (Device, BitWanted, Reg)
 Clear a specific bit in a Modbus register for a device.
ModbusModule ByteSwap (Val)
 Swap the high and low bytes of a 16-bit integer value.
ModbusModule ClearModbus (Device)
 Clear all Modbus ContSeqCall registers for a device.
ModbusModule GetSeqDescription (PLCDevice, Seq)
 Get the description string for a specific PLC sequence.
ModbusModule GetSeqTimeoutStatus (PLCDevice, Seq)
 Get the timeout status of a specific PLC sequence.
ModbusModule GetAllSeqTimeoutStatus (PLCDevice)
 Get the timeout status of all 64 PLC sequences for a device.
ModbusModule StartFunction (device, func)
 Start a Modbus function on a device and wait for acknowledgement.
ModbusModule StopFunction (device, func)
 Stop a running Modbus function on a device.

Function Documentation

◆ ByteSwap()

ModbusModule ByteSwap ( Val )

Swap the high and low bytes of a 16-bit integer value.

[[---------—BYTE SWAP-----------------—

Extracts the low byte (bits 0-7) and high byte (bits 8-15) of the input value, then recombines them with their positions swapped. Used for correcting byte-order differences between Mach4 and Modbus devices that use different byte ordering conventions.

Parameters
Val(number) The 16-bit integer value to byte-swap. Must be in range -65535 to 65535
Returns
(number|nil) The byte-swapped value, or nil on error
(boolean) Success flag - true if the swap was performed successfully
(string) Status message describing the outcome
Note
Documentation generated by AI on 2026-03-03

◆ ClearBit()

ModbusModule ClearBit ( Device ,
BitWanted ,
Reg  )

Clear a specific bit in a Modbus register for a device.

[[---------—CLEAR BITS-----------------—

Reads the current value of the register word containing the target bit, optionally byte-swaps it if the device is configured for byte swapping, clears the specified bit using bitwise AND with the complement mask, reverses the byte swap if applied, then writes the result back to the register.

Parameters
Device(number) The Modbus device index (0-7)
BitWanted(number) The bit number to clear (0-1024)
Reg(string) The base register name (e.g., "ContSeqCall", "PLCSeqFB")
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if the bit was cleared successfully
(string) Status message describing the outcome
Note
Documentation generated by AI on 2026-03-03

◆ ClearModbus()

ModbusModule ClearModbus ( Device )

Clear all Modbus ContSeqCall registers for a device.

[[---------—CLEAR MODBUS-----------------—

Resets the ContSeqCall registers (indices 0-3) to 0 for the specified Modbus device. This is typically called before reporting a sequence error to ensure no stale call bits remain active in the PLC communication registers.

Parameters
Device(number) The Modbus device index to clear (0-7)
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if all registers were cleared successfully
(string) Status message describing the outcome
Warning
Clearing Modbus registers while a sequence is actively running may cause PLC communication issues. This function should only be called during error recovery.
Note
Documentation generated by AI on 2026-03-03

◆ GetAllSeqTimeoutStatus()

ModbusModule GetAllSeqTimeoutStatus ( PLCDevice )

Get the timeout status of all 64 PLC sequences for a device.

Iterates through all 64 possible sequence numbers (0-63) and checks each one for a timeout condition using ModbusModule.GetSeqTimeoutStatus(). Returns a table summarizing which sequences have timed out, a combined timeout message, and an overall timeout flag.

Parameters
PLCDevice(number|nil) The PLC device index (0-7). If nil, defaults to 0
Returns
(table) Result table with keys:
  • TimedOutSequences (table): Array of sequence numbers that have timed out
  • TimedOutMessage (string): Human-readable message listing all timed-out sequences
  • TimeoutStatus (boolean): True if any sequence has timed out, false otherwise
(boolean) Success flag - true if operation completed successfully
(string) Status message describing the outcome
Note
Documentation generated by AI on 2026-03-03

◆ GetBit()

ModbusModule GetBit ( Device ,
BitWanted ,
Reg  )

Get the boolean state of a specific bit in a Modbus register.

[[---------—GET BIT-----------------—

Reads the register word that contains the target bit for the given device, optionally byte-swaps the value if the device is configured for byte swapping, then extracts and returns the boolean state of the specified bit using bitwise operations. The word index is calculated as floor(BitWanted / 16) and the bit position within the word is BitWanted mod 16.

Parameters
Device(number) The Modbus device index (0-7)
BitWanted(number) The bit number to read (0-1024)
Reg(string) The base register name (e.g., "ContSeqCall", "PLCSeqFB")
Returns
(boolean|nil) True if the bit is set, false if clear, or nil on error
(boolean) Success flag - true if the bit was read successfully
(string) Status message describing the bit state or error
Note
Documentation generated by AI on 2026-03-03

◆ GetSeqData()

ModbusModule GetSeqData ( Index )

Get sequence data from a Modbus PLC device by index.

Retrieves data from the PLC sequence data register at the specified index. This function is currently commented out in the codebase and represents legacy functionality for bidirectional sequence data exchange with the PLC.

Parameters
Index(number) The data index to retrieve from the PLC sequence data registers (0-15)
Returns
(nil) No meaningful primary return value
(boolean) Success flag
(string) Status message
Note
This function body is commented out and non-functional in the current codebase
Documentation generated by AI on 2026-03-03

◆ GetSeqDescription()

ModbusModule GetSeqDescription ( PLCDevice ,
Seq  )

Get the description string for a specific PLC sequence.

Reads the timeout message/description for the given sequence from the Mach4 profile under the MachMotionModbusDevicePLCConfiguration section. This description is used in alarm messages to identify which sequence timed out or failed.

Parameters
PLCDevice(number|nil) The PLC device index (0-7). If nil, defaults to 0
Seq(number) The sequence number to retrieve the description for (0-63)
Returns
(string|nil) The description string for the sequence, or nil on error
(boolean) Success flag - true if operation completed successfully
(string) Status message describing the outcome
Note
Documentation generated by AI on 2026-03-03

◆ GetSeqTimeoutStatus()

ModbusModule GetSeqTimeoutStatus ( PLCDevice ,
Seq  )

Get the timeout status of a specific PLC sequence.

Checks the PLCSeqTimeOut bit for the specified sequence number on the given PLC device. Returns true if the sequence has timed out, false if it completed normally.

Parameters
PLCDevice(number|nil) The PLC device index (0-7). If nil, defaults to 0
Seq(number) The sequence number to check (0-63)
Returns
(boolean|nil) True if the sequence has timed out, false if not, or nil on error
(boolean) Success flag - true if operation completed successfully
(string) Status message describing the outcome
Note
Documentation generated by AI on 2026-03-03

◆ IsSeqRunning()

ModbusModule IsSeqRunning ( Device ,
seq_number  )

Check if a specific PLC sequence is currently running.

Reads the PLCSeqFB bit for the given sequence number on the specified device and returns true if the feedback bit is set, indicating the sequence is active.

Parameters
Device(number) The Modbus device index (0-7)
seq_number(number) The sequence number to check (0-63)
Returns
(boolean) True if the sequence is currently running, false otherwise
Note
Documentation generated by AI on 2026-03-03

◆ RunSequenceWait()

ModbusModule RunSequenceWait ( sequence ,
user_message ,
device  )

Run a Modbus PLC sequence and wait for it to complete.

Calls mb.SeqCallWrapper() to start the specified sequence on the given device, then waits for completion. In a screen chunk context, displays a please-wait dialog that polls mb.IsSeqRunning() until the sequence finishes. In an M-code chunk context, calls mb.SeqWaitWrapper() to block until completion. If device is not provided, defaults to device 0.

Parameters
sequence(number) The sequence number to run
user_message(string|nil) Message to display in the please-wait dialog. If nil, defaults to "PLC Sequence Running..."
device(number|nil) The Modbus device index. If nil, defaults to 0
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if the sequence completed successfully
(string) Status message describing the outcome

Behavior differs based on execution context:

  • Screen chunk: Shows a non-blocking please-wait dialog while polling sequence state
  • M-code chunk: Blocks execution until the sequence completes
    Note
    Documentation generated by AI on 2026-03-03

◆ SeqAllWait()

ModbusModule SeqAllWait ( Device ,
PrintToStatus  )

Wait for all 64 PLC sequences on a device to complete.

[[---------—SEQUENCE ALL WAIT-----------------—

Builds a stack of all sequence numbers (0-63) and polls each one's PLCSeqFB bit until all sequences report as not running. If the machine is stopped or disabled during the wait, the function checks for timeout conditions and returns an appropriate error message. Optionally logs progress to the operator log when sequences are still pending.

Parameters
Device(number) The Modbus device index (0-7)
PrintToStatus(boolean) If true, logs waiting and completion messages to the operator log
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if all sequences completed, false on stop/disable/timeout
(string) Status message describing the outcome
Warning
This function blocks execution while polling. If the machine is stopped or disabled, the function will exit early and return an error rather than hanging indefinitely.
Note
Documentation generated by AI on 2026-03-03

◆ SeqCall()

ModbusModule SeqCall ( Device ,
PrintToStatus ,
... )

Call and wait for PLC sequences to start on a Modbus device.

[[---------—SEQUENCE CALL-----------------—

Validates the device number, PLC enabled status, and machine enabled state before proceeding. Builds an argument stack from variadic inputs (accepts individual numbers or tables), verifies that no requested sequences are already running or have their call bits active, then sets the ContSeqCall bits for all requested sequences. Polls until all sequences acknowledge via their PLCSeqFB bits or a 500ms timeout occurs. Checks machine stop status and PLC timeout status throughout. Optionally logs progress to the operator log.

Parameters
Device(number) The Modbus device index (0-7)
PrintToStatus(boolean) If true, logs waiting and completion messages to the operator log
...(number|table) Sequence numbers to call, as individual numbers or tables of numbers
Returns
(nil) No meaningful primary return value on success
(boolean) Success flag - true if all sequences started, false on error/stop/timeout
(string) Status message describing the outcome
Warning
This function blocks execution while waiting for PLC acknowledgement. The machine must be enabled before calling. If the machine is stopped or disabled during the wait, the function exits early with an error message.
Note
Documentation generated by AI on 2026-03-03

◆ SeqCallWrapper()

ModbusModule SeqCallWrapper ( Device ,
seq_numbers  )

Protected wrapper for starting PLC sequences with error recovery.

Verifies machine stop status before proceeding, then calls mb._SeqCallWrapper() inside a w.pcall() protected call. If the call fails, clears all Modbus call bits via ModbusModule.ClearModbus(), re-checks the stop status, and raises an appropriate alarm. Table-type errors are raised as alarm messages; string errors are logged via w.FunctionError().

Parameters
Device(number) The Modbus device index (0-7)
seq_numbers(number|table) A single sequence number or table of sequence numbers to call
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if sequences were started successfully
(string) Status message describing the outcome
Warning
If sequences fail to start, this function calls ModbusModule.ClearModbus() to reset all call bits before reporting the error, which will affect any other sequences waiting on the device.
See also
ModbusModule._SeqCallWrapper() for the internal implementation
Note
Documentation generated by AI on 2026-03-03

◆ SeqWait()

ModbusModule SeqWait ( Device ,
PrintToStatus ,
... )

Wait for specified PLC sequences to complete on a device.

[[---------—SEQUENCE WAIT-----------------—

Builds a stack from the variadic sequence number arguments (accepts individual numbers or tables of numbers) and polls each sequence's PLCSeqFB bit until all report as not running. Validates device number and PLC enabled status before waiting. Checks machine stop status and PLC timeout status during the wait loop. On completion, verifies no sequences are in a timed-out state before returning success.

Parameters
Device(number) The Modbus device index (0-7)
PrintToStatus(boolean) If true, logs waiting and completion messages to the operator log
...(number|table) Sequence numbers to wait for, as individual numbers or tables of numbers
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if all sequences completed, false on stop/disable/timeout
(string) Status message describing the outcome
Warning
This function blocks execution while polling sequence completion. It will exit early if the machine is stopped, disabled, or a PLC timeout is detected.
Note
Documentation generated by AI on 2026-03-03

◆ SeqWaitWrapper()

ModbusModule SeqWaitWrapper ( Device ,
seq_numbers  )

Protected wrapper for the internal sequence wait implementation.

Calls mb._SeqWaitWrapper() inside a w.pcall() protected call. If the call fails, checks the machine stop status - if stopped, returns the error directly; otherwise raises an alarm message for table-type errors or logs via w.FunctionError() for string errors.

Parameters
Device(number) The Modbus device index (0-7)
seq_numbers(number|table) A single sequence number or table of sequence numbers to wait for. A value less than 1 triggers "wait all" mode.
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if all sequences completed successfully
(string) Status message describing the outcome
See also
ModbusModule._SeqWaitWrapper() for the internal implementation
Note
Documentation generated by AI on 2026-03-03

◆ SetBit()

ModbusModule SetBit ( Device ,
BitWanted ,
Reg  )

Set a specific bit in a Modbus register for a device.

[[---------—SET BITS-----------------—

Reads the current value of the register word containing the target bit, optionally byte-swaps it if the device is configured for byte swapping, sets the specified bit using bitwise OR, reverses the byte swap if applied, then writes the result back to the register.

Parameters
Device(number) The Modbus device index (0-7)
BitWanted(number) The bit number to set (0-1024)
Reg(string) The base register name (e.g., "ContSeqCall", "PLCSeqFB")
Returns
(nil) No meaningful primary return value
(boolean) Success flag - true if the bit was set successfully
(string) Status message describing the outcome
Note
Documentation generated by AI on 2026-03-03

◆ SetSeqData()

ModbusModule SetSeqData ( Device ,
Index ,
Val  )

Send a data value to a PLC sequence data register on a Modbus device.

[[---------—SEND DATA-----------------—

Validates all inputs, checks that the PLC device is enabled, then writes the specified value to the ContSeqData register at the given index for the named device. The active handshaking protocol (setting/clearing ContSeqBitData and waiting for PLCSeqBitData) is currently patched out - the function writes the value directly to the register without the full handshake sequence.

Parameters
Device(number) The Modbus device index (0-7)
Index(number) The data register index to write to (0-14)
Val(number) The value to write to the register (-32767 to 32767)
Returns
(number|nil) The value that was written, or nil on error
(boolean) Success flag - true if data was sent successfully
(string) Status message describing the outcome
Note
The full PLC handshaking protocol is currently disabled via a patch; data is written directly to the register without waiting for PLC acknowledgement
Documentation generated by AI on 2026-03-03

◆ StartFunction()

ModbusModule StartFunction ( device ,
func  )

Start a Modbus function on a device and wait for acknowledgement.

Sends a start command to the Modbus control register for the specified device and function, then polls the function's return code register until it reaches 0 (indicating the function started successfully) or a 1000ms timeout expires. If the function does not start within the timeout, an error is raised via w.Error().

Parameters
device(string) The device name to send the start command to
func(string) The function name to start on the device
Warning
This function blocks execution for up to 1000ms while polling for start confirmation. If the function does not start in time, w.Error() is raised and execution does not continue normally.
Note
Documentation generated by AI on 2026-03-03

References func().

◆ StopFunction()

ModbusModule StopFunction ( device ,
func  )

Stop a running Modbus function on a device.

Sends a stop command to the Modbus control register for the specified device and function. This is a fire-and-forget operation with no confirmation that the function actually stopped.

Parameters
device(string) The device name to send the stop command to
func(string) The function name to stop on the device
Note
Documentation generated by AI on 2026-03-03

References func().