| | |
| | | { |
| | | // string ip = "10.68.9.15"; |
| | | short port = 102; |
| | | lock (_lockdpj) |
| | | |
| | | try |
| | | { |
| | | try |
| | | { |
| | | 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; |
| | | } |
| | | catch (Exception ex) |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 1); |
| | | if (plc.IsConnected == true) |
| | | { |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | throw; |
| | | LogHelper.Info($"该设备正在处理其他交互[{ip}]"); |
| | | throw new Exception("PLC协议层连接失败"); |
| | | } |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | int result = 0; |
| | | // 验证连接状态 |
| | | if (plc.IsConnected != true) |
| | | { |
| | | LogHelper.Info($"物理连接成功但协议层未就绪[{ip}]"); |
| | | throw new Exception("PLC协议层连接失败"); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | plc.Close(); |
| | | LogHelper.Info($"读取叠盘机,ip:{ip},端口:{port},地址:{startByte},读取值转换:{result}"); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | |
| | | catch (Exception ex) |
| | | { |
| | | plc.Close(); |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"ReadDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | throw; |
| | | } |
| | | |
| | | } |
| | | /// <summary> |
| | |
| | | bool result = false; |
| | | // string ip = "10.68.9.15"; |
| | | short port = 102; |
| | | lock (_lockdpj) |
| | | |
| | | try |
| | | { |
| | | try |
| | | { |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 0); |
| | | var plc = new Plc(CpuType.S7200Smart, ip, port, 0, 1); |
| | | |
| | | 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) |
| | | LogHelper.Info($"链接叠盘机,ip:{ip},端口:{port}"); |
| | | S7Helper.Link(plc); |
| | | if (plc.IsConnected != true) |
| | | { |
| | | // Task.Run(() => WriteDpj(ip, startByte, value)); |
| | | LogHelper.Info($"叠盘机:{ip}链接失败"); |
| | | LogHelper.Error($"WriteDpj ip:{ip},startByte:{startByte} Error:{ex}", ex); |
| | | return false; |
| | | LogHelper.Info($"物理连接成功但协议层未就绪[{ip}]"); |
| | | throw new Exception("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}"); |
| | | } |
| | | plc.Close(); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | |
| | | catch (Exception ex) |
| | | { |
| | | plc.Close(); |
| | | // 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 |
| | | |
| | | |
| | | |
| | | } |
| | | internal static short[] ReadInt(string device, int db, int byteAddr, int count) |
| | | { |