using 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.Runtime.InteropServices;namespace ComTest{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;}#endregionComQsPlc.QsCom PLC = new ComQsPlc.QsCom();ComQsPlc.QsCom.PlcMemory X = ComQsPlc.QsCom.PlcMemory.DI;ComQsPlc.QsCom.PlcMemory Y = ComQsPlc.QsCom.PlcMemory.DQ;ComQsPlc.QsCom.PlcMemory M = ComQsPlc.QsCom.PlcMemory.MR;ComQsPlc.QsCom.PlcMemory D = ComQsPlc.QsCom.PlcMemory.DR; ComQsPlc.QsCom.PlcMemory R = ComQsPlc.QsCom.PlcMemory.RR;bool EntLink;long ScanCount;short ScanRet;bool ScanRun;[DllImport("winmm.dll", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]public static extern long timeGetTime();public void Form1_Load(System.Object sender, System.EventArgs e){short i = 0;this.CenterToScreen();cmbReadMry.Items.Add("X");cmbReadMry.Items.Add("Y");cmbReadMry.Items.Add("M");cmbReadMry.Items.Add("D"); cmbReadMry.Items.Add("R");cmbWriteMry.Items.Add("X");cmbWriteMry.Items.Add("Y");cmbWriteMry.Items.Add("M");cmbWriteMry.Items.Add("D"); cmbWriteMry.Items.Add("R");cmbBitMry.Items.Add("X");cmbBitMry.Items.Add("Y");cmbBitMry.Items.Add("M");cmbBitMry.Items.Add("D"); cmbBitMry.Items.Add("R");for (i = 0; i <= 15; i ){cmbBit.Items.Add("Bit" System.Convert.ToString(i));}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");cmbReadMry.SelectedIndex = 2;cmbWriteMry.SelectedIndex = 2;cmbBitMry.SelectedIndex = 0;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.ComLink(Convert.ToUInt16(txtPort.Text), Convert.ToUInt32(txtRate.Text), 8, 1, ComQsPlc.QsCom.ParityType.Odd, Convert.ToUInt16(txtDelay.Text), "DEMO");txtReLink.Text = re.ToString();if (re == 0){EntLink = true;MessageBox.Show("PLC联接成功: " restr);}else{EntLink = false;MessageBox.Show("PLC联接失败: " restr);}}public void butClose_Click(System.Object sender, System.EventArgs e){short re = 0;if (!EntLink){MessageBox.Show("还未与PLC建立联接!");//return;}re = PLC.DeLink();txtReClose.Text = re.ToString();}public void butRead_Click(System.Object sender, System.EventArgs e){short i = 0;object[] RD = null;RD = new object[Convert.ToInt16(txtReadCnt.Text)];if (!EntLink){MessageBox.Show("还未与PLC建立联接!");//return;}int var1 = cmbReadType.SelectedIndex 1; ComQsPlc.QsCom.DataType typ = (ComQsPlc.QsCom.DataType)var1;switch (cmbReadMry.SelectedIndex){case 0: ScanRet = PLC.CmdRead(Convert.ToUInt16(txtStation.Text), X, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;case 1: ScanRet = PLC.CmdRead(Convert.ToUInt16(txtStation.Text), Y, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;case 2: ScanRet = PLC.CmdRead(Convert.ToUInt16(txtStation.Text), M, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break;case 3: ScanRet = PLC.CmdRead(Convert.ToUInt16(txtStation.Text), D, typ, Convert.ToUInt16(txtReadAdd.Text), Convert.ToUInt16(txtReadCnt.Text), ref RD);break; case 4: ScanRet = PLC.CmdRead(Convert.ToUInt16(txtStation.Text), R, 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;ComQsPlc.QsCom.DataType typ =(ComQsPlc.QsCom.DataType) var1;switch (cmbWriteMry.SelectedIndex){case 0: ScanRet = PLC.CmdWrite(Convert.ToUInt16(txtStation.Text), X, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;case 1: ScanRet = PLC.CmdWrite(Convert.ToUInt16(txtStation.Text), Y, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;case 2: ScanRet = PLC.CmdWrite(Convert.ToUInt16(txtStation.Text), M, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break;case 3: ScanRet = PLC.CmdWrite(Convert.ToUInt16(txtStation.Text), D, typ, Convert.ToUInt16(txtWriteAdd.Text), Convert.ToUInt16(txtWriteCnt.Text), ref WD);break; case 4: ScanRet = PLC.CmdWrite(Convert.ToUInt16(txtStation.Text), R, 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;ScanRun = true;Timer1.Enabled = true;butScan.Text = "Stop R/W";}else{ScanRun = false;Timer1.Enabled = false;butScan.Text = "Cycle R/W";}}public void Timer1_Tick(System.Object sender, System.EventArgs e){Timer1.Enabled = false;if (!ScanRun){butScan_Click(null, null);return;}int tim = System.Convert.ToInt32(timeGetTime());if (!EntLink){MessageBox.Show("还未与PLC建立联接!");return;}//butRead_Click(null, null);if (ScanRet < 0){ScanRun = false;return;}butWrite_Click(null, null);if (ScanRet < 0){ScanRun = false;return;}//ScanCount ;txtScanCnt.Text = ScanCount.ToString();txtScanPrd.Text = (System.Convert.ToInt32(timeGetTime()) - tim) "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(Convert.ToUInt16(txtStation.Text), X, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break;case 1:re = PLC.Bit_Test(Convert.ToUInt16(txtStation.Text), Y, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break;case 2:re = PLC.Bit_Test(Convert.ToUInt16(txtStation.Text), M, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break;case 3:re = PLC.Bit_Test(Convert.ToUInt16(txtStation.Text), D, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd);break; case 4: re = PLC.Bit_Test(Convert.ToUInt16(txtStation.Text), R, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex), ref rd); break;}txtBitTest.Text = System.Convert.ToString(rd);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(Convert.ToUInt16(txtStation.Text), X, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 1:re = PLC.Bit_Set(Convert.ToUInt16(txtStation.Text), Y, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 2:re = PLC.Bit_Set(Convert.ToUInt16(txtStation.Text), M, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 3:re = PLC.Bit_Set(Convert.ToUInt16(txtStation.Text), D, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break; case 4: re = PLC.Bit_Set(Convert.ToUInt16(txtStation.Text), R, 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(Convert.ToUInt16(txtStation.Text), X, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 1:re = PLC.Bit_Reset(Convert.ToUInt16(txtStation.Text), Y, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 2:re = PLC.Bit_Reset(Convert.ToUInt16(txtStation.Text), M, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex));break;case 3:re = PLC.Bit_Reset(Convert.ToUInt16(txtStation.Text), D, Convert.ToUInt16(txtBitAdd.Text),Convert.ToUInt16( cmbBit.SelectedIndex));break; case 4: re = PLC.Bit_Reset(Convert.ToUInt16(txtStation.Text), R, Convert.ToUInt16(txtBitAdd.Text), Convert.ToUInt16(cmbBit.SelectedIndex)); break;}txtReBit.Text = re.ToString();}}}
下载VS2010与三菱Qplc案例用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论