private void button_read_bool_Click(object sender, EventArgs e) { // 读取bool变量 if (textBox14.Text == "1") { DemoUtils.ReadResultRender(allenBradleyNet.ReadBool(textBox3.Text), textBox3.Text, textBox4); } else { DemoUtils.ReadResultRender(allenBradleyNet.ReadBool(textBox3.Text, ushort.Parse(textBox14.Text)), textBox3.Text, textBox4); } }
/// <summary> /// 读取数据 /// </summary> /// <param name="dataAddress"></param> /// <returns></returns> public OperateResult <string> SyncReadData(string dataAddress) { if (string.IsNullOrEmpty(dataAddress) || (dataAddress.Length <= 1)) { return(new OperateResult <string>("传入的参数都不能为空")); } if (ServerIP == null) { return(new OperateResult <string>("设备未初始化,请先初始化。")); } try { AllenBradleyNet allenBradleyNet = new AllenBradleyNet(); allenBradleyNet.IpAddress = ServerIP.ToString(); allenBradleyNet.Port = ServerPort; OperateResult connect = allenBradleyNet.ConnectServer(); if (!connect.IsSuccess) { return(new OperateResult <string>("PLC连接失败")); } //todo:暂时支持bool类型读数据 OperateResult <string> read = new OperateResult <string>(); if (allenBradleyNet.ReadBool(dataAddress).Content) { read = OperateResult.CreateSuccessResult("1"); } else { read = OperateResult.CreateSuccessResult("0"); } allenBradleyNet.ConnectClose(); return(read); } catch (Exception ex) { string error = $"读取 设备IP({ServerIP}) 地址({dataAddress}) 失败,错误为({ex.Message.ToString()})"; Console.WriteLine(error); return(new OperateResult <string>(error)); } }
public static object ABRead(AllenBradleyNet plc, string address, string dataType, ushort dataLength) { lock (_lock) { object value = new object(); switch (dataType) { case "int": value = plc.ReadInt32(address).Content; break; case "bool": value = plc.ReadBool(address).Content; break; case "float": value = plc.ReadFloat(address).Content; break; case "double": value = plc.ReadDouble(address).Content; break; case "string": value = plc.ReadString(address, dataLength).Content; break; case "short": value = plc.ReadInt16(address).Content; break; case "long": value = plc.ReadInt64(address).Content; break; default: break; } return(value); } }
private void button_read_bool_Click(object sender, EventArgs e) { // 读取bool变量 readResultRender(allenBradleyNet.ReadBool(textBox3.Text), textBox3.Text, textBox4); }
private void ShowDataRoots( ) { if (rootTags != null) { foreach (var item in rootTags) { // 不是结构体及数组的情况则给予显示 if (!item.IsStruct) { if (item.ArrayDimension == 0) { if (item.SymbolType == AllenBradleyHelper.CIP_Type_Byte) { OperateResult <byte> read = allenBradleyNet.ReadByte(item.Name); if (read.IsSuccess) { item.Tag = (sbyte)read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Word) { OperateResult <short> read = allenBradleyNet.ReadInt16(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_DWord) { OperateResult <int> read = allenBradleyNet.ReadInt32(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_LInt) { OperateResult <long> read = allenBradleyNet.ReadInt64(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_USInt) { OperateResult <byte> read = allenBradleyNet.ReadByte(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_UInt) { OperateResult <ushort> read = allenBradleyNet.ReadUInt16(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_UDint) { OperateResult <uint> read = allenBradleyNet.ReadUInt32(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_ULint) { OperateResult <ulong> read = allenBradleyNet.ReadUInt64(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Bool) { OperateResult <bool> read = allenBradleyNet.ReadBool(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Real) { OperateResult <float> read = allenBradleyNet.ReadFloat(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Double) { OperateResult <double> read = allenBradleyNet.ReadDouble(item.Name); if (read.IsSuccess) { item.Tag = read.Content; } } } else if (item.ArrayDimension == 1) { if (item.SymbolType == AllenBradleyHelper.CIP_Type_Byte) { OperateResult <byte[]> read = allenBradleyNet.Read(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { sbyte[] data = new sbyte[read.Content.Length]; for (int i = 0; i < data.Length; i++) { data[i] = (sbyte)read.Content[i]; } item.Tag = data.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Word) { OperateResult <short[]> read = allenBradleyNet.ReadInt16(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_DWord) { OperateResult <int[]> read = allenBradleyNet.ReadInt32(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_LInt) { OperateResult <long[]> read = allenBradleyNet.ReadInt64(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_USInt) { OperateResult <byte[]> read = allenBradleyNet.Read(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_UInt) { OperateResult <ushort[]> read = allenBradleyNet.ReadUInt16(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_UDint) { OperateResult <uint[]> read = allenBradleyNet.ReadUInt32(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_ULint) { OperateResult <ulong[]> read = allenBradleyNet.ReadUInt64(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Real) { OperateResult <float[]> read = allenBradleyNet.ReadFloat(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_Double) { OperateResult <double[]> read = allenBradleyNet.ReadDouble(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = read.Content.ToArrayString( ); } } else if (item.SymbolType == AllenBradleyHelper.CIP_Type_D3) { OperateResult <int[]> read = allenBradleyNet.ReadInt32(item.Name, (ushort)item.ArrayLength[0]); if (read.IsSuccess) { item.Tag = SoftBasic.BoolArrayToString(allenBradleyNet.ByteTransform.TransByte(read.Content).ToBoolArray( )); } } } } } } }
public OperateResult <string> Read(AllenBradleyDataConfig dataConfig) { try { AllenBradleyNet allenBradleyNet = new AllenBradleyNet(); allenBradleyNet.IpAddress = IP; allenBradleyNet.Port = Port; OperateResult connect = allenBradleyNet.ConnectServer(); if (!connect.IsSuccess) { return(new OperateResult <string>("PLC连接失败")); } OperateResult <string> isRead = new OperateResult <string> { IsSuccess = false }; switch (dataConfig.DataType) { case AllenBradleyDataTypeEnum.BOOL: var bRes = allenBradleyNet.ReadBool(dataConfig.DataAdr); if (bRes.IsSuccess == true) { isRead.IsSuccess = true; isRead.Content = bRes.Content.ToString(); } break; case AllenBradleyDataTypeEnum.BYTE: var cRes = allenBradleyNet.ReadByte(dataConfig.DataAdr); if (cRes.IsSuccess == true) { isRead.IsSuccess = true; isRead.Content = cRes.Content.ToString(); } break; case AllenBradleyDataTypeEnum.SHORT: var ui16Res = allenBradleyNet.ReadUInt16(dataConfig.DataAdr); if (ui16Res.IsSuccess == true) { isRead.IsSuccess = true; isRead.Content = ui16Res.Content.ToString(); } break; case AllenBradleyDataTypeEnum.INT: var ui32Res = allenBradleyNet.ReadUInt32(dataConfig.DataAdr); if (ui32Res.IsSuccess == true) { isRead.IsSuccess = true; isRead.Content = ui32Res.Content.ToString(); } break; case AllenBradleyDataTypeEnum.LONG: var ui64Res = allenBradleyNet.ReadUInt64(dataConfig.DataAdr); if (ui64Res.IsSuccess == true) { isRead.IsSuccess = true; isRead.Content = ui64Res.Content.ToString(); } break; default: isRead.IsSuccess = false; break; } allenBradleyNet.ConnectClose(); return(isRead); } catch { return(new OperateResult <string>("AB PLC通讯发生致命错误")); } }