C# 开发实战:基于FinsTCP协议与欧姆龙PLC的上位机通信

97 阅读5分钟

前言

在工业自动化领域,PLC(可编程逻辑控制器)作为核心控制单元,与上位机之间的通讯至关重要。欧姆龙PLC支持FinsTCP协议,这是一种基于TCP/IP的工业通信协议,广泛用于数据读写和设备控制。本文将详细介绍如何使用C#开发基于FinsTCP协议的通讯模块,实现与欧姆龙PLC的数据交互。

一、FinsTCP协议简介

FinsTCP协议是欧姆龙公司为其PLC设备设计的一种基于TCP/IP的通信协议,支持数据读写、设备状态查询等功能。

其报文格式如下:

1、获取PLC节点地址

在建立连接后,需获取PLC的节点地址(DA1)和PC节点地址(SA1),用于后续的通信报文构造。

2、Fins命令结构

Fins命令用于定义通信类型(读/写)、目标内存区域、偏移地址等信息。其结构如下:

3、IO存储器地址标识

欧姆龙PLC的内存地址由内存区域(如DM区、CIO区等)、通道号(CH)和偏移量(Offset)组成,用于定位具体的数据位置。

二、实现过程

1、通讯原理

基于FinsTCP协议的通讯本质上是通过Socket/TCP/IP发送和接收字节数组,发送请求报文后接收并解析响应报文。

2、基于TcpClient的发送与接收方法

发送BYTE数据

public static bool SendData(out string msg, TcpClient tcpClient, byte[] sd)
{
    msg = string.Empty;
    try
    {
        tcpClient.GetStream().Write(sd, 0, sd.Length);
        return true;
    }
    catch(Exception ex)
    {
        msg = ex.Message;
        return false;
    }
}

接收BYTE数据

public static bool ReceiveData(out string msg, TcpClient tcpClient, byte[] rd)
{
    msg = string.Empty;
    try
    {
        int index = 0;
        do
        {
            int len = tcpClient.GetStream().Read(rd, index, rd.Length - index);
            if (len == 0)
                return false;
            else
                index += len;
        } while (index < rd.Length);
        return true;
    }
    catch(Exception ex)
    {
        msg = ex.Message;
        return false;
    }
}

3、基于Socket的发送与接收方法

发送BYTE数据

public bool SendData(out string msg, byte[] sd)
{
    msg = string.Empty;
    try
    {
        if(!(IsConnected && _Socket != null && _Socket.Connected))
        {
            if(!Connect(out msg))
            {
                Thread.Sleep(40);
                if (!Connect(out msg)) return false;
            }
        }
        _Socket.Send(sd, sd.Length, 0);
        return true;
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Disconnect(out string _msg);
        return false;
    }
}

接收BYTE数据

public bool ReceiveData(out string msg, byte[] rd)
{
    msg = string.Empty;
    try
    {
        if (!(IsConnected && _Socket != null && _Socket.Connected))
        {
            if (!Connect(out msg))
            {
                Thread.Sleep(40);
                if (!Connect(out msg)) return false;
            }
        }
        _Socket.Receive(rd, rd.Length, 0);
        return true;
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Disconnect(out string _msg);
        return false;
    }
}

4、网络状态检测

public static bool PingCheck(string ip, int connectTimeout = 10000)
{
    Ping ping = new Ping();
    PingReply pr = ping.Send(ip, connectTimeout);
    return pr.Status == IPStatus.Success;
}

三、欧姆龙PLC的连接与初始化

1、握手报文

private byte[] HandShake()
{
    byte[] array = new byte[20];
    array[0] = 0x46;
    array[1] = 0x49;
    array[2] = 0x4E;
    array[3] = 0x53;

    array[4] = 0;
    array[5] = 0;
    array[6] = 0;
    array[7] = 0x0C;

    array[8] = 0;
    array[9] = 0;
    array[10] = 0;
    array[11] = 0;

    array[12] = 0;
    array[13] = 0;
    array[14] = 0;
    array[15] = 0;

    array[16] = 0;
    array[17] = 0;
    array[18] = 0;
    array[19] = 0;

    return array;
}

2、Fins命令构造

