| | |
| | | { |
| | | private static bool debug = true; |
| | | private static S7.Net.Plc plc = null; |
| | | |
| | | static S7Helper() |
| | | { |
| | | Init(); |
| | | } |
| | | private static Dictionary<string, Plc> plcDic = new Dictionary<string, Plc>(); |
| | | private static void Init() |
| | | { |
| | | //配置文件读取所有的plc进行初始化 |
| | | try |
| | | { |
| | | var plc1 = new Plc(CpuType.S71500, "", 0, 1); |
| | | plcDic.Add("plc1", plc1); |
| | | Link(plc1); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | Console.WriteLine("S7Helper Init err=" + ex.Message); |
| | | } |
| | | } |
| | | //static S7Helper() |
| | | //{ |
| | | // Init(); |
| | | //} |
| | | private static Dictionary<string, Plc> plcDic = new Dictionary<string, Plc>(); |
| | | //private static void Init() |
| | | //{ |
| | | // //配置文件读取所有的plc进行初始化 |
| | | // try |
| | | // { |
| | | // var plc1 = new Plc(CpuType.S71500, "", 0, 1); |
| | | // plcDic.Add("plc1", plc1); |
| | | // Link(plc1); |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | |
| | | // Console.WriteLine("S7Helper Init err=" + ex.Message); |
| | | // } |
| | | //} |
| | | private static Plc GetPlc(string plc) |
| | | { |
| | | if (plcDic.ContainsKey(plc)) |
| | |
| | | catch (Exception ex) |
| | | { |
| | | // Console.WriteLine($"plc{plc.IP}连接失败,err={ex.Message}"); |
| | | LogHelper.Error($"Link Error plc{plc.IP}连接失败,err={ex.Message}",ex); |
| | | LogHelper.Error($"Link Error plc{plc.IP}连接失败,err={ex.Message}", ex); |
| | | //Init(); |
| | | } |
| | | |
| | |
| | | //https://www.ad.siemens.com.cn/productportal/Prods/S7-1200_PLC_EASY_PLUS/SmartSMS/060.html |
| | | //https://www.ad.siemens.com.cn/productportal/Prods/S7-1200_PLC_EASY_PLUS/07-Program/02-basic/01-Data_Type/09-String.html |
| | | |
| | | private static object _lockdpj=new object(); |
| | | private static object _lockdpj = new object(); |
| | | |
| | | /// <summary> |
| | | /// 合肥佳通读取叠盘机 |
| | |
| | | /// <param name="startByte">偏移量地址 vb2001</param> |
| | | /// /// <param name="varType">值类型</param> |
| | | /// <returns></returns> |
| | | public static int ReadDpj(string ip,int startByte, S7.Net.VarType varType) |
| | | public static int ReadDpj(string ip, int startByte, S7.Net.VarType varType) |
| | | { |
| | | // string ip = "10.68.9.15"; |
| | | // string ip = "10.68.9.15"; |
| | | short port = 102; |
| | | lock (_lockdpj) |
| | | { |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogHelper.Error($"ReadDpj Error:{ex}", ex); |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | /// <param name="startByte">偏移量地址</param> |
| | | /// <param name="value">写入值 byte类型只能0 1</param> |
| | | /// <returns></returns> |
| | | public static bool WriteDpj(string ip,int startByte, byte value) |
| | | public static bool WriteDpj(string ip, int startByte, byte value) |
| | | { |
| | | bool result = false; |
| | | // string ip = "10.68.9.15"; |
| | | // string ip = "10.68.9.15"; |
| | | short port = 102; |
| | | lock (_lockdpj) |
| | | { |
| | |
| | | |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | // plc.Write($"VB{startByte}", value); |
| | | // plc.Write($"VB{startByte}", value); |
| | | Thread.Sleep(300); |
| | | plc.Write( |
| | | dataType: DataType.DataBlock, |
| | |
| | | } |
| | | else |
| | | { |
| | | // Task.Run(() => WriteDpj(ip, startByte, value)); |
| | | LogHelper.Info($"叠盘机写入失败,回读值:{fdvalue}"); |
| | | } |
| | | |
| | | return result; |
| | | return result; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogHelper.Info($"WriteDpj Error{ex}"); |
| | | throw; |
| | | // Task.Run(() => WriteDpj(ip, startByte, value)); |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"WriteDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | // S7-200 Smart的V区也映射为DB1 |