Sending commands and waiting
This page says what the answers of SendCommand and WaitComplete mean. Their fields are
in the API reference.
The usual sequence:
- Set the mode or teleop the command needs and wait for it (Sequencing).
- Send the command and check
status. OnRCS_ERROR,not_sentsays whether a retry is safe. - Check the response’s
connection_id, then callWaitCompletewith itslinuxcnc_serial(one command). - Confirm the result from a fresh status (What the answer means).
SendCommand answers when the command is dispatched
RCS_DONE from SendCommand means “dispatched”, not “finished”. An MDI move or a homing
sequence is usually still running when the response arrives. To know when it has finished,
call WaitComplete and confirm from the status.
A stop is answered the same way, once its LinuxCNC call has returned. It may first have waited for the message being written ahead of it (see Stops).
RCS_ERROR from SendCommand
A command the server refuses, or that fails while it is sent, is answered RCS_ERROR with
the reason in error_message, not with a gRPC error. The gRPC status codes, such as
UNAVAILABLE while the server is not connected to LinuxCNC, are in Errors.
| Cause | error_message | not_sent |
|---|---|---|
| The server rejected a value. | The server’s own text, for example MDI command must not be empty. | True |
| A LinuxCNC call failed. | The text of LinuxCNC’s error. | True if LinuxCNC took none of the command’s messages, otherwise false |
| The write to LinuxCNC’s command channel failed. | The command was not accepted by LinuxCNC: the write to its command channel failed, so nothing was sent | True |
| The command helper process was lost with the command. | The command helper process stopped while it had the command; the command may have been sent to LinuxCNC. Check the status before sending it again | False |
| No command helper could be started for the command. | The start error. | True |
| The connection or the helper kept changing while the command waited. | The command was not sent: the connection to LinuxCNC or its command helper changed 3 times while the command waited for its turn | True |
| A stop arrived while the command waited, or LinuxCNC has not taken a stop yet. | The texts are in Stops. | True, or false when a stop cut a command sent as several messages |
| LinuxCNC was found gone while the command waited. | The LinuxCNC this server was attached to has shut down or been restarted; the server is stopping so that a new server process can connect | True, or false between two messages of one command |
| No command is set in the request. | Unknown command type: None | True |
| Too many commands are waiting. | Too many commands are waiting; the command was not sent (see Thread pool). | True |
| The server is shutting down. | The server is shutting down; the command was not sent | True |
| The client went away before the command’s turn. | The request was cancelled before its turn; the command was not sent | True |
The server’s checks (Values the server rejects) all run before the first message is sent, so a rejected command is never partly sent. Stops are not refused for shutdown, a gone client or a waiting queue.
Commands sent as several messages
Most commands are one LinuxCNC message. Four are sent as several, one after the other:
| Command | Messages, in order |
|---|---|
coolant | Mist, flood |
set_limit | Minimum limit, maximum limit |
override_config | Feed override, spindle override, feed hold, adaptive feed |
program_options | Optional stop, block delete |
If one message fails, a stop arrives between two of them, or the command helper is lost
between two of them, the earlier messages have been sent and stay in effect and the later
ones are not sent. The answer is RCS_ERROR with not_sent false, and it does not say how
many messages went out. Read the status before you send the command again.
If LinuxCNC takes none of the messages, the answer is RCS_ERROR and nothing was sent. If
it takes some and silently drops another, the answer is RCS_DONE. The same holds for
opening a program, which LinuxCNC itself sends as two messages (close, then open).
not_sent says when a retry is safe
With RCS_ERROR, not_sent true means the server is certain that nothing was written to
LinuxCNC for the request, so sending it again cannot run it twice. False means the server
cannot rule out that something was written: check the status first. A 1.0 server never sets
the field, so from one, false says nothing.
acknowledged says whether the task took the command
After a send returns, the server reads the status once and reports what it saw in
CommandResponse.acknowledged. It does not change status.
- True: the task had echoed the command’s serial. It has taken the command and may still be executing it.
- False: the command was written to LinuxCNC’s queue, but the task had not taken it after LinuxCNC’s wait of about 5 seconds per message. The task is not answering. The command is not withdrawn: it runs when the task runs again, unless LinuxCNC is shut down or restarted first. Read the status before you rely on it or send it again.
- Unset: the server cannot tell. Nothing was dispatched, the status could not be read after the send, the server reconnected in between, the status belongs to another LinuxCNC, or the answer is from a 1.0 server. A stop never waits for a status read, so a stop’s answer can also leave it unset.
For a command sent as several messages it describes the last one. See A LinuxCNC task that does not respond.
The response serial is the server’s, not LinuxCNC’s
CommandResponse.serial is the serial you set on the request or, if you left it at 0, a
number from the server’s own counter. It is a label for your own bookkeeping. Never compare
it with status.task.echo_serial_number, and WaitComplete ignores it. The counter wraps
to 1 after 2147483647.
linuxcnc_serial identifies the command to LinuxCNC
CommandResponse.linuxcnc_serial is LinuxCNC’s own serial number for the command. It is
set whenever the command was dispatched and unset whenever the answer is RCS_ERROR, also
when part of the command was sent. An unset field reads as 0, so test it (HasField in
Python) before you copy it.
- It identifies the command; it does not prove the task received it. The serial is returned
even when the task did not answer (
acknowledgedfalse). - It says nothing about the outcome. LinuxCNC can still refuse the command, an MDI line in
the wrong mode for example. Pass the value to
WaitCompleteto getRCS_DONEorRCS_ERRORfor it. - For a command sent as several messages it is the serial of the last message LinuxCNC took, so a wait on it covers that message only.
- It is valid for one run of LinuxCNC. Compare serials only between answers with the same,
non-empty
connection_id(see Telling servers and connections apart). - It can be set while
acknowledgedis unset, when the server’s status turned out to belong to another LinuxCNC than the one that took the command. The command was sent, and the server stops (see When the server finds its LinuxCNC gone).
What LinuxCNC’s serial tells you
- The serial advances by the number of messages a command is sent as: 1 for most commands,
2 for
coolant,set_limit,program_optionsand opening a program, 4 foroverride_config. Don’t predict a serial from the previous one; use the value returned. - An echo that has reached the serial means the task has read the command, not that its effect is visible yet. Read the status field you care about until it changes.
- An error is not always counted against the command that caused it. When two clients write
commands within 1 to 2 ms of each other, a move refused for a soft limit can show up
against the neighboring command’s serial. A wait on a
linuxcnc_serialcan then answerRCS_ERRORfor a good command orRCS_DONEfor the refused one. Interpreter errors, such as a bad G-code line, were not seen to do this. Read the error messages (StreamErrors orstatus.errors).
WaitComplete
WaitComplete waits for a command sent through this server to finish. The request’s
serial is ignored and only echoed back. The request and response tables and a full example
are in the API reference.
Without linuxcnc_serial: the latest command
The wait is on the most recent command sent through this server by any client. After
sending A and then B, it reports on B whichever serial you pass.
- The target is fixed when the call arrives. A command sent during the wait does not change it.
- A command still being sent when the call arrives is the latest one. The wait answers
RCS_EXECat its timeout while that send lasts. - If nothing has been sent through this server since it connected to LinuxCNC, the answer
is
RCS_DONEat once, without a status read and whatever the machine is doing. The same holds after a reconnect, except while a stop holds the other commands (see When the outcome is not known).
With linuxcnc_serial: one command
Set linuxcnc_serial in the request to the value SendCommand returned. The wait then
applies LinuxCNC’s own serial rule to that command.
- The value must be among the last 4096 serials the server recorded on its current
connection to LinuxCNC. Any other value is answered
RCS_ERRORat once (linuxcnc_serial N was not issued by this server on its current connection to LinuxCNC; the outcome of that command is unknown). An explicit 0 is checked like any other value. A command that failed after part of it was sent is recorded too, although its response has no serial. - Only the number is checked, and a restarted LinuxCNC counts from the start again. Before
the wait, check that the command’s
connection_idis not empty and equalsServerInfo.connection_id. - The wait is not held up by another command being sent. After
acknowledgedfalse it waits for the task to take the command.
What the answer means
| Status | Meaning |
|---|---|
RCS_DONE | LinuxCNC has finished the command or echoed a later one, or nothing was sent through this server. |
RCS_ERROR | The command failed (Command failed), or the server cannot tell and error_message says why. |
RCS_EXEC | The wait ended before the server saw the command finish. |
While the task has echoed the command’s serial and still reports RCS_EXEC, the wait goes
on. RCS_DONE does not prove that your command finished. Once LinuxCNC has echoed a later
command, the answer is RCS_DONE at once, whatever became of the earlier one. The later
command can come from this server, from the GUI or from any other client: a feed override
during a long move, an E-stop or an abort. So after a move, confirm it with a fresh status:
task.task_state still STATE_ON, task.interp_state at INTERP_IDLE, trajectory.inpos
true and trajectory.current_vel zero. After an abort only the position, short of the
target, shows that the move was cut short.
RCS_EXEC is not proof that the command is still running: the wait timed out or the client
went away, and the status may not have been readable. While
a stopped task holds the status,
a wait keeps its command and answers from the first status the server can read again, or
RCS_EXEC at its timeout.
Timeout
A timeout of 0 means the default, 5 seconds. It is the server’s wait, not the client’s
gRPC deadline: set the deadline longer, or the call ends with DEADLINE_EXCEEDED before
the server can answer RCS_EXEC. The timeout covers the whole wait, including any time the
call waits for other RPCs that are reading the status. A status read already under way when
it runs out is finished first, so the call can overrun by about the
status read timeout,
0.1 s by default, or longer when the server then tries a new status channel. With
--status-read-timeout off, or if the server could not set its own read timeout, a read
waits as long as LinuxCNC’s NML file says (10 seconds in the stock file).
When the outcome is not known
A wait without linuxcnc_serial answers RCS_ERROR, and never reports on an earlier
command instead, in these cases:
error_message | When |
|---|---|
The latest command sent through this server was lost with the command helper process; its outcome is not known | The helper was lost with the latest command, a stop included, or the server could not tell what happened to it. |
The latest command's outcome is not known: the status this server reads cannot show it (it may belong to another LinuxCNC) | The server’s status may belong to another LinuxCNC than the one that took the command. |
The connection to LinuxCNC changed; the latest command's outcome is not known | The server reconnected while a stop holds the other commands. |
A wait made afterwards gets the same answer until a later command has been sent and
recorded, or a reconnect finds that LinuxCNC was restarted. A wait with linuxcnc_serial
is not affected.
A reconnect during a wait
Don’t rely on WaitComplete across a reconnect; read the status. A wait with
linuxcnc_serial answers RCS_ERROR (Reconnected to LinuxCNC during the wait; the outcome of command N is unknown). A wait without it switches to the latest command sent since the
reconnect. If there is none it answers RCS_DONE, or RCS_ERROR while a stop
holds the other commands. A reconnect right after the
wait’s last status read goes unnoticed, so the answer can still be RCS_DONE. If the server
finds its LinuxCNC gone during a wait, the answer is RCS_ERROR and says so.
Waiting and stops
A wait polls the status every 50 ms and does not block a stop: an E-stop from another client or thread is sent while the wait is in progress. One of those status reads can still stall the whole server, a stop included, for up to the status read timeout while a stopped task holds the status (see What the server cannot do).
A stop counts as the latest command like any other. To judge one stop, wait on its
linuxcnc_serial when the response has one, after the
connection_id check. If that wait is refused because
the serial was not issued, read the status.
Each wait in progress holds one of the server’s general worker threads. Streams and waits
share a limit, and one more is refused with RESOURCE_EXHAUSTED (see
Thread pool).
Modes and teleop: what to send first
The server never switches the task mode or teleop for you. Every command is passed to
LinuxCNC as it is, and LinuxCNC refuses a command sent in the wrong mode. The table gives
what each kind of command needs on a trivkins machine; the error texts are LinuxCNC’s.
After any motion command, confirm the effect in the status (position, homed,
motion_mode) instead of trusting the response alone.
| Command | Task mode | Teleop (trajectory.motion_mode) | In the wrong state |
|---|---|---|---|
home, unhome | MODE_MANUAL | Off (TRAJ_MODE_FREE) | “must be in joint mode to home” |
jog with is_joint = true | MODE_MANUAL | Off (TRAJ_MODE_FREE) | “Mode is TELEOP, cannot jog joint” |
jog with is_joint = false | MODE_MANUAL | On (TRAJ_MODE_TELEOP) | “Joint jog requested for undefined joint number=-1” |
mdi | MODE_MDI, joints homed | Set by LinuxCNC (TRAJ_MODE_COORD) | “Can’t issue MDI command when not homed”, or a mode error |
program | MODE_AUTO | Set by LinuxCNC | A mode error |
Sequencing
- Send the mode or teleop command, then wait until the status shows it. Call
WaitCompletewith thelinuxcnc_serialfrom the response, or pollGetStatusuntiltask.task_modeortrajectory.motion_modehas the value you asked for. Don’t sleep for a fixed time. - Joints can be jogged before homing; axes cannot. An axis jog needs teleop, which needs the joints homed. To move an unhomed machine, jog joints (GetMachineConfig gives the joint for an axis).
- On a trivkins machine LinuxCNC switches to teleop by itself once every joint is homed.
After homing, a joint jog or a second
homeis refused until you sendteleopwithenable = false. - MDI and auto change the motion mode too, and going back to
MODE_MANUALdoes not always restore teleop. Checktrajectory.motion_modebefore an axis jog, and sendteleopif needed.
def set_mode_and_wait(stub, mode, timeout=5.0):
sent = stub.SendCommand(
linuxcnc_pb2.LinuxCNCCommand(mode=linuxcnc_pb2.ModeCommand(mode=mode)),
timeout=30.0,
)
if sent.status != linuxcnc_pb2.RCS_DONE:
raise RuntimeError(sent.error_message)
request = linuxcnc_pb2.WaitCompleteRequest(timeout=timeout)
if sent.HasField("linuxcnc_serial"):
request.linuxcnc_serial = sent.linuxcnc_serial
stub.WaitComplete(request, timeout=timeout + 5.0) # the gRPC deadline, longer than the wait
status = stub.GetStatus(linuxcnc_pb2.GetStatusRequest(), timeout=5.0)
if status.task.task_mode != mode:
raise RuntimeError("mode change did not take effect")How a refusal shows up
- A jog or home that LinuxCNC refuses still gets
RCS_DONEfromSendCommandand fromWaitComplete. The refusal is only an operator error message (StreamErrors orstatus.errors), and nothing moves or homes. - An MDI command in the wrong mode, or a line the interpreter rejects such as
M999, getsRCS_DONEfromSendCommandandRCS_ERROR(Command failed) fromWaitComplete, unless LinuxCNC has echoed a later command by then, which givesRCS_DONE. The operator error message is the only place that says what was wrong with the line.task.statestaysRCS_ERRORuntil the next command, and the interpreter stays idle.
Telling whether the machine is moving
trajectory.inposstays true during an axis jog. Usetrajectory.current_vel,axes[n].velocityorjoints[n].velocity, which are non-zero while the jog moves.- After an E-stop during a jog,
trajectory.current_velkeeps its last value until the machine is turned on again. A loop that waits for zero velocity must also stop whentask.task_stateis no longerSTATE_ON. task.interp_statealone is not a busy test for MDI: during some MDI moves it readsINTERP_IDLEthroughout, andtask.exec_statecan readEXEC_DONEmid-move.trajectory.inposis false during an MDI move and true at its end.- Treat the machine as idle only when all three agree:
task.interp_stateisINTERP_IDLE,trajectory.inposis true andtrajectory.current_velis zero.