PLC通讯实现-C#实现欧姆龙以太网通讯FINS(二)
程序员文章站
2024-03-17 12:37:58
...
PLC通讯实现-C#实现欧姆龙以太网通讯FINS(二)
背景
本人近十年的工作都与工业软件相关、其中工控系统开发过程中有一个必要环节就是跟各大厂商的PLC进行通讯,而对于从互联网行业跨入工业互联网行业的从业人员来说要实现各型号PLC通讯还是需要一个过程的,本人在此对主流型号PLC通讯实现进行总结以便大家参考。
抽象设计
首先我们要进行一下抽象设计,先设计一个抽象类(接口也可以,此处因为还有其他业务使用了抽象类)BaseEquip,对PLC的常规操作进行定义,即Open、Read、Write、Close,业务代码调用BaseEquip进行PLC的读写,然后在实现各型号的Equip类,对Open、Read、Write、Close进行实现,根据配置在业务代码中对BaseEquip进行实例化,这样后期更改PLC型号后,只需修改配置即可,不用修改业务代码。
欧姆龙以太网通讯实现FINS
实现语言C#
抽象基类BaseEquip
public class BaseEquip
{
/// <summary>
/// 打开设备
/// </summary>
/// <returns>成功返回true,失败返回false</returns>
public abstract bool Open();
/// <summary>
/// 读取信息
/// </summary>
/// <param name="block">数据块</param>
/// <param name="start">起始地址</param>
/// <param name="len">长度</param>
/// <param name="buff">读取返回信息</param>
/// <returns>成功返回true,失败返回false</returns>
public abstract bool Read(string block, int start, int len, out object[] buff);
/// <summary>
/// 写入信息
/// </summary>
/// <param name="block">数据块</param>
/// <param name="start">起始地址</param>
/// <param name="buff">要写入的数据</param>
/// <returns>成功返回true,失败返回false</returns>
public abstract bool Write(int block, int start, object[] buff);
/// <summary>
/// 关闭设备
/// </summary>
public abstract void Close();
}
设备实现类Equip实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Equips;
namespace Mesnac.Equip.OMRON.FINS.GroupNet
{
public class Equip : BaseEquip
{
private TcpFactory _socketFactory = null;//TcpFactory.GetInstance("53", "53", "192.168.1.83", "9600");
private bool _isOpen = false; //是否打开连接
private Mesnac.Equips.Connection.GroupNet.ConnType connType = null;
private Dictionary<string, string> _dicIP = null;
#region 辅助方法 - 获取设备组IP字典
public Dictionary<string, string> GetIPS()
{
this.connType = (Mesnac.Equips.Connection.GroupNet.ConnType)this.Main.ConnType;
this._dicIP = new Dictionary<string, string>();
try
{
if (!String.IsNullOrEmpty(this.connType.IPList))
{
string[] strItems = this.connType.IPList.Split(new char[] { ';' });
if (null != strItems && strItems.Length > 0)
{
foreach (string strItem in strItems)
{
if (!String.IsNullOrEmpty(strItem))
{
string[] items = strItem.Split(new char[] { ':' });
if (null != items && items.Length > 0)
{
this._dicIP.Add(items[0], items[1]);
}
}
}
}
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("打开设备失败:解析设备配置错误-" + ex.Message);
}
return this._dicIP;
}
#endregion
/// <summary>
/// 获取串口工厂
/// </summary>
/// <returns></returns>
private TcpFactory GetTcpFactory()
{
//Mesnac.Equips.Connection.COM.ConnType connType = (Mesnac.Equips.Connection.COM.ConnType)this.Main.ConnType;
string IP = this._dicIP[this.Name]; ;
this._socketFactory = TcpFactory.GetInstance("", "", IP, "9600");
return this._socketFactory;
}
public override bool Open()
{
try
{
if (this._dicIP == null)
{
this._dicIP = GetIPS();
}
if (this._isOpen == true && (_socketFactory != null))
{
return true;
}
this.State = false;
this._socketFactory = this.GetTcpFactory();
this._socketFactory.creatConn();
if (!this._socketFactory.MainTCPSocket.Connected)
{
//ICSharpCode.Core.LoggingService.Warn("PLC连接失败:串口未打开失败!");
this.State = false;
return this.State;
}
else
{
this.State = true;
this._isOpen = true;
Console.WriteLine("连接成功!");
return this.State;
}
}
catch (Exception ex)
{
this.State = false;
this._isOpen = false;
Console.WriteLine(ex.Message);
return this.State;
}
}
public override bool Read(string block, int start, int len, out object[] buff)
{
buff = new object[len];
try
{
if (len > 256)
{
for (int i = 0; i < len; i++)
{
buff[i] = 0;
}
base.State = false;
return false;
}
int maxOneLen = 50; //单次允许读取的最大长度,欧姆龙限制为50个字
int count = len / maxOneLen; //要读取的次数
int mod = len % maxOneLen; //剩余的长度
bool flag = true; //保存读取标志
for (int i = 0; i < count; i++)
{
object[] _buff = new object[maxOneLen];
flag = this.ReadByLen(block, start + i * maxOneLen, maxOneLen, out _buff);
if (flag == false)
{
base.State = flag;
return false;
}
for (int k = i * maxOneLen; k < (i + 1) * maxOneLen; k++)
{
buff[k] = _buff[k - i * maxOneLen];
}
}
if (mod > 0)
{
object[] _buff = new object[mod];
flag = this.ReadByLen(block, start + count * maxOneLen, mod, out _buff);
if (flag == false)
{
base.State = flag;
return false;
}
for (int k = count * maxOneLen; k < count * maxOneLen + mod; k++)
{
buff[k] = _buff[k - count * maxOneLen];
}
}
base.State = flag;
return flag;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(String.Format("读取PLC(OMRON)设备失败-({0})!", ex.Message));
base.State = false;
return false;
}
}
/// <summary>
/// 单次读取最长20个字的方法
/// </summary>
/// <param name="block">块号</param>
/// <param name="start">起始字</param>
/// <param name="len">长度,最长不超过100</param>
/// <param name="buff">数据缓冲区,存放读取的数据</param>
/// <returns>读取成功返回true,读取失败返回false</returns>
private bool ReadByLen(string block, int start, int len, out object[] buff)
{
lock (this)
{
buff = new object[len];
if (!this.Open())
{
return false;
}
int state = len;
object[] _buff = new object[len];
int iblock = Convert.ToInt32(block);
//iblock = iblock + start;
bool result = this._socketFactory.Read(iblock, start, len, out _buff);
if (!result)
{
//ICSharpCode.Core.LoggingService.Warn("PLC读取失败:" + this.GetErrInfo(result));
this.State = false;
return false;
}
else
{
this.State = true;
}
int iReadLen = len;
if (iReadLen > state)
{
iReadLen = state;
}
for (int i = 0; i < iReadLen; i++)
{
int value = 0;
int.TryParse(_buff[i].ToString(), out value);
if (value > ushort.MaxValue)
{
value = ushort.MaxValue - value;
}
buff[i] = value;
}
return true;
}
}
public override bool Write(int block, int start, object[] buff)
{
lock (this)
{
try
{
if (buff.Length > 256)
{
return false;
}
int len = buff.Length;
int maxOneLen = 25; //单次允许读取的最大长度,OMRON限制为25个字
int count = len / maxOneLen; //要读取的次数
int mod = len % maxOneLen; //剩余的长度
bool flag = true; //保存写入标志
int[] _buff = new int[buff.Length];
for (int i = 0; i < buff.Length; i++)
{
//object[] _buff = new object[len];
//for (int k = i * maxOneLen; k < (i + 1) * maxOneLen; k++)
//{
// _buff[k - i * maxOneLen] = buff[k];
//}
int value = 0;
int.TryParse(buff[i].ToString(), out value);
_buff[i] = value;
}
flag = this._socketFactory.Write(block, start, _buff);
if (flag == false)
{
return false;
}
return flag;
}
catch (Exception ex)
{
//ICSharpCode.Core.LoggingService.Error(String.Format("写入PLC(OMRON)设备失败-({0})!", ex.Message));
//Console.WriteLine(ex.Message);
return false;
}
}
}
public override void Close()
{
lock (this)
{
try
{
this._socketFactory.MainTCPSocket.Close();
if (this._socketFactory.MainTCPSocket.Connected)
{
//ICSharpCode.Core.LoggingService.Warn("PLC【欧姆龙】关闭失败:端口处于打开状态");
}
else
{
this.State = false;
this._isOpen = false;
//Console.WriteLine("关闭成功!");
}
}
catch (Exception ex)
{
//ICSharpCode.Core.LoggingService.Error("PLC【欧姆龙】关闭失败:" + ex.Message);
}
}
}
}
}
Socket通讯工厂类TcpFactory实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Mesnac.Equip.OMRON.FINS
{
public class TcpFactory
{
private static TcpFactory _instance = null; //TCP工厂实例
private string _stationPC = "12"; //PC站点
private string _stationPLC = "13"; //PLC站点\
private string _plcIP = string.Empty;//PLCip
private string _tcpPort = "";//端口号
private byte client_node_no;
private byte server_node_no;
private TcpClient plc_Socket = null;
private NetworkStream myNetworkStream;
private TcpFactory(string stationPC, string stationPLC, string PlcIP, string TcpPort)
{
this._stationPC = stationPC;
this._stationPLC = stationPLC;
this._plcIP = PlcIP;
this._tcpPort = TcpPort;
}
public static TcpFactory GetInstance(string stationPC, string stationPLC, string PlcIP, string TcpPort)
{
return new TcpFactory(stationPC, stationPLC, PlcIP, TcpPort);
if (_instance == null)
{
_instance = new TcpFactory(stationPC, stationPLC, PlcIP, TcpPort);
}
return _instance;
}
/// <summary>
/// Socket对象
/// </summary>
public TcpClient MainTCPSocket
{
get
{
return this.plc_Socket;
}
}
#region 十六进制转换
private byte[] StringToBytes16(string source)
{
byte[] destination = new byte[source.Length / 2];
for (int i = 0, j = 0; i < source.Length && j < source.Length / 2; i += 2, j++)
{
string item = source.Substring(i, 2);
destination[j] = Convert.ToByte(item, 16);
}
return destination;
}
#endregion
/// <summary>
/// 通讯握手
/// FINS/TCP header(握手协议头)
/// Header(4bytes) + Length(4bytes) + Command(4bytes) + Error code(4bytes) + Client node address(4bytes)
/// Header 46494E53 ASCII code: 'FINS'
/// Length 0000000C 12bytes:Length of data from command onwards.
/// Command 00000000
/// Error code 00000000 Not used,so does not require checking by server.
/// Client node address (FINS node address of FINS/TCP client) 00000000 to 000000FE 0 to 254 Note: Client FINS node addresses are automatically obtained when set to 0.
/// </summary>
/// <param name="Plc_IP"></param>
/// <param name="Tcp_Port"></param>
/// <returns></returns>
public bool creatConn()
{
bool execResu = true;
//创建Socket连接 46494E530000000C000000000000000000000000
string tcp_header = "46494E530000000C000000000000000000000001";
byte[] fins_tcp_header = StringToBytes16(tcp_header);
try
{
plc_Socket = new TcpClient();
plc_Socket.Connect(IPAddress.Parse(_plcIP), int.Parse(_tcpPort));
myNetworkStream = plc_Socket.GetStream();
}
catch (SocketException ex)
{
ICSharpCode.Core.LoggingService.Error("PLC连接失败:" + ex.Message);
//MessageBox.Show(ex.Message);
execResu = false;
return execResu;
}
WriteData(fins_tcp_header);
byte[] responseMessage = ReadData();
//检查返回包
if (responseMessage.Length == 24)
{
if (responseMessage[8] != 0x00 || responseMessage[9] != 0x00 || responseMessage[10] != 0x00 || responseMessage[11] != 0x01
|| responseMessage[12] != 0x00 || responseMessage[13] != 0x00 || responseMessage[14] != 0x00 || responseMessage[15] != 0x00)
{
return false;
}
client_node_no = responseMessage[19];
server_node_no = responseMessage[23];
}
else
{
return false;
}
return execResu;
}
public bool Read(int block, int start, int len, out object[] buff)
{
lock (this)
{
string Ssend_header = "46494E530000001A";
Ssend_header += "0000000200000000";
byte[] send_header = StringToBytes16(Ssend_header);
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int SID = ra.Next(1, 100);//自动生成数来检查返回包
byte[] fins_header_comm = new byte[12];
fins_header_comm[0] = 0x80;//ICF
fins_header_comm[1] = 0x00;//RSV
fins_header_comm[2] = 0x02;//GCT
fins_header_comm[3] = 0x00; //DNA
fins_header_comm[4] = server_node_no;//PLC端节点号
fins_header_comm[5] = 0x00;//DA2
fins_header_comm[6] = 0x00;//SNA
fins_header_comm[7] = client_node_no;//PC端节点号,通过连接程序直接获得的
fins_header_comm[8] = 0x00;//SA2
fins_header_comm[9] = Convert.ToByte(SID.ToString(), 16);//SID
fins_header_comm[10] = 0x01;
fins_header_comm[11] = 0x01;//读命令
string saddr_value = "82" + Convert.ToString(block + start, 16).PadLeft(4, '0') + "00";//821C2000
saddr_value += Convert.ToString(len, 16).PadLeft(4, '0');
byte[] addr_value = StringToBytes16(saddr_value);
WriteData(Combin(send_header, fins_header_comm, addr_value));
//WriteData();
//WriteData();
byte[] Reseponse = ReadData();
//MessageBox.Show(Reseponse.Length.ToString());
//检查返回包
buff = new object[len];
if (Reseponse[8] != 0 || Reseponse[9] != 0 || Reseponse[10] != 0 || Reseponse[11] != 2
|| Reseponse[12] != 0 || Reseponse[13] != 0 || Reseponse[14] != 0 || Reseponse[15] != 0
|| Reseponse[26] != 1 || Reseponse[27] != 1 || Reseponse[28] != 0 || Reseponse[29] != 0
|| Reseponse[25] != Convert.ToByte(SID.ToString(), 16))
{
return false;
}
//MessageBox.Show((Convert.ToInt32(Reseponse[Reseponse.Length - 2].ToString("X2") + Reseponse[Reseponse.Length - 1].ToString("X2"), 16)).ToString());
int j = 0;
for (int i = len; i > 0; i--)
{
buff[j] = Convert.ToInt32(Reseponse[Reseponse.Length - (i * 2)].ToString("X2") + Reseponse[Reseponse.Length - i * 2 + 1].ToString("X2"));
j++;
}
return true;
}
}
byte[] Combin(byte[] header, byte[] comm, byte[] value)
{
int length=header.Length + comm.Length + value.Length;
byte[] returnByte = new byte[length];
int j=0;
for (int i = 0; i < header.Length; i++)
{
returnByte[j] = header[i];
j++;
}
for (int i = 0; i < comm.Length; i++)
{
returnByte[j] = comm[i];
j++;
}
for (int i = 0; i < value.Length; i++)
{
returnByte[j] = value[i];
j++;
}
return returnByte;
}
/// <summary>
/// 写
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public bool Write(int block, int start, int[] buff)
{
int writeCount = 1;//写入数量
string Ssend_header = "46494E53000000";
Ssend_header += Convert.ToString((2 * writeCount + 1 + 25), 16).PadLeft(2, '0');
Ssend_header += "0000000200000000";
byte[] send_header = StringToBytes16(Ssend_header);
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int SID = ra.Next(1, 100);
byte[] fins_header_comm = new byte[12];
fins_header_comm[0] = 0x80;//ICF4294967295
fins_header_comm[1] = 0x00;//RSV
fins_header_comm[2] = 0x02;//GCT
fins_header_comm[3] = 0x00; //DNA
fins_header_comm[4] = server_node_no;//PLC端节点号
fins_header_comm[5] = 0x00;//DA2
fins_header_comm[6] = 0x00;//SNA
fins_header_comm[7] = client_node_no;//PC端节点号,通过连接程序直接获得的
fins_header_comm[8] = 0x00;//SA2
fins_header_comm[9] = Convert.ToByte(SID.ToString(), 16);//SID
fins_header_comm[10] = 0x01;
fins_header_comm[11] = 0x02;//写命令
string saddr_value = "82" + Convert.ToString(block + start, 16).PadLeft(4, '0') + "00";
saddr_value += Convert.ToString(writeCount, 16).PadLeft(4, '0');
for (int i = 0; i < buff.Length; i++)
{
if ("System.Int64" == buff[i].GetType().ToString())
{
saddr_value += Convert.ToString((long)buff[i], 16).PadLeft(8, '0').Substring(4, 4);
saddr_value += Convert.ToString((long)buff[i], 16).PadLeft(8, '0').Substring(0, 4);
}
else
{
saddr_value += Convert.ToString((int)buff[i], 16).PadLeft(4, '0');
}
}
byte[] addr_value = StringToBytes16(saddr_value);
WriteData(send_header);
WriteData(fins_header_comm);
WriteData(addr_value);
byte[] Reseponse = ReadData();
//检查返回包
if (Reseponse[8] != 0 || Reseponse[9] != 0 || Reseponse[10] != 0 || Reseponse[11] != 2
|| Reseponse[12] != 0 || Reseponse[13] != 0 || Reseponse[14] != 0 || Reseponse[15] != 0
|| Reseponse[26] != 1 || Reseponse[27] != 2 || Reseponse[28] != 0 || Reseponse[29] != 0
|| Reseponse[25] != Convert.ToByte(SID.ToString(), 16))
{
ICSharpCode.Core.LoggingService.Error("PLC连接失败:" + Reseponse[8].ToString() + Reseponse[9].ToString() + Reseponse[10].ToString() + Reseponse[11].ToString());
//MessageBox.Show(Reseponse[8].ToString() + Reseponse[9].ToString() + Reseponse[10].ToString() + Reseponse[11].ToString());
return false;
}
return true;
}
#region 数据处理
private void WriteData(byte[] myByte)
{
byte[] writeBytes = myByte;
myNetworkStream.Write(writeBytes, 0, writeBytes.Length);
myNetworkStream.Flush();
Thread.Sleep(10);
}
private byte[] ReadData()
{
int k = plc_Socket.Available;
while (k == 0)
{
k = plc_Socket.Available;
Thread.Sleep(10);
}
byte[] myBufferBytes = new byte[k];
myNetworkStream.Read(myBufferBytes, 0, k);
myNetworkStream.Flush();
return myBufferBytes;
}
#endregion
}
}