MX Component基础使用
完整代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using ActUtlTypeLib; 11 12 namespace MX_Component 13 { 14 public partial class Form1 : Form 15 { 16 //传教Utl控件 17 private ActUtlType plc; 18 public Form1() 19 { 20 InitializeComponent(); 21 plc = new ActUtlType(); 22 guna2Button2.Enabled = false; 23 guna2Button3.Enabled = false; 24 guna2Button4.Enabled = false; 25 } 26 //连接plc 27 private void guna2Button1_Click(object sender, EventArgs e) 28 { 29 int iReturnCode; 30 int iLogicalStationNumber; 31 if (guna2TextBox1.Text == "") 32 { 33 MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 34 return; 35 } 36 iLogicalStationNumber = Convert.ToInt32(this.guna2TextBox1.Text); 37 try 38 { 39 plc.ActLogicalStationNumber = iLogicalStationNumber;//输入站号 40 iReturnCode = plc.Open();//打开连接 41 //设置控件 42 if (iReturnCode == 0) 43 { 44 this.guna2Button2.Enabled = true; 45 this.guna2Button1.Enabled = false; 46 guna2TextBox1.Enabled = false; 47 guna2TextBox2.Enabled = false; 48 guna2Button3.Enabled = true; 49 guna2Button4.Enabled = true; 50 } 51 } 52 catch (Exception ex) 53 { 54 MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 55 return; 56 } 57 } 58 //关闭连接 59 private void guna2Button2_Click(object sender, EventArgs e) 60 { 61 int iReturnCode; 62 try 63 { 64 iReturnCode = plc.Close();//关闭连接 65 if (iReturnCode == 0) 66 { 67 this.guna2Button2.Enabled = false; 68 this.guna2Button1.Enabled = true; 69 guna2TextBox1.Enabled = true; 70 guna2TextBox2.Enabled = true; 71 guna2Button3.Enabled = false; 72 guna2Button4.Enabled = false; 73 } 74 } 75 catch (Exception ex) 76 { 77 MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 78 return; 79 } 80 } 81 82 //软元件批量读取 83 private void guna2Button3_Click(object sender, EventArgs e) 84 { 85 int iReturnCode; 86 string szDeviceName; 87 int iNumberOfData; 88 short[] arrDeviceValue; 89 string[] arrData; 90 try 91 { 92 if (guna2TextBox4.Text == null || guna2TextBox6.Text == null)//判断TextBox是否为空 93 { 94 MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 95 return; 96 } 97 szDeviceName = string.Join("\n", guna2TextBox4.Lines); 98 iNumberOfData = Convert.ToInt32(guna2TextBox6.Text); 99 arrDeviceValue = new short[iNumberOfData]; 100 iReturnCode = plc.ReadDeviceBlock2(szDeviceName, iNumberOfData, out arrDeviceValue[0]);//读取数据 101 if (iReturnCode == 0)//判断读取结果 102 { 103 arrData = new string[iNumberOfData]; 104 //数据分行显示到TextBox中 105 for (int i = 0; i < iNumberOfData; i++) 106 { 107 arrData[i] = arrDeviceValue[i].ToString(); 108 } 109 guna2TextBox5.Lines = arrData; 110 } 111 } 112 catch (Exception ex) 113 { 114 MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 115 return; 116 } 117 118 } 119 120 //批量写入数据 121 private void guna2Button4_Click(object sender, EventArgs e) 122 { 123 int iReturnCode; 124 string szDeviceName; 125 int iNumberOfData; 126 short[] lpsData; 127 128 if (guna2TextBox4.Text == null || guna2TextBox6.Text == null||guna2TextBox3.Text==null) 129 { 130 MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 131 return; 132 } 133 szDeviceName = guna2TextBox4.Text; 134 iNumberOfData = Convert.ToInt32(guna2TextBox6.Text); 135 lpsData = new short[iNumberOfData]; 136 try 137 { 138 if (guna2TextBox3.Lines.Length!= iNumberOfData) 139 { 140 MessageBox.Show("The element count of the array has to be the same as the size.", 141 Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 142 return; 143 } 144 //待写数据转为short[] 145 for (int i = 0; i < iNumberOfData; i++) 146 { 147 lpsData[i] = Convert.ToInt16( guna2TextBox3.Lines[i]); 148 } 149 iReturnCode = plc.WriteDeviceBlock2(szDeviceName, iNumberOfData, ref lpsData[0]);//写入数据 150 } 151 catch (Exception ex) 152 { 153 MessageBox.Show(ex.Message,Name, MessageBoxButtons.OK, MessageBoxIcon.Error); 154 return; 155 } 156 } 157 }
MX Component函数一览
Open | 通信线路的打开 |
Close | 通信线路的关闭 |
ReadDeviceBlock | 软元件的批量读取 (4字节数据) |
WriteDeviceBlock | 软元件的批量写入 (4字节数据) |
ReadDeviceRandom | 软元件的随机读取 (4字节数据) |
WriteDeviceRandom | 软元件的随机写入 (4字节数据) |
SetDevice | 软元件1点的设置 (4字节数据) |
GetDevice | 软元件1点的数据获取 (4字节数据) |
ReadBuffer | 缓冲存储器的读取 |
WriteBuffer | 缓冲存储器的写入 |
GetClockData | CPU模块的时钟数据读取 |
SetClockData | CPU模块的时钟数据写入 |
GetCpuType | CPU模块型号读取 |
SetCpuStatus | PU模块的远程RUN/STOP/PAUSE |
EntryDeviceStatus | 2 软元件的状态监视登录 |
FreeDeviceStatus | 软元件的状态监视登录的解除 |
OnDeviceStatus | 事件通知 |
ReadDeviceBlock2 | 软元件的批量读取 (2字节数据) |
WriteDeviceBlock2 | 软元件的批量写入 (2字节数据) |
ReadDeviceRandom2 | 软元件的随机读取 (2字节数据) |
WriteDeviceRandom2 | 软元件的随机写入 (2字节数据) |
SetDevice2 | 软元件1点的设置 (2字节数据) |
GetDevice2 | 软元件1点的数据获取 (2字节数据) |
GetErrorMessage | 错误内容及处理方法的获取 |
ReadFirstFile | 文件名/目录名的搜索 |
ReadNextFile | 文件名/目录名的搜索 |
ReadClose | 搜索的结束 |
GetFile | 记录文件的传送 |
Dispose | 存储器的释放 |
添加控件