找传奇、传世资源到传世资源站!

C#基恩斯通信实例(PLC)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

DLL Testing for KEYENCE nK EtherNet TCP
from clipboardusing System.Collections.Generic;using System;using System.Linq;using System.Drawing;using System.Diagnostics;using System.Data;using System.Xml.Linq;using Microsoft.VisualBasic;using System.Collections;using System.Windows.Forms;using System.Net;using System.Runtime.InteropServices;namespace TCP_CLIENT{public partial class Form1{public Form1(){InitializeComponent();if (defaultInstance == null)defaultInstance = this;}#region Default Instanceprivate static Form1 defaultInstance;public static Form1 Default{get{if (defaultInstance == null){defaultInstance = new Form1();defaultInstance.FormClosed = new FormClosedEventHandler(defaultInstance_FormClosed);}return defaultInstance;}set{defaultInstance = value;}}static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e){defaultInstance = null;}#endregionKvEntPlc.PlcClient PLC = new KvEntPlc.PlcClient();bool EntLink;long ScanCount;short ScanRet; Int32 PlcHand;[DllImport("winmm.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]public static extern UInt32 timeGetTime();public void Form1_Load(System.Object sender, System.EventArgs e){short i = 0;this.CenterToScreen();cmbReadMry.Items.Clear();cmbReadMry.Items.Add("R");cmbReadMry.Items.Add("MR");cmbReadMry.Items.Add("DM"); cmbReadMry.Items.Add("EM");cmbWriteMry.Items.Clear();cmbWriteMry.Items.Add("R");cmbWriteMry.Items.Add("MR");cmbWriteMry.Items.Add("DM"); cmbWriteMry.Items.Add("EM");cmbBitMry.Items.Clear();cmbBitMry.Items.Add("R");cmbBitMry.Items.Add("MR");cmbBitMry.Items.Add("DM"); cmbBitMry.Items.Add("EM");cmbReadType.Items.Clear();cmbReadType.Items.Add("INT16");cmbReadType.Items.Add("UINT16");cmbReadType.Items.Add("DINT32");cmbReadType.Items.Add("HEX32");cmbReadType.Items.Add("REAL32");cmbReadType.Items.Add("BIN16");cmbWriteType.Items.Clear();cmbWriteType.Items.Add("INT16");cmbWriteType.Items.Add("UINT16");cmbWriteType.Items.Add("DINT32");cmbWriteType.Items.Add("HEX32");cmbWriteType.Items.Add("REAL32");cmbWriteType.Items.Add("BIN16");for (i = 0; i <= 15; i ){cmbBit.Items.Add("Bit" System.Convert.ToString(i));}cmbReadMry.SelectedIndex = 2;cmbWriteMry.SelectedIndex = 2;cmbBitMry.SelectedIndex = 1;cmbBit.SelectedIndex = 0;cmbReadType.SelectedIndex = 0;cmbWriteType.SelectedIndex = 0;lstRead.Items.Clear();txtWrite.Text = "";}public void butLink_Click(System.Object sender, System.EventArgs e){short re = 0;string restr = ""; re = PLC.EntLink(txtLocalIP.Text.Trim(), Convert.ToUInt16(txtLocalPort.Text), txtRemoteIP.Text.Trim(), (Convert.ToUInt16(txtRemotePort.Text)), "DEMO", ref PlcHand); txtReLink.Text = re.ToString();if (re == 0){EntLink = true;MessageBox.Show("PLC联接成功!");}else{EntLink = false;MessageBox.Show("PLC联接失败: " restr);}}public void butClose_Click(System.Object sender, System.EventArgs e){short re = 0; re = PLC.DeLink(PlcHand);txtReClose.Text = re.ToString(); EntLink = false;}public void butRead_Click(System.Object sender, System.EventArgs e){short i = 0;object[] RD = null;RD = new object[Convert.ToUInt16(txtReadCnt.Text)];if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}int var1 = cmbReadType.SelectedIndex 1; KvEntPlc.PlcClient.DataType typ = (KvEntPlc .PlcClient.DataType)var1;switch (cmbReadMry.SelectedIndex){case 0: ScanRet = PLC.CmdRead(PlcHand,KvEntPlc.PlcClient.PlcMemory.CIO, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;case 1: ScanRet = PLC.CmdRead(PlcHand, KvEntPlc.PlcClient.PlcMemory.MR, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;case 2: ScanRet = PLC.CmdRead(PlcHand, KvEntPlc.PlcClient.PlcMemory.DR, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;case 3: ScanRet = PLC.CmdRead(PlcHand, KvEntPlc.PlcClient.PlcMemory.ER, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;}txtReRead.Text = ScanRet.ToString();lstRead.Items.Clear();for (i = 0; i <= (RD.Length - 1); i ){if (!(RD[i] == null)){lstRead.Items.Add(RD[i]);}else{lstRead.Items.Add("0");}}}public void butWrite_Click(System.Object sender, System.EventArgs e){short i = 0;string[] temp = null;object[] WD = null;if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}WD = new object[Convert.ToUInt16(txtWriteCnt.Text)];temp = txtWrite.Text.Split('\n');for (i = 0; i <= (WD.Length - 1); i ){if (i > (temp.Length - 1)){WD[i] = 0;}else{WD[i] = temp[i].Trim();}}int var1 = cmbWriteType.SelectedIndex 1; KvEntPlc.PlcClient.DataType typ = (KvEntPlc.PlcClient.DataType)var1;switch (cmbWriteMry.SelectedIndex){case 0: ScanRet = PLC.CmdWrite(PlcHand, KvEntPlc.PlcClient.PlcMemory.CIO, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;case 1: ScanRet = PLC.CmdWrite(PlcHand, KvEntPlc.PlcClient.PlcMemory.MR, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;case 2: ScanRet = PLC.CmdWrite(PlcHand, KvEntPlc.PlcClient.PlcMemory.DR, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;case 3: ScanRet = PLC.CmdWrite(PlcHand, KvEntPlc.PlcClient.PlcMemory.ER, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;}txtReWrite.Text = ScanRet.ToString();}public void butScan_Click(System.Object sender, System.EventArgs e){if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}if (butScan.Text == "Cycle R/W"){ScanCount = 0;Timer1.Enabled = true;butScan.Text = "Stop R/W";}else{Timer1.Enabled = false;butScan.Text = "Cycle R/W";}}public void Timer1_Tick(System.Object sender, System.EventArgs e){ Timer1.Enabled = false; int tim = System.Convert.ToInt32(timeGetTime()); if (!EntLink) { MessageBox.Show("还未与PLC建立联接!"); return; } // butRead_Click(null, null); butWrite_Click(null, null); // if ((Convert.ToInt32(txtReRead.Text) < 0) || (Convert.ToInt32(txtReWrite.Text) < 0)) { butScan.Text = "Cycle R/W"; return; } else { ScanCount ; txtScanCnt.Text = ScanCount.ToString(); txtScanPrd.Text = (System.Convert.ToInt32(timeGetTime()) - tim).ToString() "ms"; } Timer1.Enabled = true;}public void butBitTest_Click(System.Object sender, System.EventArgs e){if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}bool rd = false;short re = 0;switch (cmbBitMry.SelectedIndex){case 0: re = PLC.Bit_Test(PlcHand, KvEntPlc.PlcClient.PlcMemory.CIO, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break;case 1: re = PLC.Bit_Test(PlcHand, KvEntPlc.PlcClient.PlcMemory.MR, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break;case 2: re = PLC.Bit_Test(PlcHand, KvEntPlc.PlcClient.PlcMemory.DR, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break; case 3: re = PLC.Bit_Test(PlcHand, KvEntPlc.PlcClient.PlcMemory.ER, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd); break;}txtBitTest.Text = rd.ToString();txtReBit.Text = re.ToString();}public void butBitSet_Click(System.Object sender, System.EventArgs e){if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}short re = 0;switch (cmbBitMry.SelectedIndex){case 0: re = PLC.Bit_Set(PlcHand, KvEntPlc.PlcClient.PlcMemory.CIO, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 1: re = PLC.Bit_Set(PlcHand, KvEntPlc.PlcClient.PlcMemory.MR, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 2: re = PLC.Bit_Set(PlcHand, KvEntPlc.PlcClient.PlcMemory.DR, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break; case 3: re = PLC.Bit_Set(PlcHand, KvEntPlc.PlcClient.PlcMemory.ER , Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex)); break;}txtReBit.Text = re.ToString();}public void butBitRst_Click(System.Object sender, System.EventArgs e){if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}short re = 0;switch (cmbBitMry.SelectedIndex){case 0: re = PLC.Bit_Reset(PlcHand, KvEntPlc.PlcClient.PlcMemory.CIO, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 1: re = PLC.Bit_Reset(PlcHand, KvEntPlc.PlcClient.PlcMemory.MR, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 2: re = PLC.Bit_Reset(PlcHand, KvEntPlc.PlcClient.PlcMemory.DR, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break; case 3: re = PLC.Bit_Reset(PlcHand, KvEntPlc.PlcClient.PlcMemory.ER, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex)); break;}txtReBit.Text = re.ToString();}}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复