Fanuc Focas Python
FOCAS is a proprietary library (DLLs on Windows, shared objects on Linux) that allows external applications to read from and write to a FANUC CNC over Ethernet or HSSB (High-Speed Serial Bus). It bypasses the need for PLC ladder logic for basic data retrieval.
Key capabilities include:
Here is a compact, production-ready loop that logs to CSV using pandas. fanuc focas python
import pandas as pd
from datetime import datetime
import time
def collect_machine_data(handle, csv_filename="cnc_data.csv"):
data_log = []
try:
while True:
timestamp = datetime.now()
status = get_status(handle)
spindle = get_spindle_load(handle)
pos = get_absolute_position(handle)
if spindle != -1.0 and pos:
row =
"Timestamp": timestamp,
"Status": status,
"Spindle_Load_Percent": spindle,
"X_Pos": pos['X'],
"Y_Pos": pos['Y'],
"Z_Pos": pos['Z']
data_log.append(row)
# Every 10 records, flush to CSV
if len(data_log) >= 10:
df = pd.DataFrame(data_log)
df.to_csv(csv_filename, mode='a', header=not pd.io.common.file_exists(csv_filename), index=False)
data_log = [] # Clear buffer
time.sleep(0.5) # Poll every 500ms (FOCAS limit is usually 100ms)
except KeyboardInterrupt:
print("Saving final data...")
if data_log:
pd.DataFrame(data_log).to_csv(csv_filename, mode='a', header=not pd.io.common.file_exists(csv_filename), index=False)
cnc = Fanuc('192.168.1.100', 8193)
If you do not want to manually define ctypes structures, there are open-source wrappers like pyfanuc on PyPI (though maintenance varies).
To use a wrapper (if available/compatible): FOCAS is a proprietary library (DLLs on Windows,
pip install pyfanuc
from pyfanuc import fanuc
cnc = fanuc.Fanuc('192.168.1.100')
if cnc.connect():
print(cnc.get_position())
cnc.disconnect()
Note: Using raw ctypes is often preferred in industrial environments because it does not depend on a third-party Python package maintainer.
import ctypes
import os
# Path to your Fwlib32.dll file
# Ensure this file is in your script directory or provide full path
dll_path = r"C:\Path\To\Fwlib32.dll"
try:
focas = ctypes.windll.LoadLibrary(dll_path)
except Exception as e:
print(f"Error loading FOCAS library: e")
exit()