public OperateResult Write(AllenBradleyDataConfig dataConfig, string dataValue) { try { AllenBradleyNet allenBradleyNet = new AllenBradleyNet(); allenBradleyNet.IpAddress = IP; allenBradleyNet.Port = Port; OperateResult connect = allenBradleyNet.ConnectServer(); if (!connect.IsSuccess) { return(new OperateResult <string>("AB PLC连接失败")); } OperateResult isWrite; switch (dataConfig.DataType) { case AllenBradleyDataTypeEnum.BOOL: isWrite = allenBradleyNet.Write(dataConfig.DataAdr, bool.Parse(dataValue)); break; case AllenBradleyDataTypeEnum.BYTE: isWrite = allenBradleyNet.Write(dataConfig.DataAdr, byte.Parse(dataValue)); break; case AllenBradleyDataTypeEnum.SHORT: isWrite = allenBradleyNet.Write(dataConfig.DataAdr, short.Parse(dataValue)); break; case AllenBradleyDataTypeEnum.INT: isWrite = allenBradleyNet.Write(dataConfig.DataAdr, int.Parse(dataValue)); break; case AllenBradleyDataTypeEnum.LONG: isWrite = allenBradleyNet.Write(dataConfig.DataAdr, long.Parse(dataValue)); break; default: isWrite = new OperateResult { IsSuccess = false }; break; } if (!isWrite.IsSuccess) { return(new OperateResult <string>("AB PLC写入失败")); } allenBradleyNet.ConnectClose(); return(OperateResult.CreateSuccessResult()); } catch { return(new OperateResult <string>("AB PLC通讯发生致命错误")); } }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show(DemoUtils.IpAddressInputWrong); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(textBox15.Text, out byte slot)) { MessageBox.Show(DemoUtils.SlotInputWrong); return; } allenBradleyNet.IpAddress = textBox1.Text; allenBradleyNet.Port = port; allenBradleyNet.Slot = slot; if (!string.IsNullOrEmpty(textBox16.Text)) { allenBradleyNet.PortSlot = HslCommunication.BasicFramework.SoftBasic.HexStringToBytes(textBox16.Text); } try { OperateResult connect = allenBradleyNet.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; userControlReadWriteOp1.SetReadWriteNet(allenBradleyNet, "A1", true); } else { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.ToMessageShowString( )); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show(DemoUtils.IpAddressInputWrong); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(textBox15.Text, out byte slot)) { MessageBox.Show(DemoUtils.SlotInputWrong); return; } allenBradleyNet?.ConnectClose( ); allenBradleyNet = new AllenBradleyNet( ); allenBradleyNet.IpAddress = textBox1.Text; allenBradleyNet.Port = port; allenBradleyNet.Slot = slot; //if (!string.IsNullOrEmpty( textBox16.Text )) //{ // allenBradleyNet.PortSlot = HslCommunication.BasicFramework.SoftBasic.HexStringToBytes( textBox16.Text ); //} OperateResult connect = allenBradleyNet.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; panel3.Enabled = true; TagRefresh( ); } else { MessageBox.Show(StringResources.Language.ConnectedFailed + connect.ToMessageShowString( )); } }
public override void DeviceConn(AllenBradleyNetConfig config) { AllenBradleyNet allenBradleyNet = new AllenBradleyNet(); allenBradleyNet.IpAddress = config.IP; allenBradleyNet.Port = config.Port; allenBradleyNet.Slot = config.Slot; allenBradleyNet.PortSlot = config.PortSlot; OperateResult connect = allenBradleyNet.ConnectServer(); NetworkDevice = allenBradleyNet; if (!connect.IsSuccess) { throw new Exception("Connect Failed"); } }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show(DemoUtils.IpAddressInputWrong); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(textBox15.Text, out byte slot)) { MessageBox.Show(DemoUtils.SlotInputWrong); return; } allenBradleyNet.IpAddress = textBox1.Text; allenBradleyNet.Port = port; allenBradleyNet.Slot = slot; try { OperateResult connect = allenBradleyNet.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(Communication.StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; userControlCurve1.ReadWriteNet = allenBradleyNet; } else { MessageBox.Show(Communication.StringResources.Language.ConnectedFailed + connect.ToMessageShowString( )); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show("Ip地址输入不正确!"); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show("端口号输入不正确!"); return; } if (!byte.TryParse(textBox15.Text, out byte slot)) { MessageBox.Show("slot输入不正确!"); return; } allenBradleyNet.IpAddress = textBox1.Text; allenBradleyNet.Port = port; allenBradleyNet.Slot = slot; try { OperateResult connect = allenBradleyNet.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show("连接成功!"); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; } else { MessageBox.Show("连接失败!" + connect.ToMessageShowString( )); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <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)); } }
private void button1_Click(object sender, EventArgs e) { if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(textBox15.Text, out byte slot)) { MessageBox.Show(DemoUtils.SlotInputWrong); return; } dictStructDefine = new Dictionary <int, AbTagItem[]>( ); allenBradleyNet?.ConnectClose( ); allenBradleyNet = new AllenBradleyNet( ); allenBradleyNet.IpAddress = textBox1.Text; allenBradleyNet.Port = port; allenBradleyNet.Slot = slot; if (!string.IsNullOrEmpty(textBox16.Text)) { allenBradleyNet.MessageRouter = new MessageRouter(textBox16.Text); } OperateResult connect = allenBradleyNet.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; panel3.Enabled = true; //allenBradleyNet.LogNet = new HslCommunication.LogNet.LogNetFileSize( "" ); //allenBradleyNet.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; TagRefresh( ); } else { MessageBox.Show(StringResources.Language.ConnectedFailed + connect.ToMessageShowString( )); } }
/// <summary> /// heart beat /// </summary> private void SetLifebeat_AB(object objLine) { var line = objLine as Xml_InfoConfig; AllenBradleyNet plcLifebeat = null; try { plcLifebeat = new AllenBradleyNet(line.IP) { OffLine = XML_Tool.xml.SysConfig.offlineTest, }; plcLifebeat.ConnectServer(); var value = false; while (true) { if (value) { setStatusEvent(line.Name, 1); } else { setStatusEvent(line.Name, 0); } ABWrite(abPlc[line.Index], line.HeartBeatAddr, value.ToString(), line.AddrType, 0); value = !value; Thread.Sleep(1000); } } catch (Exception ex) { recodePLCStationMessage(string.Empty, line.Name + " Write heartbeat error:" + ex.Message, 1, true); recodePLCStationMessage(string.Empty, line.Name + " The connection is broken and an attempt is made to reconnect...", 0, true); if (plcLifebeat != null) { plcLifebeat.ConnectClose(); } Thread.Sleep(2000); new Thread(new ParameterizedThreadStart(ConnectABPLC)) { IsBackground = true }.Start(line); } }
/// <summary> /// 同步写数据,支持按照位写 /// </summary> /// <param name="dataAddress"></param> /// <param name="dataValues"></param> /// <returns></returns> public OperateResult SyncWriteData(string dataAddress, string dataValues) { string dataValue = dataValues.Split('|')[0]; 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:读数据 OperateResult isWrite = allenBradleyNet.Write(dataAddress, Int16.Parse(dataValue)); if (!isWrite.IsSuccess) { return(new OperateResult <string>("PLC写入失败")); } allenBradleyNet.ConnectClose(); return(OperateResult.CreateSuccessResult()); } catch (Exception ex) { string error = $"读取 设备IP({ServerIP}) 地址({dataAddress}) 失败,错误为({ex.Message.ToString()})"; Console.WriteLine(error); return(new OperateResult <string>(error)); } }
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通讯发生致命错误")); } }