| | |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading; |
| | | using System.Threading.Tasks; |
| | | using System.Web.Services.Description; |
| | | using System.Web.UI.WebControls.WebParts; |
| | |
| | | { |
| | | private static bool debug = true; |
| | | private static S7.Net.Plc plc = null; |
| | | |
| | | |
| | | static S7Helper() |
| | | { |
| | | Init(); |
| | |
| | | catch (Exception ex) |
| | | { |
| | | // Console.WriteLine($"plc{plc.IP}连接失败,err={ex.Message}"); |
| | | LogHelper.Info($"Link Error plc{plc.IP}连接失败,err={ex.Message}"); |
| | | 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(); |
| | | |
| | | /// <summary> |
| | | /// 合肥佳通读取叠盘机 |
| | | /// </summary> |
| | | /// <param name="startByte">偏移量地址 vb2001</param> |
| | | /// /// <param name="varType">值类型</param> |
| | | /// <returns></returns> |
| | | public static int ReadDpj(int startByte, S7.Net.VarType varType) |
| | | public static int ReadDpj(string ip, int startByte, S7.Net.VarType varType) |
| | | { |
| | | string ip = "10.68.9.15"; short port = 102; |
| | | try |
| | | // string ip = "10.68.9.15"; |
| | | short port = 102; |
| | | lock (_lockdpj) |
| | | { |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | int result = 0; |
| | | if (varType == VarType.Int) |
| | | try |
| | | { |
| | | var value = (short)plc.Read(DataType.DataBlock, 1, startByte, VarType.Int, 1); |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port},读取类型{varType},读取值{value}"); |
| | | result = int.Parse(value.ToString()); |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | int result = 0; |
| | | if (varType == VarType.Int) |
| | | { |
| | | var value = (short)plc.Read(DataType.DataBlock, 1, startByte, VarType.Int, 1); |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port},读取类型{varType},读取值{value}"); |
| | | result = int.Parse(value.ToString()); |
| | | } |
| | | else//合肥佳通除了数量,其他都是byte类型 |
| | | { |
| | | |
| | | byte value = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port},读取类型{varType},读取值{value}"); |
| | | result = value; |
| | | } |
| | | |
| | | LogHelper.Info($"读取叠盘机,ip:{ip},端口:{port},地址:{startByte},读取值转换:{result}"); |
| | | return result; |
| | | } |
| | | else |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | byte value = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port},读取类型{varType},读取值{value}"); |
| | | result = value; |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | throw; |
| | | } |
| | | } |
| | | |
| | | LogHelper.Info($"读取叠盘机,ip:{ip},端口:{port},地址:{startByte},读取值转换:{result}"); |
| | | return result; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogHelper.Info($"ReadDpj Error{ex}"); |
| | | throw; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | /// <param name="startByte">偏移量地址</param> |
| | | /// <param name="value">写入值 byte类型只能0 1</param> |
| | | /// <returns></returns> |
| | | public static bool WriteDpj(int startByte, int value) |
| | | public static bool WriteDpj(string ip, int startByte, byte value) |
| | | { |
| | | bool result = false; |
| | | string ip = "10.68.9.15"; short port = 102; |
| | | try |
| | | // string ip = "10.68.9.15"; |
| | | short port = 102; |
| | | lock (_lockdpj) |
| | | { |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); |
| | | |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | plc.Write( |
| | | dataType: DataType.DataBlock, |
| | | db: 1, |
| | | startByteAdr: startByte, |
| | | value: value |
| | | ); |
| | | LogHelper.Info($"写入叠盘机,ip:{ip},端口:{port},地址:{startByte},写入值:{value}"); |
| | | byte[] readBack = plc.ReadBytes(DataType.DataBlock, 1, startByte, 1); |
| | | if (readBack[0] == value) |
| | | try |
| | | { |
| | | result = true; |
| | | LogHelper.Info($"叠盘机写入成功,回读值:{readBack[0]}"); |
| | | } |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); |
| | | |
| | | return result; |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | // plc.Write($"VB{startByte}", value); |
| | | Thread.Sleep(300); |
| | | plc.Write( |
| | | dataType: DataType.DataBlock, |
| | | db: 1, |
| | | startByteAdr: startByte, |
| | | value: value |
| | | ); |
| | | LogHelper.Info($"写入叠盘机,ip:{ip},端口:{port},地址:{startByte},写入值:{value}"); |
| | | Thread.Sleep(300); |
| | | byte fdvalue = (byte)plc.Read(DataType.DataBlock, 1, startByte, VarType.Byte, 1); |
| | | if (fdvalue == value) |
| | | { |
| | | result = true; |
| | | LogHelper.Info($"叠盘机写入成功,回读值:{fdvalue}"); |
| | | } |
| | | else |
| | | { |
| | | // Task.Run(() => WriteDpj(ip, startByte, value)); |
| | | LogHelper.Info($"叠盘机写入失败,回读值:{fdvalue}"); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // Task.Run(() => WriteDpj(ip, startByte, value)); |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"WriteDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | return false; |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogHelper.Info($"WriteDpj Error{ex}"); |
| | | throw; |
| | | } |
| | | |
| | | |
| | | |
| | | // S7-200 Smart的V区也映射为DB1 |