private void btnCon_Click(object sender, EventArgs e) { string s = btnCon.Text.Trim(); if (s == "连接") { string plcip = txtPlcip.Text.Trim(); string plcport = txtPlcport.Text.Trim(); ENT = new EtherNetPLC(); short re = ENT.Link(plcip, short.Parse(plcport), 500); if (re == 0) { btnCon.Text = "断开"; txtPCNode.Text = ENT.PCNode.ToString(); } else { MessageBox.Show("连接出错!"); } } else { short re = ENT.Close(); if (re == 0) { btnCon.Text = "连接"; txtPCNode.Text = "pcNode"; } else { MessageBox.Show("断开出错!"); } } }
public bool DisconnectToOmronPLC() { short bconnect = omronethernetplc.Close(); if (bconnect == 0) { bConnectOmronPLC = false; return(true); } else { //MessageBox.Show("断开出错!"); bConnectOmronPLC = false; return(false); } }
private (bool IsError, string ErrorMessage, List <Dose> Datas) GetPLCData() { var datas = new List <Dose>(); PlcMemory dm = PlcMemory.DM; short resultCountCells = 0; EtherNetPLC cp1 = new EtherNetPLC(); try { cp1.Link(_configApp.IpAddress, _configApp.PortAddress, 300); cp1.ReadWord(dm, COUNTER_CELLS_FOR_READING, out resultCountCells); if (resultCountCells > 0) { short total = (short)(resultCountCells / PACKEGES); //целое число пакетов (кратное числу возможному для приема пакетов за раз). short mod = (short)(resultCountCells % PACKEGES); //остаток пакетов от целого числа short[] data = new short[600]; for (int i = 0; i < total; i++) { cp1.ReadWords(dm, (short)(START_CELL + (i * PACKEGES * COUNT_DATAROWS)), (short)((PACKEGES * COUNT_DATAROWS)), out data); datas.AddRange(TransformArrayToDose(data).ToArray()); } if (mod > 0) { cp1.ReadWords(dm, (short)(START_CELL + ((short)total * PACKEGES * COUNT_DATAROWS)), (short)((mod * COUNT_DATAROWS)), out data); datas.AddRange(TransformArrayToDose(data).ToArray()); } cp1.WriteWord(dm, COUNTER_CELLS_FOR_READING, 0); } // cp1.WriteWord(dm, COUNTER_CELLS_FOR_READING, 0); return(false, "", datas.ToList()); } catch { return(true, "Connection to PLC didn't establish", null); } finally { cp1.Close(); } }
/// <summary> /// 关闭Fins通讯 /// </summary> public void CloseOmronFins() { mOmronFins.Close(); }