步驟,先連接PLC,然后在填入對應的點位 D10 然后去讀取。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using ActUtlTypeLib;namespace MX_Component{public partial class Form1 : Form{//傳教Utl控件private ActUtlType plc;public Form1(){InitializeComponent();plc = new ActUtlType();guna2Button2.Enabled = false;guna2Button3.Enabled = false;guna2Button4.Enabled = false;}//連接plcprivate void guna2Button1_Click(object sender, EventArgs e){int iReturnCode;int iLogicalStationNumber;if (guna2TextBox1.Text == ""){MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}iLogicalStationNumber = Convert.ToInt32(this.guna2TextBox1.Text);try{plc.ActLogicalStationNumber = iLogicalStationNumber;//輸入站號iReturnCode = plc.Open();//打開連接//設置控件if (iReturnCode == 0){this.guna2Button2.Enabled = true;this.guna2Button1.Enabled = false;guna2TextBox1.Enabled = false;guna2TextBox2.Enabled = false;guna2Button3.Enabled = true;guna2Button4.Enabled = true;}}catch (Exception ex){MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}}//關閉連接private void guna2Button2_Click(object sender, EventArgs e){int iReturnCode;try{iReturnCode = plc.Close();//關閉連接if (iReturnCode == 0){this.guna2Button2.Enabled = false;this.guna2Button1.Enabled = true;guna2TextBox1.Enabled = true;guna2TextBox2.Enabled = true;guna2Button3.Enabled = false;guna2Button4.Enabled = false;}}catch (Exception ex){MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}}//軟元件批量讀取private void guna2Button3_Click(object sender, EventArgs e){int iReturnCode;string szDeviceName;int iNumberOfData;short[] arrDeviceValue;string[] arrData;try{if (guna2TextBox4.Text == null || guna2TextBox6.Text == null)//判斷TextBox是否為空{MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}szDeviceName = string.Join("\n", guna2TextBox4.Lines);iNumberOfData = Convert.ToInt32(guna2TextBox6.Text);arrDeviceValue = new short[iNumberOfData];iReturnCode = plc.ReadDeviceBlock2(szDeviceName, iNumberOfData, out arrDeviceValue[0]);//讀取數據if (iReturnCode == 0)//判斷讀取結果{arrData = new string[iNumberOfData];//數據分行顯示到TextBox中for (int i = 0; i < iNumberOfData; i++){arrData[i] = arrDeviceValue[i].ToString();}guna2TextBox5.Lines = arrData;}}catch (Exception ex){MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}}//批量寫入數據private void guna2Button4_Click(object sender, EventArgs e){int iReturnCode;string szDeviceName;int iNumberOfData;short[] lpsData;if (guna2TextBox4.Text == null || guna2TextBox6.Text == null||guna2TextBox3.Text==null){MessageBox.Show("The TextBox has not been entered.", Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}szDeviceName = guna2TextBox4.Text;iNumberOfData = Convert.ToInt32(guna2TextBox6.Text);lpsData = new short[iNumberOfData];try{if (guna2TextBox3.Lines.Length!= iNumberOfData){MessageBox.Show("The element count of the array has to be the same as the size.",Text, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}//待寫數據轉為short[]for (int i = 0; i < iNumberOfData; i++){lpsData[i] = Convert.ToInt16( guna2TextBox3.Lines[i]);}iReturnCode = plc.WriteDeviceBlock2(szDeviceName, iNumberOfData, ref lpsData[0]);//寫入數據}catch (Exception ex){MessageBox.Show(ex.Message,Name, MessageBoxButtons.OK, MessageBoxIcon.Error);return;}}}
其他參考信息
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 存儲器的釋放