private void userButton40_Click(object sender, EventArgs e) { // 读取操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的 bool M100_7 = siemensTcpNet.ReadBool("M100.7").Content; // 读取M100.7是否通断 byte byte_M100 = siemensTcpNet.ReadByte("M100").Content; // 读取M100的值 short short_M100 = siemensTcpNet.ReadInt16("M100").Content; // 读取M100-M101组成的字 ushort ushort_M100 = siemensTcpNet.ReadUInt16("M100").Content; // 读取M100-M101组成的无符号的值 int int_M100 = siemensTcpNet.ReadInt32("M100").Content; // 读取M100-M103组成的有符号的数据 uint uint_M100 = siemensTcpNet.ReadUInt32("M100").Content; // 读取M100-M103组成的无符号的值 float float_M100 = siemensTcpNet.ReadFloat("M100").Content; // 读取M100-M103组成的单精度值 long long_M100 = siemensTcpNet.ReadInt64("M100").Content; // 读取M100-M107组成的大数据值 ulong ulong_M100 = siemensTcpNet.ReadUInt64("M100").Content; // 读取M100-M107组成的无符号大数据 double double_M100 = siemensTcpNet.ReadDouble("M100").Content; // 读取M100-M107组成的双精度值 string str_M100 = siemensTcpNet.ReadString("M100", 10).Content; // 读取M100-M109组成的ASCII字符串数据 // 写入操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的 siemensTcpNet.Write("M100.7", true); // 写位 siemensTcpNet.Write("M100", (byte)0x33); // 写单个字节 siemensTcpNet.Write("M100", (short)12345); // 写双字节有符号 siemensTcpNet.Write("M100", (ushort)45678); // 写双字节无符号 siemensTcpNet.Write("M100", 123456789); // 写双字有符号 siemensTcpNet.Write("M100", (uint)3456789123); // 写双字无符号 siemensTcpNet.Write("M100", 123.456f); // 写单精度 siemensTcpNet.Write("M100", 1234556434534545L); // 写大整数有符号 siemensTcpNet.Write("M100", 523434234234343UL); // 写大整数无符号 siemensTcpNet.Write("M100", 123.456d); // 写双精度 siemensTcpNet.Write("M100", "K123456789"); // 写ASCII字符串 }
private void Test1( ) { OperateResult <bool> read = siemensTcpNet.ReadBool("M100.4"); if (read.IsSuccess) { bool m100_4 = read.Content; } else { // failed string err = read.Message; } OperateResult write = siemensTcpNet.Write("M100.4", true); if (write.IsSuccess) { // success } else { // failed string err = write.Message; } }
public bool ReadBool(string addr) { var val = siemensTcpNet.ReadBool(addr); if (val.IsSuccess) { return(val.Content); } return(false); }
/// <summary> /// 读取西门子PLC整组输入状态 /// </summary> /// <param name="readCount">需要读取输入个数</param> /// <returns></returns> public bool[] ReadAllInput(int readCount) { bool[] listInput = new bool[readCount]; int H = 0; int L = 0; string IStr = string.Empty; for (int i = 0; i < readCount; i++) { H = i / 8; L = i % 8; IStr = "I" + H.ToString() + "." + L.ToString(); if (siemensS7Net.ReadBool(IStr).Content) { listInput[i] = true; } else { listInput[i] = false;; } } return(listInput); }
public void ReadBool( ) { #region ReadBool SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110"); // 以下是简单的读取,没有仔细校验的方式 bool X1 = siemens.ReadBool("M100.0").Content; // 如果需要判断是否读取成功 OperateResult <bool> R_X1 = siemens.ReadBool("M100.0"); if (R_X1.IsSuccess) { // success bool value = R_X1.Content; } else { // failed } #endregion }
/// <summary> /// 读取单个变量,返回结果 /// </summary> /// <param name="type">数据类型</param> /// <param name="address">地址</param> /// <returns></returns> public static object ReadItem(DataType type, string address) { object result = null; switch (type) { case DataType.Bool: result = siemensS7Net.ReadBool(address).Content; break; case DataType.Byte: result = siemensS7Net.ReadByte(address).Content; break; case DataType.Int16: result = siemensS7Net.ReadInt16(address).Content; break; case DataType.UInt16: result = siemensS7Net.ReadUInt16(address).Content; break; case DataType.Int32: result = siemensS7Net.ReadInt32(address).Content; break; case DataType.UInt32: result = siemensS7Net.ReadUInt32(address).Content; break; case DataType.Float: result = siemensS7Net.ReadFloat(address).Content; break; case DataType.Int64: result = siemensS7Net.ReadInt64(address).Content; break; case DataType.UInt64: result = siemensS7Net.ReadUInt64(address).Content; break; case DataType.Double: result = siemensS7Net.ReadDouble(address).Content; break; } return(result); }
//连接监控 public void TimerCheck_Tick(object sender, EventArgs e) { IDictionaryEnumerator enumerator = Plcs.GetEnumerator(); while (enumerator.MoveNext()) { SiemensS7Net plc = enumerator.Value as SiemensS7Net; if (plc != null) { if (!plc.ReadBool("M0").IsSuccess) { plc.ConnectClose(); } } } }
private void btnReadBool_Click(object sender, EventArgs e) { if (!isConnected) { MessageBox.Show("还未连接 PLC"); return; } try { OperateResult <bool> rlt = plc.ReadBool($"DB{edtDBNumber.Text}.{edtOffset.Text}"); if (rlt.IsSuccess) { edtText.Text = rlt.Content.ToString(); } else { edtText.Text = $"{rlt.ToMessageShowString()}"; } } catch { edtText.Text = ""; } }
/// <summary> /// 读取Bool变量 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Read_Bool_Click(object sender, EventArgs e) { ReadResultRender(siemensTcpNet.ReadBool(txt_ReadSingle_Address.Text), txt_ReadSingle_Address.Text, txt_Result_Single); }
private void button_read_bool_Click(object sender, EventArgs e) { // 读取bool变量 readResultRender(siemensTcpNet.ReadBool(textBox3.Text), textBox3.Text, textBox4); }
//PLC读取 private object ReadSieTcpValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, SiemensS7Net plc) { try { if (plctype == null || !plctype.IsConnected || plcdata == null || plc == null) { return(null); } switch (plcdata.DataType) { case Common.DataTypes.Bool: //Bool plcdata.ValueNew = plc.ReadBool(plcdata.Address).Content; break; case Common.DataTypes.Byte: //Byte plcdata.ValueNew = plc.ReadByte(plcdata.Address).Content; break; case Common.DataTypes.Short: plcdata.ValueNew = plc.ReadInt16(plcdata.Address).Content; break; case Common.DataTypes.Ushort: plcdata.ValueNew = plc.ReadUInt16(plcdata.Address).Content; break; case Common.DataTypes.Int: plcdata.ValueNew = plc.ReadInt32(plcdata.Address).Content; break; case Common.DataTypes.UInt: plcdata.ValueNew = plc.ReadUInt32(plcdata.Address).Content; break; case Common.DataTypes.Long: long lValueNew = 0; if (long.TryParse(plc.ReadInt64(plcdata.Address).Content.ToString(), out lValueNew)) { long temp = BpLong.SwapInt64(lValueNew); plcdata.ValueNew = temp; } // plcdata.ValueNew = plc.ReadInt64(plcdata.Address).Content; break; case Common.DataTypes.ULong: plcdata.ValueNew = plc.ReadUInt64(plcdata.Address).Content; break; case Common.DataTypes.Float: plcdata.ValueNew = plc.ReadFloat(plcdata.Address).Content; break; case Common.DataTypes.Double: plcdata.ValueNew = plc.ReadDouble(plcdata.Address).Content; break; case Common.DataTypes.String: HslCommunication.OperateResult <byte[]> data = (HslCommunication.OperateResult <byte[]>)plc.Read(plcdata.Address, 50); if (data != null && data.Content != null && data.Content.Length > 2) { List <byte> lstData = new List <byte>(); int nLen = data.Content[1]; for (int i = 2; i < nLen + 2; i++) { lstData.Add(data.Content[i]); } plcdata.ValueNew = System.Text.Encoding.ASCII.GetString(lstData.ToArray()); } break; default: break; } } catch { //MessageBox.Show(ex.Message); } return(plcdata.ValueNew); }
public void MelsecUnitTest( ) { SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1200, "192.168.8.12"); if (!plc.ConnectServer( ).IsSuccess) { Console.WriteLine("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试"); return; } // 开始单元测试,从bool类型开始测试 string address = "M200.4"; Assert.IsTrue(plc.Write(address, true).IsSuccess); Assert.IsTrue(plc.ReadBool(address).Content == true); address = "M300"; // short类型 Assert.IsTrue(plc.Write(address, (short)12345).IsSuccess); Assert.IsTrue(plc.ReadInt16(address).Content == 12345); short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 }; Assert.IsTrue(plc.Write(address, shortTmp).IsSuccess); short[] readShort = plc.ReadInt16(address, (ushort)shortTmp.Length).Content; for (int i = 0; i < readShort.Length; i++) { Assert.IsTrue(readShort[i] == shortTmp[i]); } // ushort类型 Assert.IsTrue(plc.Write(address, (ushort)51234).IsSuccess); Assert.IsTrue(plc.ReadUInt16(address).Content == 51234); ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 }; Assert.IsTrue(plc.Write(address, ushortTmp).IsSuccess); ushort[] readUShort = plc.ReadUInt16(address, (ushort)ushortTmp.Length).Content; for (int i = 0; i < ushortTmp.Length; i++) { Assert.IsTrue(readUShort[i] == ushortTmp[i]); } // int类型 Assert.IsTrue(plc.Write(address, 12342323).IsSuccess); Assert.IsTrue(plc.ReadInt32(address).Content == 12342323); int[] intTmp = new int[] { 123812512, 123534, 976124, -1286742 }; Assert.IsTrue(plc.Write(address, intTmp).IsSuccess); int[] readint = plc.ReadInt32(address, (ushort)intTmp.Length).Content; for (int i = 0; i < intTmp.Length; i++) { Assert.IsTrue(readint[i] == intTmp[i]); } // uint类型 Assert.IsTrue(plc.Write(address, (uint)416123237).IsSuccess); Assert.IsTrue(plc.ReadUInt32(address).Content == (uint)416123237); uint[] uintTmp = new uint[] { 81623123, 91712749, 91273123, 123, 21242, 5324 }; Assert.IsTrue(plc.Write(address, uintTmp).IsSuccess); uint[] readuint = plc.ReadUInt32(address, (ushort)uintTmp.Length).Content; for (int i = 0; i < uintTmp.Length; i++) { Assert.IsTrue(readuint[i] == uintTmp[i]); } // float类型 Assert.IsTrue(plc.Write(address, 123.45f).IsSuccess); Assert.IsTrue(plc.ReadFloat(address).Content == 123.45f); float[] floatTmp = new float[] { 123, 5343, 1.45f, 563.3f, 586.2f }; Assert.IsTrue(plc.Write(address, floatTmp).IsSuccess); float[] readFloat = plc.ReadFloat(address, (ushort)floatTmp.Length).Content; for (int i = 0; i < readFloat.Length; i++) { Assert.IsTrue(floatTmp[i] == readFloat[i]); } // double类型 Assert.IsTrue(plc.Write(address, 1234.5434d).IsSuccess); Assert.IsTrue(plc.ReadDouble(address).Content == 1234.5434d); double[] doubleTmp = new double[] { 1.4213d, 1223d, 452.5342d, 231.3443d }; Assert.IsTrue(plc.Write(address, doubleTmp).IsSuccess); double[] readDouble = plc.ReadDouble(address, (ushort)doubleTmp.Length).Content; for (int i = 0; i < doubleTmp.Length; i++) { Assert.IsTrue(readDouble[i] == doubleTmp[i]); } // long类型 Assert.IsTrue(plc.Write(address, 123617231235123L).IsSuccess); Assert.IsTrue(plc.ReadInt64(address).Content == 123617231235123L); long[] longTmp = new long[] { 12312313123L, 1234L, 412323812368L, 1237182361238123 }; Assert.IsTrue(plc.Write(address, longTmp).IsSuccess); long[] readLong = plc.ReadInt64(address, (ushort)longTmp.Length).Content; for (int i = 0; i < longTmp.Length; i++) { Assert.IsTrue(readLong[i] == longTmp[i]); } // ulong类型 Assert.IsTrue(plc.Write(address, 1283823681236123UL).IsSuccess); Assert.IsTrue(plc.ReadUInt64(address).Content == 1283823681236123UL); ulong[] ulongTmp = new ulong[] { 21316UL, 1231239127323UL, 1238612361283123UL }; Assert.IsTrue(plc.Write(address, ulongTmp).IsSuccess); ulong[] readULong = plc.ReadUInt64(address, (ushort)ulongTmp.Length).Content; for (int i = 0; i < readULong.Length; i++) { Assert.IsTrue(readULong[i] == ulongTmp[i]); } // string类型 Assert.IsTrue(plc.Write(address, "123123").IsSuccess); Assert.IsTrue(plc.ReadString(address, 6).Content == "123123"); // byte类型 byte[] byteTmp = new byte[] { 0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8 }; Assert.IsTrue(plc.Write(address, byteTmp).IsSuccess); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(plc.Read(address, 6).Content, byteTmp)); plc.ConnectClose( ); }
//试验结束 public bool ReadTestReadyFinish() { return(siemensTcpNet.ReadBool(ConfigurationManager.AppSettings["TestReadyFinishAddress"].ToString()).Content); }
/// <param name="vs"></param> /// <summary> /// 读取指定块的值 /// </summary> public object Read(int index) { try { object result; var item = ListItem[index]; var arr = item.Trim().Split('.'); if (arr.Length > 1) { string types = Regex.Replace(arr[1], "[0-9]", "", RegexOptions.IgnoreCase).Trim();//获取地址块的类型 switch (types.ToLower()) { case "bool": result = SiemensTcpNet.ReadBool(GetNewItem(item)).Content; break; case "byte": result = SiemensTcpNet.ReadByte(GetNewItem(item)).Content; break; case "w": result = SiemensTcpNet.ReadInt16(GetNewItem(item)).Content; break; case "ushort": //ushort result = SiemensTcpNet.ReadUInt16(GetNewItem(item)).Content; break; case "dint": result = SiemensTcpNet.ReadInt32(GetNewItem(item)).Content; break; case "uint": result = SiemensTcpNet.ReadUInt32(GetNewItem(item)).Content; break; case "long": result = SiemensTcpNet.ReadInt64(GetNewItem(item)).Content; break; case "ulong": result = SiemensTcpNet.ReadUInt64(GetNewItem(item)).Content; break; case "real": result = SiemensTcpNet.ReadFloat(GetNewItem(item)).Content; break; case "double": result = SiemensTcpNet.ReadDouble(GetNewItem(item)).Content; break; case "string": result = SiemensTcpNet.ReadString(GetNewItem(item), 10).Content; break; default: result = -1; break; } return(result); } return(-1); } catch (Exception) { return(-1); } } /// <summary>
public async Task SiemensUnitTest( ) { SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1200, "192.168.8.12"); // "192.168.8.12" if (!plc.ConnectServer( ).IsSuccess) { Console.WriteLine("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试"); return; } // 开始单元测试,从bool类型开始测试 string address = "M200.4"; Assert.IsTrue(plc.Write(address, true).IsSuccess); Assert.IsTrue(plc.ReadBool(address).Content == true); address = "M300"; // short类型 Assert.IsTrue(plc.Write(address, (short)12345).IsSuccess); Assert.IsTrue(plc.ReadInt16(address).Content == 12345); short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 }; Assert.IsTrue(plc.Write(address, shortTmp).IsSuccess); short[] readShort = plc.ReadInt16(address, (ushort)shortTmp.Length).Content; for (int i = 0; i < readShort.Length; i++) { Assert.IsTrue(readShort[i] == shortTmp[i]); } // 异步short类型 for (int j = 0; j < 100; j++) { Assert.IsTrue((await plc.WriteAsync(address, (short)12345)).IsSuccess); Assert.IsTrue((await plc.ReadInt16Async(address)).Content == 12345); Assert.IsTrue((await plc.WriteAsync(address, shortTmp)).IsSuccess); readShort = (await plc.ReadInt16Async(address, (ushort)shortTmp.Length)).Content; for (int i = 0; i < readShort.Length; i++) { Assert.IsTrue(readShort[i] == shortTmp[i]); } } // ushort类型 Assert.IsTrue(plc.Write(address, (ushort)51234).IsSuccess); Assert.IsTrue(plc.ReadUInt16(address).Content == 51234); ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 }; Assert.IsTrue(plc.Write(address, ushortTmp).IsSuccess); ushort[] readUShort = plc.ReadUInt16(address, (ushort)ushortTmp.Length).Content; for (int i = 0; i < ushortTmp.Length; i++) { Assert.IsTrue(readUShort[i] == ushortTmp[i]); } // int类型 Assert.IsTrue(plc.Write(address, 12342323).IsSuccess); Assert.IsTrue(plc.ReadInt32(address).Content == 12342323); int[] intTmp = new int[] { 123812512, 123534, 976124, -1286742 }; Assert.IsTrue(plc.Write(address, intTmp).IsSuccess); int[] readint = plc.ReadInt32(address, (ushort)intTmp.Length).Content; for (int i = 0; i < intTmp.Length; i++) { Assert.IsTrue(readint[i] == intTmp[i]); } // uint类型 Assert.IsTrue(plc.Write(address, (uint)416123237).IsSuccess); Assert.IsTrue(plc.ReadUInt32(address).Content == (uint)416123237); uint[] uintTmp = new uint[] { 81623123, 91712749, 91273123, 123, 21242, 5324 }; Assert.IsTrue(plc.Write(address, uintTmp).IsSuccess); uint[] readuint = plc.ReadUInt32(address, (ushort)uintTmp.Length).Content; for (int i = 0; i < uintTmp.Length; i++) { Assert.IsTrue(readuint[i] == uintTmp[i]); } // float类型 Assert.IsTrue(plc.Write(address, 123.45f).IsSuccess); Assert.IsTrue(plc.ReadFloat(address).Content == 123.45f); float[] floatTmp = new float[] { 123, 5343, 1.45f, 563.3f, 586.2f }; Assert.IsTrue(plc.Write(address, floatTmp).IsSuccess); float[] readFloat = plc.ReadFloat(address, (ushort)floatTmp.Length).Content; for (int i = 0; i < readFloat.Length; i++) { Assert.IsTrue(floatTmp[i] == readFloat[i]); } // double类型 Assert.IsTrue(plc.Write(address, 1234.5434d).IsSuccess); Assert.IsTrue(plc.ReadDouble(address).Content == 1234.5434d); double[] doubleTmp = new double[] { 1.4213d, 1223d, 452.5342d, 231.3443d }; Assert.IsTrue(plc.Write(address, doubleTmp).IsSuccess); double[] readDouble = plc.ReadDouble(address, (ushort)doubleTmp.Length).Content; for (int i = 0; i < doubleTmp.Length; i++) { Assert.IsTrue(readDouble[i] == doubleTmp[i]); } // long类型 Assert.IsTrue(plc.Write(address, 123617231235123L).IsSuccess); Assert.IsTrue(plc.ReadInt64(address).Content == 123617231235123L); long[] longTmp = new long[] { 12312313123L, 1234L, 412323812368L, 1237182361238123 }; Assert.IsTrue(plc.Write(address, longTmp).IsSuccess); long[] readLong = plc.ReadInt64(address, (ushort)longTmp.Length).Content; for (int i = 0; i < longTmp.Length; i++) { Assert.IsTrue(readLong[i] == longTmp[i]); } // ulong类型 Assert.IsTrue(plc.Write(address, 1283823681236123UL).IsSuccess); Assert.IsTrue(plc.ReadUInt64(address).Content == 1283823681236123UL); ulong[] ulongTmp = new ulong[] { 21316UL, 1231239127323UL, 1238612361283123UL }; Assert.IsTrue(plc.Write(address, ulongTmp).IsSuccess); ulong[] readULong = plc.ReadUInt64(address, (ushort)ulongTmp.Length).Content; for (int i = 0; i < readULong.Length; i++) { Assert.IsTrue(readULong[i] == ulongTmp[i]); } // string类型 Assert.IsTrue(plc.Write(address, "123123").IsSuccess); Assert.IsTrue(plc.ReadString(address).Content == "123123"); // 中文,编码可以自定义 Assert.IsTrue(plc.Write(address, "测试信息123", Encoding.Unicode).IsSuccess); Assert.IsTrue(plc.ReadString(address, 14, Encoding.Unicode).Content == "测试信息123"); // byte类型 byte[] byteTmp = new byte[] { 0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8 }; Assert.IsTrue(plc.Write(address, byteTmp).IsSuccess); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(plc.Read(address, 6).Content, byteTmp)); // 批量写入测试 short[] shortValues = new short[50]; for (int i = 0; i < 50; i++) { shortValues[i] = (short)(i * 5 - 3); } Assert.IsTrue(plc.Write("M300", shortValues).IsSuccess); string[] addresses = new string[50]; ushort[] lengths = new ushort[50]; for (int i = 0; i < 50; i++) { addresses[i] = "M" + (i * 2 + 300); lengths[i] = 2; } OperateResult <byte[]> readBytes = plc.Read(addresses, lengths); Assert.IsTrue(readBytes.IsSuccess); Assert.IsTrue(readBytes.Content.Length == 100); for (int i = 0; i < 50; i++) { short shortTmp1 = plc.ByteTransform.TransInt16(readBytes.Content, i * 2); Assert.IsTrue(shortValues[i] == shortTmp1); } // 自定义类的测试 DataTest test = new DataTest( ) { Data1 = 425, Data2 = 123.53f, Data3 = new byte[] { 2, 4, 6, 8, 100, 123 } }; Assert.IsTrue(plc.Write(test).IsSuccess); Assert.IsTrue(plc.ReadInt16("M100").Content == 425); Assert.IsTrue(plc.ReadFloat("M200").Content == 123.53f); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(plc.Read("M300", 6).Content, test.Data3)); DataTest test1 = plc.Read <DataTest>( ).Content; Assert.IsTrue(test1.Data1 == test.Data1); Assert.IsTrue(test1.Data2 == test.Data2); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(test1.Data3, test.Data3)); // 大数据写入测试 Assert.IsTrue(plc.Write("M100", (short)12345).IsSuccess); Assert.IsTrue(plc.Write("M500", (short)12345).IsSuccess); Assert.IsTrue(plc.Write("M800", (short)12345).IsSuccess); OperateResult <short[]> readBatchResult = plc.ReadInt16("M100", 351); Assert.IsTrue(readBatchResult.IsSuccess); Assert.IsTrue(readBatchResult.Content[0] == 12345); Assert.IsTrue(readBatchResult.Content[200] == 12345); Assert.IsTrue(readBatchResult.Content[350] == 12345); plc.ConnectClose( ); }
public override object ReadTag(Tag tag) { //LOG.Info("HSLTag开始读取" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")); if (tag.AccessType == TagAccessType.Read || tag.AccessType == TagAccessType.ReadWrite) { try { if (tag.TagType == "bool") { var res = PLC.ReadBool(tag.Address); if (res.IsSuccess) { tag.TagValue = res.Content; tag.Quality = Quality.Good; } else { tag.Quality = Quality.Bad; } } else if (tag.TagType == "string") { OperateResult <string> res = new OperateResult <string>(); if (tag.Address.Contains("#")) { string[] adds = tag.Address.Split('#'); string address = adds[0]; ushort len = Convert.ToUInt16(adds[1]); res = PLC.ReadString(adds[0], len); } else { res = PLC.ReadString(tag.Address, 1); } if (res.IsSuccess) { tag.TagValue = res.Content; tag.Quality = Quality.Good; } else { tag.Quality = Quality.Bad; } } else if (tag.TagType == "datetime") //西门子Date_And_Time { OperateResult <byte[]> res = PLC.Read(tag.Address, 8); if (res.IsSuccess && res.Content.Length == 8) { var time = GetDateTime(res.Content, out bool isSuccess); if (isSuccess) { tag.TagValue = time; tag.Quality = Quality.Good; } } else { tag.Quality = Quality.Bad; } } else { var len = ConvertUtils.GetLength(tag); OperateResult <byte[]> res = PLC.Read(tag.Address, len); ConvertUtils.DecodeTagValue(tag, res, true); } return(tag.TagValue); } catch (Exception ex) { LOG.Error($"Datasource[{SourceName}] read error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]"); tag.Quality = Quality.Bad; return(tag.TagValue); } } else { return(null); } }
private void button_read_bool_Click(object sender, EventArgs e) { ReadResultRender(m_siemensTcpNet.ReadBool(txtAddress.Text), txtAddress.Text, txtResult); }
public override IEnumerable <IOTData> GetData() { List <IOTData> iOTs = new List <IOTData>(); foreach (S7NetResult result in DriveConfig.Results) { try { string sResult; switch (result.DataType.ToUpper()) { case "BOOL": sResult = siemensTcpNet.ReadBool(result.DB).Content.ToString(); break; case "STRING": sResult = siemensTcpNet.ReadString(result.DB, Convert.ToUInt16(result.Len)).Content; break; case "INT": sResult = siemensTcpNet.ReadInt32(result.DB).Content.ToString(); break; case "FLOAT": sResult = siemensTcpNet.ReadFloat(result.DB).Content.ToString(result.Format); break; case "DOUBLE": sResult = siemensTcpNet.ReadDouble(result.DB).Content.ToString(result.Format); break; case "BYTE": sResult = siemensTcpNet.ReadByte(result.DB).Content.ToString(); break; case "SHORT": sResult = siemensTcpNet.ReadInt16(result.DB).Content.ToString(); break; case "USHORT": sResult = siemensTcpNet.ReadUInt16(result.DB).Content.ToString(); break; case "UINT": sResult = siemensTcpNet.ReadUInt32(result.DB).Content.ToString(); break; case "LONG": sResult = siemensTcpNet.ReadInt64(result.DB).Content.ToString(); break; case "ULONG": sResult = siemensTcpNet.ReadUInt64(result.DB).Content.ToString(); break; case "DATETIME": sResult = siemensTcpNet.ReadDateTime(result.DB).Content.ToString(); break; default: sResult = siemensTcpNet.ReadString(result.DB).Content; break; } iOTs.Add(new IOTData { DataCode = result.Address, DataValue = sResult, DataName = result.Name, DriveCode = DriveConfig.DriveCode, DriveType = DriveConfig.DriveType, GTime = DateTime.Now, Unit = result.Unit }); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return(iOTs); }