private byte[] FinsCommand(RorW rw, PlcMemory mr, MemoryType mt, short ch, short offset, short cnt)
{
    byte[] array = new byte[34];
    array[0] = 0x46;
    array[1] = 0x49;
    array[2] = 0x4E;
    array[3] = 0x53;

    array[4] = 0;
    array[5] = 0;

    if (rw == RorW.Read)
    {
        array[6] = 0;
        array[7] = 0x1A;
    }
    else
    {
        if (mt == MemoryType.Word)
        {
            array[6] = (byte)((cnt * 2 + 26) / 256);
            array[7] = (byte)((cnt * 2 + 26) % 256);
        }
        else
        {
            array[6] = 0;
            array[7] = 0x1B;
        }
    }

    array[8] = 0;
    array[9] = 0;
    array[10] = 0;
    array[11] = 0x02;

    array[12] = 0;
    array[13] = 0;
    array[14] = 0;
    array[15] = 0;

    array[16] = 0x80;
    array[17] = 0x00;
    array[18] = 0x02;
    array[19] = 0x00;

    array[20] = PLCNode;
    array[21] = 0x00;
    array[22] = 0x00;
    array[23] = PCNode;

    array[24] = 0x00;
    array[25] = 0xFF;

    if (rw == RorW.Read)
    {
        array[26] = 0x01;
        array[27] = 0x01;
    }
    else
    {
        array[26] = 0x01;
        array[27] = 0x02;
    }

    array[28] = GetMemoryCode(mr, mt);
    array[29] = (byte)(ch / 256);
    array[30] = (byte)(ch % 256);
    array[31] = (byte)offset;

    array[32] = (byte)(cnt / 256);
    array[33] = (byte)(cnt % 256);

    return array;
}

3、连接初始化

public bool Open(out string msg)
{
    msg = string.Empty;
    try
    {
        if (!SocketHelper.PingCheck(Ip, ConnectTimeout))
        {
            msg = "网络故障!";
            return false;
        }

        tcpClient = new TcpClient();
        tcpClient.ReceiveTimeout = ReceiveTimeout;
        tcpClient.SendTimeout = SendTimeout;
        tcpClient.Connect(Ip, Port);

        if (!tcpClient.Connected)
        {
            throw new ApplicationException($"未连接到{Ip}");
        }

        if (!SocketHelper.SendData(out msg, tcpClient, HandShake()))
        {
            msg = $"连接,数据写入失败:{msg}!";
            return false;
        }

        byte[] buffer = new byte[24];
        if (!SocketHelper.ReceiveData(out msg, tcpClient, buffer))
        {
            msg = $"连接握手信号接收失败:{msg}!";
            return false;
        }

        if (buffer[15] != 0)
        {
            msg = $"超过最大连接数或内部连接错误";
            return false;
        }

        PCNode = buffer[19];
        PLCNode = buffer[23];
        msg = $"连接[{Ip}]成功";
        return true;

    }
    catch (Exception ex)
    {
        Close(out string _msg);
        msg = $"连接失败:{ex.Message}";
        return false;
    }
}

4、数据读取方法

public bool ReadWordsByte_B(out string msg, PlcMemory mr, int startIndex, int len, out byte[] reData)
{
    msg = string.Empty; reData = new byte[0];
    try
    {
        int i = 0;
        for (int index = startIndex; index < startIndex + len; index += OmronConsts.MAXREADDATE)
        {
            int _newLen = len + startIndex - index;
            if (_newLen > OmronConsts.MAXREADDATE) _newLen = OmronConsts.MAXREADDATE;

            byte[] array = FinsCmd(RorW.Read, mr, MemoryType.Word, (short)(index/2), 00, (short)(_newLen/2));

            if (!SocketHelper.SendData(out msg, tcpClient, array))
            {
                msg = $"读取,数据写入失败[{i}次]:{msg}!";
                return false;
            }

            byte[] buffer = new byte[30 + _newLen];
            // 后续解析逻辑...
        }
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        reData = new byte[0];
        return false;
    }
}

总结

本文详细介绍了基于欧姆龙PLC的FinsTCP协议实现C#上位机通讯模块的开发过程。主要包括协议报文格式、Socket/TcpClient的使用、握手连接、命令构造、数据收发等关键步骤。通过上述方法,可以实现与PLC的稳定通信,为上位机与PLC的数据交互打下坚实基础。

关键词

FinsTCP、欧姆龙PLC、C#通讯模块、Socket通信、TcpClient、握手协议、PLC节点地址、数据读写、报文格式、工业自动化

最后

如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!你的支持是我继续分享知识的动力。如果有任何疑问或需要进一步的帮助,欢迎随时留言。

也可以加入微信公众号 [DotNet技术匠] 社区,与其他热爱技术的同行一起交流心得,共同成长!

优秀是一种习惯,欢迎大家留言学习!