分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-06-20 58bd958f1fa8a85d0a3ac33a1ab1e2cab5d63dd2
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_WCS/WCSClient/Siemens/SiemensPLCClient.cs
@@ -3,6 +3,7 @@
using System.Net.NetworkInformation;
using WIDESEA_Comm.DataHandle;
using WIDESEA_Comm.LogInfo;
using static FreeSql.Internal.GlobalFilter;
namespace WIDESEA_WCS.WCSClient
{
@@ -14,9 +15,12 @@
        private readonly object _lockWrite = new object();
        public SiemensS7Net siemensPLCClient { get; set; }
        public SiemensPLCClient()
        public SiemensPLCClient(string model)
        {
            siemensPLCClient = new SiemensS7Net(SiemensPLCS.S1200);
            if (model == "S300")
                siemensPLCClient = new SiemensS7Net(SiemensPLCS.S300);
            else
                siemensPLCClient = new SiemensS7Net(SiemensPLCS.S1500);
            base.siemensPLCClient = this.siemensPLCClient;
        }
@@ -48,6 +52,7 @@
            siemensPLCClient.Port = Port;
            siemensPLCClient.Slot = (byte)Slot;
            siemensPLCClient.ConnectTimeOut = 1000 * 3;//超时时间
            siemensPLCClient.ReceiveTimeOut = 1000 * 3;
            siemensPLCClient.ConnectClose();
            var coonRes = siemensPLCClient.ConnectServer();
            IsConnected = coonRes.IsSuccess;
@@ -130,8 +135,11 @@
            }
            else if (typeof(DataType) == typeof(string))//字符串
            {
                var bytes = (byte[])GetContent(siemensPLCClient.Read(dbAddress, (ushort)len), dbAddress);
                string str = DataParse.GetStr(bytes, 0);
                //var bytes = (byte[])GetContent(siemensPLCClient.Read(dbAddress, (ushort)len), dbAddress);
                //string str = DataParse.GetStr(bytes, 0);
                //return (DataType)(str as object);
                var str = GetContent(siemensPLCClient.ReadString(dbAddress, (ushort)len), dbAddress).ToString();
                str = str.Replace("\0", "")?.Replace("\\", "")?.Replace("\u0014", "")?.Replace("?\u0006", "")?.Replace("\u000e\u000e", "")?.Replace("\n", "")?.Replace("?", "")?.Trim();
                return (DataType)(str as object);
            }
            else
@@ -145,9 +153,10 @@
        /// </summary>
        /// <param name="orderName">IO点名称</param>
        /// <returns></returns>
        public override DataType ReadByOrder<DataType>(string orderName)
        public override DataType ReadByOrder<DataType>(string orderName, string Method = null)
        {
            var item = itemGroups.Where(t => t.name.Equals(orderName)).FirstOrDefault();
            if (Method != null) { item = itemGroups.Where(t => t.name.Equals(orderName) && t.Methods.Equals(Method)).FirstOrDefault(); }
            if (item == null)
            {
                throw new Exception($"PLC{PLCName},未定义指令{orderName}");
@@ -188,7 +197,7 @@
            else if (typeof(DataType) == typeof(string))//字符串
            {
                var str = GetContent(siemensPLCClient.ReadString(item.dbAddress, (ushort)item.dataLen), item).ToString();
                str = str.Replace("\0", "");
                str = str.Replace("\0", "")?.Replace("\\", "")?.Replace("\u0014", "")?.Replace("?\u0006", "")?.Replace("\n", "")?.Replace("?", "")?.Trim();
                return (DataType)(str as object);
            }
            else
@@ -252,6 +261,7 @@
                if (!result.IsSuccess)
                {
                    IsConnected = false;
                    WriteLog.Write_Log(PLCName, $"{PLCName}_Write", $"{dbAddress}写入失败," + result.Message);
                    throw new Exception($"{dbAddress}写入失败," + result.Message);
                }
@@ -264,11 +274,12 @@
        /// </summary>
        /// <param name="orderName"></param>
        /// <exception cref="Exception"></exception>
        public override bool WriteByOrder(string orderName, object value)
        public override bool WriteByOrder(string orderName, object value, string Method = null)
        {
            lock (_lockWrite)
            {
                var item = itemGroups.Where(t => t.name.Equals(orderName)).FirstOrDefault();
                if (Method != null) { item = itemGroups.Where(t => t.name.Equals(orderName) && t.Methods.Equals(Method)).FirstOrDefault(); }
                if (item == null)
                {
                    throw new Exception($"PLC{PLCName},未定义指令{orderName}");
@@ -320,6 +331,7 @@
                if (!result.IsSuccess)
                {
                    IsConnected = false;
                    WriteLog.Write_Log(PLCName, $"{PLCName}_Write", $"{orderName}写入失败," + result.Message, item);
                    throw new Exception($"{PLCName},{orderName}写入失败," + result.Message);
                }