Install a client library and make the first calls. You need a
running server; the programs below reach it at localhost:50051, on the
LinuxCNC machine itself or through an
SSH tunnel.
Install a client
Status, a stream and HAL
Each program reads the status once, takes three updates from a status stream and lists the HAL
pins that match a pattern. None of them sends a command.
A real client gives every call a deadline, as the Python and Go programs do. A stream that runs
until Ctrl+C is in step 4 of the tutorial.
More to read from the server
The same stub has the other read-only calls. In Python:
importgrpcfromlinuxcnc_pbimportlinuxcnc_pb2,linuxcnc_pb2_grpcwithgrpc.insecure_channel("localhost:50051")aschannel:stub=linuxcnc_pb2_grpc.LinuxCNCServiceStub(channel)# What kind of machine this is: units, limits, which joint drives which axisconfig=stub.GetMachineConfig(linuxcnc_pb2.GetMachineConfigRequest(),timeout=5.0)print(config.machine_name,config.linear_units_name)forjointinconfig.joints:print(joint.joint_number,joint.axis_letter,joint.min_limit,joint.max_limit)# The server's version and whether it is connected to LinuxCNCserver_info=stub.GetServerInfo(linuxcnc_pb2.GetServerInfoRequest(),timeout=5.0)print(server_info.server_version,server_info.linuxcnc_connected)
GetServerInfo works while LinuxCNC is not running, when most other RPCs answer UNAVAILABLE.
A 1.0 server does not have it and answers UNIMPLEMENTED. See
GetMachineConfig,
GetServerInfo and, for uploading and
listing G-code files, File management.
Send a command
A command is a LinuxCNCCommand message with one command field set (state, mode, mdi,
jog and so on), sent with SendCommand. Four rules apply to every client:
RCS_DONE from SendCommand means the command was dispatched, not that it finished.
To wait for a command, pass the linuxcnc_serial from its response to WaitComplete, then
read the status to confirm that a motion is over (see
WaitComplete).
A refused command is not a gRPC error. SendCommand returns a normal response with
RCS_ERROR and the reason in error_message, so check response.status as well as catching
the RPC error (see
RCS_ERROR from SendCommand).
Client code should check that the machine is out of E-stop, on and homed before it sends
motion, and never take the machine out of E-stop, turn it on or home it itself.
Step 5 of the tutorial is a complete MDI program in each language that
makes these checks, and the jog_axis example does the same
for jogging.
Errors
Every call can fail with a gRPC status. In Python that is a grpc.RpcError:
UNAVAILABLE: the server cannot be reached, or it cannot reach or read LinuxCNC right now.
GetServerInfo tells the two apart, and the server’s own answers usually carry a
linuxcnc-grpc-reason in the trailing metadata.
INVALID_ARGUMENT, NOT_FOUND, ALREADY_EXISTS and ABORTED: a file RPC or WatchValues
was given something it cannot use.
RESOURCE_EXHAUSTED: too many streams and WaitComplete calls are open; try again when one
has ended.
FAILED_PRECONDITION: StreamErrors on a server started with --error-channel off, or
GetMachineConfig while the machine has been in cycle ever since the server connected; call
again once it is idle.