Skip to main content

Lua API Reference

Complete API reference for the three global objects available in Lua test scripts.


session — Connection & Messaging

Connection

FunctionParametersDescription
Connect()Connect using toolbar Host/Port/Variant
Connect(host, port, variantId)string, number, stringConnect to a specific controller
Disconnect()Hard TCP close
DisconnectWithMid3()Send MID 0003 then close TCP
Sleep(ms)numberPause execution (max 60,000ms)

Variant IDs: "OpenProtocol_Rexroth", "OpenProtocol_BMW", "OpenProtocol_Ford"

Handshake

FunctionParametersDescription
Handshake()Send MID 0001, wait for MID 0002 (10s timeout)
Handshake(timeoutMs)numberHandshake with custom timeout

Sending

FunctionParametersDescription
SendMid(midNumber)numberSend MID with empty body
SendMid(midNumber, fields)number, tableSend MID with body fields
SendMid(midNumber, fields, headerOverrides)number, table?, tableSend MID with body fields and per-call header overrides
SendRaw(payload)stringSend raw ASCII (NUL appended automatically)

Header Control

Control header fields sent with SendMid(). By default, SendMid uses revision=1 and zeros for all other fields.

FunctionParametersDescription
SetHeader(fields)tableSet persistent header defaults for all subsequent SendMid calls
SetHeader(nil)Clear all persistent header defaults
GetHeader()Returns table with current persistent header defaults
ClearHeader()Clear all persistent header defaults

Controllable fields (all optional):

FieldTypeDefaultDescription
revisionnumber1MID revision number
stationstring"00"Station ID
spindlestring"00"Spindle ID
sequencestring"00"Message sequence number
noAcknumber0No-acknowledge flag (0 or 1)
partCountstring"0"Part count
partNumberstring"0"Part number

Precedence: per-call overrides > persistent defaults > built-in defaults.

Example:

-- Set defaults once
session:SetHeader({ revision = 3, station = "01", spindle = "02" })

-- All SendMid calls now use rev 3, station 01, spindle 02
session:SendMid(10)
session:SendMid(60, { torqueMin = "100" })

-- Override revision for just this call (station/spindle still from SetHeader)
session:SendMid(8, nil, { revision = 999 })

-- Clear when done
session:ClearHeader()

Receiving

FunctionParametersReturnsDescription
WaitForMid(mid, timeoutMs)number, numbertableWait for specific MID (throws on timeout)
WaitForRawResponse(timeoutMs)numbertableWait for any message (throws on timeout)
TryWaitForRawResponse(timeoutMs)numbertable|nilWait for any message (nil on timeout)

High-Level Helpers

FunctionParametersDescription
SelectJob(jobId)numberSend MID 0038, wait for accept/reject (10s)
StartTightening()Subscribe to MID 0060 via MID 0008
StopTightening()Unsubscribe from MID 0060 via MID 0009
WaitForResult(timeoutMs)numberWait for MID 0061 tightening result

Network Simulation

FunctionParametersDescription
SetNetworkProfile(name)stringApply built-in profile (must call before Connect)
SetNetworkProfileCustom(config)tableApply custom profile (must call before Connect)

Built-in profiles: "normal", "poor_radio", "slow_station", "disconnect_after_3"

Custom config fields:

FieldTypeDefaultDescription
sendDelayMsnumber0Delay per send (ms)
receiveDelayMsnumber0Delay per receive (ms)
jitterMsnumber0Random ± jitter (ms)
packetLossPercentnumber0Drop probability 0–100
disconnectAfterMessagesnumber0Auto-disconnect after N messages

Properties

PropertyTypeDescription
IsConnectedbooleanWhether TCP connection is active
ToolHoststringToolbar host value
ToolPortnumberToolbar port value
ToolVariantIdstringToolbar variant value

Response Table Structure

Every received message returns a table with:

FieldTypeDescription
midnumberMID number
revisionnumberRevision number
lengthnumberTotal message length (bytes)
rawstringFull raw ASCII message
bodystringBody after 20-byte header
parseErrorstring/nilError if parsing failed
headertableParsed header (see below)
fieldstableParsed body fields (all values are strings)

header Sub-Table

FieldTypeDescription
MidnumberMID number
RevisionnumberRevision
LengthnumberTotal length
NoAckFlagnumberNo-ack flag (0 or 1)
StationstringStation ID
SpindlestringSpindle ID
SequencestringSequence number
PartCountstringNumber of message parts
PartNumberstringCurrent part number

assert — Assertions

All assertions accept an optional message as the last parameter.

General

FunctionParametersDescription
IsTrue(cond, msg?)boolean, string?Assert condition is true
IsFalse(cond, msg?)boolean, string?Assert condition is false
Equals(expected, actual, msg?)any, any, string?Assert values are equal
NotEquals(expected, actual, msg?)any, any, string?Assert values differ
NotNil(value, msg?)any, string?Assert value is not nil
Fail(message)stringUnconditionally fail

Tightening-Specific

FunctionParametersDescription
TorqueWithin(result, min, max)table, number, numberAssert torque in [min, max]
AngleWithin(result, min, max)table, number, numberAssert angle in [min, max]
NoAlarm(result)tableAssert no error code
MidFieldEquals(result, field, expected)table, string, anyAssert field equals value

log — Logging & Steps

FunctionParametersDescription
Step(label)stringMark a logical test step
Info(message)stringLog informational message
Warn(message)stringLog warning
Error(message)stringLog error (marks step as Failed)
Debug(message)stringLog debug message
Break(label?)string?Pause for manual inspection (5 min auto-resume)

Global Functions

FunctionReturnsDescription
createSession()ScriptSessionCreate an independent TCP session
print(...)Redirected to log:Debug()