// FormSampleApp.cs : アプリケーション用クラスの機能定義ファイル // ///////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2023 MITSUBISHI Electric Corporation All Rights Reserved // 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 static SampleAppCS.ClassDefine; namespace SampleAppCS { public partial class FormSampleApp : Form { private EZNCAUTLib.DispEZNcCommunication oEZNcAutCom = null; private Boolean bConnectStatus = false; private Boolean bCommunicateStatus = false; //Open3の第4引数は"EZNC_LOCALHOST"固定 private String strHostName = "EZNC_LOCALHOST"; //コンストラクタ public FormSampleApp() { InitializeComponent(); //アプリ名の登録 this.Text = APPLICATION_NAME + this.Text; groupBoxErrorCode.Text = APPLICATION_NAME + groupBoxErrorCode.Text; //NCタイプの登録(C70/C80除く) foreach (String stType in Enum.GetNames(typeof(SYSTEMTYPE))) { if ((stType != "EZNC_SYS_MELDASC70") && (stType != "EZNC_SYS_CNCC80")) { comboBoxSystemType.Items.Add(stType); } } } //ErrorCodeの出力 private void CheckError(String strMethod, Int32 lResult) { if (lResult != 0) { Int32 lBase = 16; textBoxErrorCode.Text = strMethod + " failed. " + "0x" + Convert.ToString(lResult, lBase) + Environment.NewLine + textBoxErrorCode.Text; } } //「接続/切断」ボタン押下時の処理 private void ButtonConnect_Click(object sender, EventArgs e) { //M700/M800シリーズではポート番号に683という固定値を指定 Int32 lPort = 683; Int32 lNCType; Int32 lResult; Boolean bResult; String strIPAddress; //IPアドレスの取得 if (textBoxIPAddress.Text == "") { MessageBox.Show("IPアドレスが不正です。"); return; } else { strIPAddress = textBoxIPAddress.Text; } //NCタイプの取得 bResult = Enum.TryParse(comboBoxSystemType.Text, out SYSTEMTYPE sysNCType); if (bResult == false) { MessageBox.Show("NCタイプが不正です。"); return; } if (oEZNcAutCom == null) { //オブジェクトの作成 oEZNcAutCom = new EZNCAUTLib.DispEZNcCommunication(); //通信回線のオープン if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80) { //下記の設定は一例のため、接続環境に合わせた設定が必要 lResult = oEZNcAutCom.SetMelsecProtocol(0x00, 0xFF, 0x00, 0x00, 0x3E1, 0x1018, 0x1002, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, strIPAddress, 0x04, Int32.Parse(textBoxTimeOut.Text), 0x00, 0x00, 0x00, 5006, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01); CheckError("SetMelsecProtocol", lResult); } else if (sysNCType == SYSTEMTYPE.EZNC_SYS_MELDASC70) { //下記の設定は一例のため、接続環境に合わせた設定が必要 lResult = oEZNcAutCom.SetMelsecProtocol(0x01, 0x01, 0x00, 0x00, 0x3E1, 0x0901, 0x1A, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, strIPAddress, 0x04, Int32.Parse(textBoxTimeOut.Text), 0x00, 0x01, 0x04, 5001, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01); CheckError("SetMelsecProtocol", lResult); } else { //TCPIP通信設定 lResult = oEZNcAutCom.SetTCPIPProtocol(strIPAddress, lPort); CheckError("SetTCPIPProtocol", lResult); } if (oEZNcAutCom != null) { //マルチスレッドの有効化 lNCType = (Int32)sysNCType | EZNC_SYS_MULTI; //通信回線のオープン lResult = oEZNcAutCom.Open3(lNCType, Int32.Parse(textBoxMachineNo.Text), Int32.Parse(textBoxTimeOut.Text), strHostName); CheckError("Open", lResult); if (lResult == 0) { //接続の設定 bConnectStatus = true; //通信回線のクローズ if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80 || sysNCType == SYSTEMTYPE.EZNC_SYS_MELDASC70) { lResult = oEZNcAutCom.Close(); } else { lResult = oEZNcAutCom.Close2((Int32)RESETTYPE.EZNC_RESET_SIMPLE); } CheckError("Close", lResult); } else { //オブジェクトの解放 oEZNcAutCom = null; MessageBox.Show("ホスト名(IPアドレス)またはNCタイプが不正です。"); } } } else { if (bCommunicateStatus == true) { //通信回線のクローズ if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80 || sysNCType == SYSTEMTYPE.EZNC_SYS_MELDASC70) { lResult = oEZNcAutCom.Close(); } else { lResult = oEZNcAutCom.Close2((Int32)RESETTYPE.EZNC_RESET_SIMPLE); } CheckError("Close", lResult); //通信終了の設定 bCommunicateStatus = false; //周期処理の終了 timerHighCycle.Enabled = false; timerLowCycle.Enabled = false; } //切断の設定 bConnectStatus = false; //オブジェクトの解放 oEZNcAutCom = null; //GUI状態の初期化 ResetGUIStatus(); } //GUI状態の更新 UpdateGUIStatus(); } //「実行」ボタン押下時の処理 private void ButtonExcute_Click(object sender, EventArgs e) { Int32 lNCType; Int32 lAxisNo = 0; Int32 lIndex = 0; Int32 lResult; String strIPAddress; //IPアドレスの取得 if (textBoxIPAddress.Text == "") { MessageBox.Show("IPアドレスが不正です。"); return; } else { strIPAddress = textBoxIPAddress.Text; } //NCタイプの取得 Enum.TryParse(comboBoxSystemType.Text, out SYSTEMTYPE sysNCType); if (radioButtonStart.Checked == true) { //マルチスレッドの有効化 lNCType = (Int32)sysNCType | EZNC_SYS_MULTI; //通信回線のオープン lResult = oEZNcAutCom.Open3(lNCType, Int32.Parse(textBoxMachineNo.Text), Int32.Parse(textBoxTimeOut.Text), strHostName); CheckError("Open", lResult); if (lResult == 0) { //通信開始の設定 bCommunicateStatus = true; //周期処理の開始 timerHighCycle.Enabled = true; timerLowCycle.Enabled = true; //NCシステム番号の取得 lResult = oEZNcAutCom.System_GetVersion(lAxisNo, lIndex, out String strNCVersion); CheckError("GetVersion", lResult); textBoxSystemVersion.Text = strNCVersion; } } else { //通信回線のクローズ if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80 || sysNCType == SYSTEMTYPE.EZNC_SYS_MELDASC70) { lResult = oEZNcAutCom.Close(); } else { lResult = oEZNcAutCom.Close2((Int32)RESETTYPE.EZNC_RESET_SIMPLE); } CheckError("Close", lResult); if (lResult == 0) { //通信終了の設定 bCommunicateStatus = false; //周期処理の終了 timerHighCycle.Enabled = false; timerLowCycle.Enabled = false; } } //GUI状態の更新 UpdateGUIStatus(); } //周期処理(0.1秒周期) private void TimerHighCycle_Tick(object sender, EventArgs e) { //位置情報の取得 GetPosition(); } //周期処理(1秒周期) private void TimerLowCycle_Tick(object sender, EventArgs e) { //アラーム情報の取得 GetAlarm(); //プログラム情報の取得 GetProgramData(); } //位置情報の取得 private void GetPosition() { Int32 lResult; Double dPosition; //NCタイプの取得 Enum.TryParse(comboBoxSystemType.Text, out SYSTEMTYPE sysNCType); //現在座標位置の取得 if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80) { textBoxCurrentPositionX.Text = "Not supported"; textBoxCurrentPositionY.Text = "Not supported"; textBoxCurrentPositionZ.Text = "Not supported"; } else { lResult = oEZNcAutCom.Position_GetCurrentPosition(1, out dPosition); CheckError("GetCurrentPosition", lResult); if (lResult == 0) { textBoxCurrentPositionX.Text = dPosition.ToString("F3"); } lResult = oEZNcAutCom.Position_GetCurrentPosition(2, out dPosition); CheckError("GetCurrentPosition", lResult); if (lResult == 0) { textBoxCurrentPositionY.Text = dPosition.ToString("F3"); } lResult = oEZNcAutCom.Position_GetCurrentPosition(3, out dPosition); CheckError("GetCurrentPosition", lResult); if (lResult == 0) { textBoxCurrentPositionZ.Text = dPosition.ToString("F3"); } } //ワーク座標位置の取得 if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80) { textBoxWorkPositionX.Text = "Not supported"; textBoxWorkPositionY.Text = "Not supported"; textBoxWorkPositionZ.Text = "Not supported"; } else { lResult = oEZNcAutCom.Position_GetWorkPosition(1, out dPosition, 0); CheckError("GetWorkPosition", lResult); if (lResult == 0) { textBoxWorkPositionX.Text = dPosition.ToString("F3"); } lResult = oEZNcAutCom.Position_GetWorkPosition(2, out dPosition, 0); CheckError("GetWorkPosition", lResult); if (lResult == 0) { textBoxWorkPositionY.Text = dPosition.ToString("F3"); } lResult = oEZNcAutCom.Position_GetWorkPosition(3, out dPosition, 0); CheckError("GetWorkPosition", lResult); if (lResult == 0) { textBoxWorkPositionZ.Text = dPosition.ToString("F3"); } } //機械座標位置の取得 lResult = oEZNcAutCom.Position_GetMachinePosition(1, out dPosition, 0); CheckError("GetMachinePosition", lResult); if (lResult == 0) { textBoxMachinePositionX.Text = dPosition.ToString("F3"); } lResult = oEZNcAutCom.Position_GetMachinePosition(2, out dPosition, 0); CheckError("GetMachinePosition", lResult); if (lResult == 0) { textBoxMachinePositionY.Text = dPosition.ToString("F3"); } lResult = oEZNcAutCom.Position_GetMachinePosition(3, out dPosition, 0); CheckError("GetMachinePosition", lResult); if (lResult == 0) { textBoxMachinePositionZ.Text = dPosition.ToString("F3"); } } //アラーム情報の取得 private void GetAlarm() { Int32 lMessageNumber = 3; Int32 lResult; String strAlarmMsg; Array lSystem = Array.CreateInstance(typeof(Int32), lMessageNumber); Array lWarning = Array.CreateInstance(typeof(Int32), lMessageNumber); Array lAlarmType = Array.CreateInstance(typeof(Int32), lMessageNumber); //アラーム表示のテキスト消去 textBoxAlarmMessage.ResetText(); //NCタイプの取得 Enum.TryParse(comboBoxSystemType.Text, out SYSTEMTYPE sysNCType); //アラーム情報の取得 if (sysNCType == SYSTEMTYPE.EZNC_SYS_MELDASC70) { lResult = oEZNcAutCom.System_GetAlarm2(lMessageNumber, (Int32)ALARMTYPE.M_ALM_ALL_ALARM, out strAlarmMsg); } else { lResult = oEZNcAutCom.System_GetAlarm3(lMessageNumber, (Int32)ALARMTYPE.M_ALM_ALL_ALARM, ref lSystem, ref lWarning, ref lAlarmType, out strAlarmMsg); } CheckError("GetAlarm", lResult); //アラーム情報の出力 if ((lResult == 0) && (strAlarmMsg != "")) { //改行コードを区切りとして文字列を分割 String[] strAlarmMsgArray = strAlarmMsg.Split(new[] { "\r\n" }, StringSplitOptions.None); for (Int32 i = 0; i < strAlarmMsgArray.Length; i++) { //アラームメッセージの出力(NC警告メッセージの場合はWaringという文字列を付与) if (Int32.Parse(lWarning.GetValue(i).ToString()) != 0) { textBoxAlarmMessage.Text = textBoxAlarmMessage.Text + "(Warning)" + strAlarmMsgArray[i] + Environment.NewLine; } else { textBoxAlarmMessage.Text = textBoxAlarmMessage.Text + strAlarmMsgArray[i] + Environment.NewLine; } } } } //プログラム情報の取得 private void GetProgramData() { Int32 lBlockNumber = 10; Int32 lCountSize; Int32 lCountStart = 0; Int32 lResult; String strProgramDataSubstring; //NCタイプの取得 Enum.TryParse(comboBoxSystemType.Text, out SYSTEMTYPE sysNCType); //実行プログラム表示のテキスト消去 listBoxCurrentProgram.Items.Clear(); //サポート対象外の場合の処理 if (sysNCType == SYSTEMTYPE.EZNC_SYS_CNCC80) { listBoxCurrentProgram.Items.Add("Not supported"); textBoxPrgNo.Text = "Not supported"; textBoxSequenceNo.Text = "Not supported"; return; } //プログラムブロック読み出し lResult = oEZNcAutCom.Program_CurrentBlockRead(lBlockNumber, out String strProgramData, out Int32 lCurrentBlockNumber); CheckError("CurrentBlockRead", lResult); //プログラム情報の出力 if (lResult == 0) { //改行コードを区切りとして文字列をリストに追加 lCountSize = strProgramData.IndexOf("\n"); while (lCountSize > 0) { strProgramDataSubstring = strProgramData.Substring(lCountStart, lCountSize - lCountStart - 1); listBoxCurrentProgram.Items.Add(strProgramDataSubstring); lCountStart = lCountSize + 1; lCountSize = strProgramData.IndexOf("\n", lCountStart); } //強調行の指定 if (lCurrentBlockNumber == 1) { //1ブロック目を強調 listBoxCurrentProgram.SelectedIndex = 0; } else if (lCurrentBlockNumber == 2) { //2ブロック目を強調 listBoxCurrentProgram.SelectedIndex = 1; } } //プログラム番号の取得 lResult = oEZNcAutCom.Program_GetProgramNumber2((Int32)PROGRAMTYPE.EZNC_MAINPRG, out String strProgramNo); CheckError("GetProgramNumber", lResult); if(lResult == 0) { textBoxPrgNo.Text = strProgramNo; } //シーケンス番号の取得 lResult = oEZNcAutCom.Program_GetSequenceNumber((Int32)PROGRAMTYPE.EZNC_MAINPRG, out Int32 lSequenceNo); CheckError("GetSequenceNumber", lResult); if (lResult == 0) { textBoxSequenceNo.Text = lSequenceNo.ToString(); } } //GUI状態の更新 private void UpdateGUIStatus() { //接続中の場合 if(bConnectStatus == true) { textBoxConnectStatus.Text = "接続"; textBoxConnectStatus.BackColor = Color.LightBlue; buttonConnect.Text = "切断"; textBoxIPAddress.Enabled = false; comboBoxSystemType.Enabled = false; buttonExcute.Enabled = true; //通信開始中の場合 if(bCommunicateStatus == true) { textBoxOpenStatus.Text = "通信開始"; textBoxOpenStatus.BackColor = Color.LightBlue; } //通信終了中の場合 else { textBoxOpenStatus.Text = "通信終了"; textBoxOpenStatus.BackColor = Color.Yellow; } } //切断中の場合 else { textBoxConnectStatus.Text = "切断"; textBoxConnectStatus.BackColor = Color.Red; buttonConnect.Text = "接続"; textBoxOpenStatus.Text = "通信終了"; textBoxOpenStatus.BackColor = Color.Yellow; textBoxIPAddress.Enabled = true; comboBoxSystemType.Enabled = true; buttonExcute.Enabled = false; } } //GUI状態の初期化 private void ResetGUIStatus() { textBoxSystemVersion.ResetText(); textBoxCurrentPositionX.ResetText(); textBoxCurrentPositionY.ResetText(); textBoxCurrentPositionZ.ResetText(); textBoxWorkPositionX.ResetText(); textBoxWorkPositionY.ResetText(); textBoxWorkPositionZ.ResetText(); textBoxMachinePositionX.ResetText(); textBoxMachinePositionY.ResetText(); textBoxMachinePositionZ.ResetText(); listBoxCurrentProgram.Items.Clear(); textBoxPrgNo.ResetText(); textBoxSequenceNo.ResetText(); textBoxAlarmMessage.ResetText(); textBoxErrorCode.ResetText(); } } }