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 Test2( ) { byte m100_byte = siemensTcpNet.ReadByte("M100").Content; short m100_short = siemensTcpNet.ReadInt16("M100").Content; ushort m100_ushort = siemensTcpNet.ReadUInt16("M100").Content; int m100_int = siemensTcpNet.ReadInt32("M100").Content; uint m100_uint = siemensTcpNet.ReadUInt32("M100").Content; float m100_float = siemensTcpNet.ReadFloat("M100").Content; double m100_double = siemensTcpNet.ReadDouble("M100").Content; string m100_string = siemensTcpNet.ReadString("M100", 10).Content; HslCommunication.Core.IByteTransform ByteTransform = new HslCommunication.Core.ReverseBytesTransform( ); }
public void ReadExample( ) { #region ReadExample1 SiemensS7Net siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110"); // 此处以M100寄存器作为示例 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字符串数据 // 读取数组 short[] short_M100_array = siemensTcpNet.ReadInt16("M100", 10).Content; // 读取M100-M101组成的字 ushort[] ushort_M100_array = siemensTcpNet.ReadUInt16("M100", 10).Content; // 读取M100-M101组成的无符号的值 int[] int_M100_array = siemensTcpNet.ReadInt32("M100", 10).Content; // 读取M100-M103组成的有符号的数据 uint[] uint_M100_array = siemensTcpNet.ReadUInt32("M100", 10).Content; // 读取M100-M103组成的无符号的值 float[] float_M100_array = siemensTcpNet.ReadFloat("M100", 10).Content; // 读取M100-M103组成的单精度值 long[] long_M100_array = siemensTcpNet.ReadInt64("M100", 10).Content; // 读取M100-M107组成的大数据值 ulong[] ulong_M100_array = siemensTcpNet.ReadUInt64("M100", 10).Content; // 读取M100-M107组成的无符号大数据 double[] double_M100_array = siemensTcpNet.ReadDouble("M100", 10).Content; // 读取M100-M107组成的双精度值 #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); }
private void btnReadByte_Click(object sender, EventArgs e) { if (!isConnected) { MessageBox.Show("还未连接 PLC"); return; } try { OperateResult <byte> rlt = plc.ReadByte($"DB{edtDBNumber.Text}.{edtOffset.Text}"); if (rlt.IsSuccess) { edtText.Text = rlt.Content.ToString(); } else { edtText.Text = $"{rlt.ToMessageShowString()}"; } } catch { edtText.Text = ""; } }
private void button_read_byte_Click(object sender, EventArgs e) { // 读取byte变量 readResultRender(siemensTcpNet.ReadByte(textBox3.Text), textBox3.Text, textBox4); }
/// <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 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); }
private void button_read_byte_Click(object sender, EventArgs e) { ReadResultRender(m_siemensTcpNet.ReadByte(txtAddress.Text), txtAddress.Text, txtResult); }
//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); }
/// <summary> /// 读取byte变量 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Read_Byte_Click(object sender, EventArgs e) { ReadResultRender(siemensTcpNet.ReadByte(txt_ReadSingle_Address.Text), txt_ReadSingle_Address.Text, txt_Result_Single); }