1. Giới thiệu
FOCAS2 là thư viện API của FANUC để giao tiếp với máy CNC qua Ethernet. Bài viết này hướng dẫn cách viết ứng dụng C# để kết nối và điều khiển CNC FANUC.
2. Giao diện ứng dụng
Tạo Windows Forms với các thành phần:
- TextBox txtIP: Nhập địa chỉ IP CNC
- TextBox txtPort: Nhập port (mặc định 8193)
- TextBox txtDevice: Nhập số device
- Button btnConnect: Nút kết nối
- Button btnDisconnect: Nút ngắt kết nối
- Label lblStatus: Hiển thị trạng thái kết nối
- Label lblDeviceStatus: Hiển thị trạng thái device
- Timer: Cập nhật trạng thái định kỳ
3. Code C# đầy đủ
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FanucCNCConnection
{
public partial class MainForm : Form
{
// Constants
private const short EW_OK = 0;
private ushort _handle = 0;
private bool _isConnected = false;
// API Import
[DllImport("Fwlib32.dll")]
private static extern short cnc_allclibhndl3(string ip, ushort port,
short timeout, out ushort handle);
[DllImport("Fwlib32.dll")]
private static extern short cnc_freelibhndl(ushort handle);
[DllImport("Fwlib32.dll")]
private static extern short cnc_statinfo(ushort handle, [Out] ODBST a);
[StructLayout(LayoutKind.Sequential)]
public class ODBST
{
public short dummy; public short tmmode;
public short aut; public short run;
public short motion; public short mstb;
public short emergency; public short alarm;
public short aut_check; public short hdck;
public short stack;
}
public MainForm()
{ InitializeComponent(); }
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
string ip = txtIP.Text;
ushort port = ushort.Parse(txtPort.Text);
short ret = cnc_allclibhndl3(ip, port, 10, out _handle);
if (ret == EW_OK)
{
_isConnected = true;
lblStatus.Text = "✅ Đã kết nối";
lblStatus.ForeColor = System.Drawing.Color.Green;
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
timerUpdate.Start();
}
else
{
lblStatus.Text = "❌ Lỗi kết nối: " + ret;
}
}
catch (Exception ex)
{
lblStatus.Text = "❌ " + ex.Message;
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
if (_isConnected)
{
timerUpdate.Stop();
cnc_freelibhndl(_handle);
_isConnected = false;
lblStatus.Text = "⏹️ Đã ngắt";
lblStatus.ForeColor = System.Drawing.Color.Black;
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
}
}
private void timerUpdate_Tick(object sender, EventArgs e)
{
if (!_isConnected) return;
ODBST status = new ODBST();
short ret = cnc_statinfo(_handle, status);
if (ret == EW_OK)
{
string mode = status.aut == 1 ? "Memory" : "MDI";
string run = status.run == 3 ? "Start" : "Stop";
string al = status.alarm != 0 ? "⚠️ ALARM" : "✅ OK";
lblDeviceStatus.Text = $"{mode} | {run} | {al}";
}
}
}
}
4. Cấu hình CNC
Trên máy FANUC, cài đặt:
- IP Address: 192.168.1.1
- Subnet Mask: 255.255.255.0
- Port: 8193
- Bật FOCAS2 trong tham số
5. Các hàm API quan trọng
- cnc_allclibhndl3: Kết nối với CNC
- cnc_freelibhndl: Đóng kết nối
- cnc_statinfo: Đọc trạng thái máy
- cnc_absolute: Đọc tọa độ
Bài viết dựa trên tài liệu FOCAS2 FANUC.
