65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
using S7.Net;
|
|
using SamplePre.Communication.Communication;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SamplePre.Communication
|
|
{
|
|
public class CommunicationFactory
|
|
{
|
|
/// <summary>
|
|
/// 创建通讯实例
|
|
/// </summary>
|
|
/// <param name="protocolType">协议类型</param>
|
|
/// <param name="connectionParams">连接参数</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotSupportedException"></exception>
|
|
public static ICommunication CreateCommunication(CommProtocolType protocolType, string connectionParams)
|
|
{
|
|
ICommunication comm = null;
|
|
|
|
switch (protocolType)
|
|
{
|
|
case CommProtocolType.S7:
|
|
comm = CreateS7(connectionParams);
|
|
break;
|
|
case CommProtocolType.ModbusTcp:
|
|
comm = CreateModbusTcp(connectionParams);
|
|
break;
|
|
case CommProtocolType.ModbusRtu:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
|
|
return comm;
|
|
}
|
|
|
|
|
|
public static ICommunication CreateS7(string connectionParams)
|
|
{
|
|
string[] params1 = connectionParams.Split(':');
|
|
if(params1 == null || params1.Length < 3) return null;
|
|
//ICommunication comm = new S7PlcCommunicator(CpuType.S71500, "192.168.10.2", 0, 1);
|
|
ICommunication comm = new S7PlcCommunicator(CpuType.S71500, params1[0], int.Parse( params1[1]), int.Parse( params1[2]));
|
|
|
|
return comm;
|
|
}
|
|
|
|
public static ICommunication CreateModbusTcp(string connectionParams)
|
|
{
|
|
string[] params1 = connectionParams.Split(':');
|
|
if (params1 == null || params1.Length < 2) return null;
|
|
ICommunication comm = new ModbusTcpCommunicator(params1[0], int.Parse(params1[1]));
|
|
|
|
return comm;
|
|
}
|
|
|
|
}
|
|
}
|