Install a client library and make your first API calls. This guide assumes you already have a running gRPC server.
Client Installation
Quick Start Examples
Sending Commands
Commands follow a consistent pattern across all languages:
- Create a
LinuxCNCCommandmessage - Set the specific command field (state, mode, mdi, jog, etc.)
- Call
SendCommand - Optionally call
WaitCompleteto wait for execution
Two things to know before you rely on the responses:
RCS_DONEfromSendCommandmeans the command was dispatched, not that it finished. The response’slinuxcnc_serialidentifies the command to LinuxCNC; pass it toWaitCompleteto wait on that command’s serial. The answer is exact as long as nothing else is sent to LinuxCNC meanwhile: once a later command has been echoed, from any client, it isRCS_DONEand does not say how the earlier command ended (see Command and Error Semantics).- The server does not switch modes for you. See Modes and teleop: what to send first.
Example: MDI Command (Python)
import time
from linuxcnc_pb import linuxcnc_pb2, linuxcnc_pb2_grpc
# Ensure machine is on and in MDI mode first
def send_mdi(stub, gcode):
cmd = linuxcnc_pb2.LinuxCNCCommand()
cmd.serial = 1
cmd.timestamp = int(time.time() * 1e9)
cmd.mdi.command = gcode
response = stub.SendCommand(cmd)
if response.status != linuxcnc_pb2.RCS_DONE:
print(f"Error: {response.error_message}")
return False
# Wait on this command's serial
wait_req = linuxcnc_pb2.WaitCompleteRequest(timeout=30.0)
if response.HasField("linuxcnc_serial"):
wait_req.linuxcnc_serial = response.linuxcnc_serial
else:
print("Note: no linuxcnc_serial; waiting on the latest command sent by any client")
response = stub.WaitComplete(wait_req)
# RCS_EXEC here means the wait timed out and the command is still running
return response.status == linuxcnc_pb2.RCS_DONE
# Usage
send_mdi(stub, "G0 X10 Y10")Example: Jogging (Python)
def jog_axis(stub, axis, velocity):
"""Start continuous jog on an axis."""
cmd = linuxcnc_pb2.LinuxCNCCommand()
cmd.serial = 1
cmd.timestamp = int(time.time() * 1e9)
cmd.jog.type = linuxcnc_pb2.JOG_CONTINUOUS
cmd.jog.is_joint = False # Axis mode
cmd.jog.index = axis # 0=X, 1=Y, 2=Z
cmd.jog.velocity = velocity
return stub.SendCommand(cmd)
def jog_stop(stub, axis):
"""Stop jogging an axis."""
cmd = linuxcnc_pb2.LinuxCNCCommand()
cmd.serial = 2
cmd.timestamp = int(time.time() * 1e9)
cmd.jog.type = linuxcnc_pb2.JOG_STOP
cmd.jog.is_joint = False
cmd.jog.index = axis
return stub.SendCommand(cmd)Streaming Status
Instead of polling, subscribe to real-time updates:
HAL Queries
Query HAL pins, signals, and parameters:
Python
from linuxcnc_pb import hal_pb2, hal_pb2_grpc
hal_stub = hal_pb2_grpc.HalServiceStub(channel)
# Query all pins matching a pattern
request = hal_pb2.QueryPinsCommand(pattern="axis.*position*")
response = hal_stub.QueryPins(request)
for pin in response.pins:
print(f"{pin.name}: {pin.value}")Machine Configuration
GetMachineConfig tells a client what kind of machine it is talking to:
units, limits, and which joint drives which axis.
config = stub.GetMachineConfig(linuxcnc_pb2.GetMachineConfigRequest())
print(config.machine_name, config.linear_units_name)
for joint in config.joints:
print(joint.joint_number, joint.axis_letter, joint.min_limit, joint.max_limit)See GetMachineConfig.
Server Version
GetServerInfo returns the server’s version and whether it is connected to
LinuxCNC. It works while LinuxCNC is not running, when most other RPCs answer
UNAVAILABLE. A 1.0 server does not have it and answers UNIMPLEMENTED.
server_info = stub.GetServerInfo(linuxcnc_pb2.GetServerInfoRequest())
print(server_info.server_version, server_info.linuxcnc_connected)See GetServerInfo.
Files
UploadFile, ListFiles, ReadFile and DeleteFile manage G-code files in
the server’s NC files directory, and ResolveFile tells you whether a name is
the program LinuxCNC has loaded. See
File Management.
Error Handling
All gRPC calls can raise grpc.RpcError. Common status codes:
| Code | Meaning |
|---|---|
UNAVAILABLE | Server not reachable, or the server is running but LinuxCNC is not (it connects by itself when LinuxCNC starts; GetServerInfo tells the two apart), LinuxCNC’s status cannot be read right now, or the server is stopping after its LinuxCNC went away. The linuxcnc-grpc-reason trailing metadata says which (see the API reference) |
UNIMPLEMENTED | The server is older than the RPC, for example GetServerInfo on a 1.0 server |
INVALID_ARGUMENT | A file RPC was given a bad argument: a file name outside the NC files directory, an empty name, empty or too large upload content |
NOT_FOUND | A file that does not exist |
FAILED_PRECONDITION | StreamErrors on a server started with --error-channel off, or GetMachineConfig before the server has read the INI file (ini-not-read-in-cycle) |
INTERNAL | Server-side error |
A rejected command is not a gRPC error. If the server rejects it before
sending (a joint or spindle index out of range, an empty MDI command, an
unknown command), SendCommand returns a normal CommandResponse with
RCS_ERROR and the reason in error_message. RCS_ERROR can also follow a
command that was partly sent (see
RCS_ERROR from SendCommand).
If LinuxCNC rejects it afterwards, WaitComplete returns RCS_ERROR or
LinuxCNC reports the problem as an error message (see StreamErrors). So
check response.status as well as catching grpc.RpcError.
To check whether the server is connected to LinuxCNC without calling it, use
the standard gRPC health service: "" and linuxcnc.LinuxCNCService report
NOT_SERVING until LinuxCNC is up.
try:
status = stub.GetStatus(linuxcnc_pb2.GetStatusRequest())
except grpc.RpcError as e:
print(f"Error: {e.code()}: {e.details()}")Next Steps
- Server Configuration - Auto-start, TLS, and advanced setup
- API Reference - Complete API documentation
- Examples Guide - Detailed example walkthroughs