Server setup

Install the server on the LinuxCNC machine, start it and check that it answers. Server configuration describes its options.

Requirements

  • LinuxCNC 2.9 or newer, on a real machine or a simulator configuration (see Requirements).
  • Python 3.10 or newer.
  • SSH access to the machine, if clients run on another computer.

Install

On the LinuxCNC machine, install the server into a virtual environment (venv) that can see LinuxCNC’s Python modules:

sudo apt install python3-venv
python3 -m venv --system-site-packages ~/linuxcnc-grpc-venv
~/linuxcnc-grpc-venv/bin/pip install linuxcnc-grpc

--system-site-packages is required. It lets the venv import the linuxcnc and hal modules that LinuxCNC installs for the system Python; without it the server fails with ModuleNotFoundError: No module named 'linuxcnc'. The package holds both the server and the Python client library.

Debian 12 and 13 refuse a plain pip install linuxcnc-grpc into the system Python with externally-managed-environment, so use the venv or pipx.

With pipx

sudo apt install pipx
pipx install --system-site-packages linuxcnc-grpc

pipx installs the command as ~/.local/bin/linuxcnc-grpc. Use that path wherever these pages run ~/linuxcnc-grpc-venv/bin/linuxcnc-grpc.

Running it by name

To run linuxcnc-grpc without its path, activate the venv (source ~/linuxcnc-grpc-venv/bin/activate) or link the command into a directory on your PATH, with an absolute path:

sudo ln -s /home/your-username/linuxcnc-grpc-venv/bin/linuxcnc-grpc /usr/local/bin/linuxcnc-grpc

Start the server

The server has no authentication and no TLS. Anyone who can reach its port can command the machine and read, upload and delete files in its NC files directory. Bind it to 127.0.0.1 and reach it through an SSH tunnel, or bind it to a LAN address only on a trusted, isolated network. Without --host it listens on all interfaces, so pass --host every time. See Security considerations.

~/linuxcnc-grpc-venv/bin/linuxcnc-grpc --host 127.0.0.1 --port 50051

Start it before or after LinuxCNC. If LinuxCNC is not running yet, the server waits for it and answers UNAVAILABLE on the RPCs that need it (see Starting before LinuxCNC).

Among the startup lines you should see:

2024-01-15 10:30:45 [INFO] linuxcnc_grpc.server: Server configured; binding 127.0.0.1:50051
2024-01-15 10:30:45 [INFO] linuxcnc_grpc.server: Listening on 127.0.0.1:50051

Connect from another computer

--host 127.0.0.1 makes the server listen only on the LinuxCNC machine itself. On the client computer, open an SSH tunnel and leave it running:

ssh -N -L 127.0.0.1:50051:127.0.0.1:50051 user@cnc-machine

Clients then connect to localhost:50051. The tunnel supplies the authentication and encryption the server lacks. A server bound to a LAN address on a trusted network is at LINUXCNC_IP:50051 instead.

Check that it answers

On the LinuxCNC machine, ask the server for its version and whether it has connected to LinuxCNC:

~/linuxcnc-grpc-venv/bin/python -c "
import grpc
from linuxcnc_pb import linuxcnc_pb2, linuxcnc_pb2_grpc

channel = grpc.insecure_channel('localhost:50051')
stub = linuxcnc_pb2_grpc.LinuxCNCServiceStub(channel)
info = stub.GetServerInfo(linuxcnc_pb2.GetServerInfoRequest(), timeout=5)
print(f'Server {info.server_version}, connected to LinuxCNC: {info.linuxcnc_connected}')
channel.close()
"

From another computer, install the client in a venv (python3 -m venv venv && venv/bin/pip install linuxcnc-grpc), open the tunnel and run the same check with venv/bin/python.

Health service

The server answers the standard gRPC health service for "" (the server as a whole) and for its two services:

~/linuxcnc-grpc-venv/bin/python -c "
import grpc
from grpc_health.v1 import health_pb2, health_pb2_grpc

channel = grpc.insecure_channel('localhost:50051')
health = health_pb2_grpc.HealthStub(channel)
for service in ('', 'linuxcnc.LinuxCNCService', 'hal.HalService'):
    reply = health.Check(health_pb2.HealthCheckRequest(service=service), timeout=5)
    print(repr(service), health_pb2.HealthCheckResponse.ServingStatus.Name(reply.status))
channel.close()
"
'' SERVING
'linuxcnc.LinuxCNCService' SERVING
'hal.HalService' SERVING

"" and linuxcnc.LinuxCNCService are NOT_SERVING while the server waits for LinuxCNC. Use Check for probes; Watch is limited to a few clients at a time (see Health service). The server does not offer gRPC reflection, so grpcurl ... list does not work.

Port check

nc -zv localhost 50051

This shows only that something listens on the port. Through a tunnel it succeeds even when the server is not running, because the tunnel itself listens. If a LAN bind does not answer, check the firewall.

Start it with LinuxCNC

Add one line to your machine’s HAL file:

loadusr /home/your-username/linuxcnc-grpc-venv/bin/linuxcnc-grpc --host 127.0.0.1 --port 50051

Give the full path, because loadusr does not search the venv. Don’t use loadusr -W, which makes LinuxCNC wait forever. Auto-start with LinuxCNC has the details, and the systemd unit to use if the server must come back by itself.

Next