using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using ORiN2.ManagedCAO; namespace CaoProvOMRONNJTest { public partial class FrmTest : Form { private CCaoEngine _engine = new CCaoEngine(); class TestInfo { public TestInfo(string name, Type dataType, bool isArray, int arrayLength) : this(name, string.Empty, dataType, isArray, arrayLength, false) { } public TestInfo(string name, Type dataType, bool isArray, int arrayLength, bool isError) : this(name, string.Empty, dataType, isArray, arrayLength, isError) { } public TestInfo(string name, string path, Type dataType, bool isArray, int arrayLength, bool isError) { Name = name; Path = path; DataType = dataType; IsArray = isArray; ArrayLength = arrayLength; IsError = isError; } public string Name { get; set; } public string Path { get; set; } public Type DataType { get; set; } public bool IsArray { get; set; } public int ArrayLength { get; set; } public bool IsError { get; set; } } enum TestMode { ReadWrite, ReadOnly } class TestInfoWithOption { public TestInfoWithOption() { TestMode = FrmTest.TestMode.ReadWrite; } public string Name { get; set; } public string Option { get; set; } public Type DataType { get; set; } public bool IsArray { get; set; } public object InitValue { get; set; } public object WriteValue { get; set; } public object VaridValue { get; set; } public bool IsError { get; set; } public bool IsStruct { get; set; } public TestMode TestMode { get; set; } } public FrmTest() { InitializeComponent(); Disposed += new EventHandler(FrmTest_Disposed); } void FrmTest_Disposed(object sender, EventArgs e) { _engine.Dispose(); } private void FrmTest_Load(object sender, EventArgs e) { txtLog.Text = Path.Combine(Environment.CurrentDirectory, "log.txt"); } private bool Varidate(Type t, object x, object y) { try { if (x is Array) { Array xArr = x as Array; Array yArr = y as Array; if (xArr == null || yArr == null || xArr.Length != yArr.Length) { return false; } for (int i = 0; i < xArr.Length; i++) { if (!Varidate(t, xArr.GetValue(i), yArr.GetValue(i))) { return false; } } return true; } else { bool eq = Convert.ChangeType(x, t).Equals(y); return eq; } } catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex); return false; } } ushort us(int x) { return (ushort)x; } private void btnExecute_Click(object sender, EventArgs e) { List NO_OPTION_TEST_LIST = new List() { new TestInfo("Var_BOOL", typeof(bool), false, 0), new TestInfo("Var_BYTE", typeof(byte), false, 0), new TestInfo("Var_WORD", typeof(ushort), false, 0), new TestInfo("Var_DWORD", typeof(uint), false, 0), new TestInfo("Var_LWORD", typeof(ulong), false, 0), new TestInfo("Var_SINT", typeof(sbyte), false, 0), new TestInfo("Var_INT", typeof(short), false, 0), new TestInfo("Var_DINT", typeof(int), false, 0), new TestInfo("Var_LINT", typeof(long), false, 0), new TestInfo("Var_USINT", typeof(byte), false, 0), new TestInfo("Var_UINT", typeof(ushort), false, 0), new TestInfo("Var_UDINT", typeof(uint), false, 0), new TestInfo("Var_ULINT", typeof(ulong), false, 0), new TestInfo("Var_REAL", typeof(float), false, 0), new TestInfo("Var_LREAL", typeof(double), false, 0), new TestInfo("Var_TIME", typeof(ulong), false, 0), new TestInfo("Var_DATE", typeof(ulong), false, 0), new TestInfo("Var_TIME_OF_DAY", typeof(ulong), false, 0), new TestInfo("Var_DATE_AND_TIME", typeof(ulong), false, 0), new TestInfo("Var_STRING", typeof(string), false, 0), new TestInfo("Var_STRUCT.VAL_B.VAL_A", typeof(short), true, 16), new TestInfo("Var_STRUCT.VAL_B.VAL_A[0]", typeof(short), false, 0), new TestInfo("Var_UNION.U_BYTE", typeof(byte), true, 4), new TestInfo("Var_UNION.U_WORD", typeof(ushort), true, 2), new TestInfo("Var_UNION.U_DWORD", typeof(uint), false, 0), new TestInfo("Var_ENUM", typeof(int), false, 0), new TestInfo("Var_BOOL_ARR", typeof(bool), true, 8), new TestInfo("Var_BYTE_ARR", typeof(byte), true, 8), new TestInfo("Var_WORD_ARR", typeof(ushort), true, 8), new TestInfo("Var_DWORD_ARR", typeof(uint), true, 8), new TestInfo("Var_LWORD_ARR", typeof(ulong), true, 8), new TestInfo("Var_SINT_ARR", typeof(sbyte), true, 8), new TestInfo("Var_INT_ARR", typeof(short), true, 8), new TestInfo("Var_DINT_ARR", typeof(int), true, 8), new TestInfo("Var_LINT_ARR", typeof(long), true, 8), new TestInfo("Var_USINT_ARR", typeof(byte), true, 8), new TestInfo("Var_UINT_ARR", typeof(ushort), true, 8), new TestInfo("Var_UDINT_ARR", typeof(uint), true, 8), new TestInfo("Var_ULINT_ARR", typeof(ulong), true, 8), new TestInfo("Var_REAL_ARR", typeof(float), true, 8), new TestInfo("Var_LREAL_ARR", typeof(double), true, 8), new TestInfo("Var_TIME_ARR", typeof(ulong), true, 8), new TestInfo("Var_DATE_ARR", typeof(ulong), true, 8), new TestInfo("Var_TIME_OF_DAY_ARR", typeof(ulong), true, 8), new TestInfo("Var_DATE_AND_TIME_ARR", typeof(ulong), true, 8), new TestInfo("Var_STRING_ARR", typeof(string), true, 8, true), new TestInfo("Var_STRING_ARR[0]", typeof(string), false, 0), new TestInfo("Var_INT_ARR2", typeof(short), true, 8), new TestInfo("Var_BYTE_ARR_B", typeof(byte), true, 2000), new TestInfo("Var_WORD_ARR_B", typeof(ushort), true, 2000), new TestInfo("Var_DWORD_ARR_B", typeof(uint), true, 2000), new TestInfo("Var_BOOL_ARR_B", typeof(bool), true, 64), }; List WITH_OPTION_TEST_LIST = new List() { new TestInfoWithOption() { Name = "Var_BOOL_ARR", DataType = typeof(bool), Option = "LBound=2, Elem=1", IsArray = true, IsError = false, InitValue = new bool[] { false, false, false, false, false, false, false, false }, WriteValue = true, VaridValue = new bool[] { false, false, true, false, false, false, false, false } }, new TestInfoWithOption() { Name = "Var_BOOL_ARR", DataType = typeof(bool), Option = "LBound=2, Elem=2", IsArray = true, IsError = false, InitValue = new bool[] { false, false, false, false, false, false, false, false }, WriteValue = new bool[] { true, true }, VaridValue = new bool[] { false, false, true, true, false, false, false, false } }, new TestInfoWithOption() { Name = "Var_BOOL_ARR", DataType = typeof(bool), Option = "Elem=2", IsArray = true, IsError = false, InitValue = new bool[] { false, false, false, false, false, false, false, false }, WriteValue = new bool[] { true, true }, VaridValue = new bool[] { true, true, false, false, false, false, false, false } }, new TestInfoWithOption() { Name = "Var_BOOL_ARR", DataType = typeof(bool), Option = "Elem=9", IsArray = true, IsError = true, InitValue = new bool[] { false, false, false, false, false, false, false, false }, WriteValue = new bool[9], VaridValue = new bool[] { true, true, false, false, false, false, false, false } }, new TestInfoWithOption() { Name = "Var_WORD_ARR", DataType = typeof(ushort), Option = "LBound=2, Elem=1", IsArray = true, IsError = false, InitValue = new ushort[] { us(100), us(101), us(102), us(103), us(104), us(105), us(106), us(107) }, WriteValue = us(201), VaridValue = new ushort[] { us(100), us(101), us(201), us(103), us(104), us(105), us(106), us(107) }, }, new TestInfoWithOption() { Name = "Var_WORD_ARR", DataType = typeof(ushort), Option = "LBound=2, Elem=2", IsArray = true, IsError = false, InitValue = new ushort[] { us(100), us(101), us(102), us(103), us(104), us(105), us(106), us(107) }, WriteValue = new ushort[] { us(201), us(202) }, VaridValue = new ushort[] { us(100), us(101), us(201), us(202), us(104), us(105), us(106), us(107) }, }, new TestInfoWithOption() { Name = "Var_WORD_ARR", DataType = typeof(ushort), Option = "Elem=2", IsArray = true, IsError = false, InitValue = new ushort[] { us(100), us(101), us(102), us(103), us(104), us(105), us(106), us(107) }, WriteValue = new ushort[] { us(201), us(202) }, VaridValue = new ushort[] { us(201), us(202), us(102), us(103), us(104), us(105), us(106), us(107) }, }, new TestInfoWithOption() { Name = "Var_WORD_ARR", DataType = typeof(ushort), Option = "Elem=9", IsArray = true, IsError = true, InitValue = new ushort[] { us(100), us(101), us(102), us(103), us(104), us(105), us(106), us(107) }, WriteValue = new ushort[9], VaridValue = new ushort[] { us(201), us(202), us(102), us(103), us(104), us(105), us(106), us(107) }, }, new TestInfoWithOption() { Name = "Var_LINT_ARR", DataType = typeof(long), Option = "LBound=2, Elem=1", IsArray = true, IsError = false, InitValue = new long[] { 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L }, WriteValue = 201L, VaridValue = new long[] { 100L, 101L, 201L, 103, 104L, 105L, 106L, 107L }, }, new TestInfoWithOption() { Name = "Var_LINT_ARR", DataType = typeof(long), Option = "LBound=2, Elem=2", IsArray = true, IsError = false, InitValue = new long[] { 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L }, WriteValue = new long[] { 201L, 202L }, VaridValue = new long[] { 100L, 101L, 201L, 202L, 104L, 105L, 106L, 107L }, }, new TestInfoWithOption() { Name = "Var_LINT_ARR", DataType = typeof(long), Option = "Elem=2", IsArray = true, IsError = false, InitValue = new long[] { 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L }, WriteValue = new long[] { 201L, 202L }, VaridValue = new long[] { 201L, 202L, 102L, 103L, 104L, 105L, 106L, 107L }, }, new TestInfoWithOption() { Name = "Var_LINT_ARR", DataType = typeof(long), Option = "Elem=9", IsArray = true, IsError = true, InitValue = new long[] { 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L }, WriteValue = new long[9], VaridValue = new long[] { 201L, 202L, 102L, 103L, 104L, 105L, 106L, 107L }, }, new TestInfoWithOption() { Name = "Var_STRUCT_ARR", DataType = typeof(byte), Option = "LBound=2,Elem=1", IsArray = true, IsError = false, IsStruct = true, InitValue = new byte[2 * 17 * 8], WriteValue = new byte[] { 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, 0x0e, 0x00, 0x0f, 0x00, 0x10, 0x00 }, VaridValue = new object[8] { new byte[2 * 17], new byte[2 * 17], new byte[] { 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, 0x0e, 0x00, 0x0f, 0x00, 0x10, 0x00 }, new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17] } }, new TestInfoWithOption() { Name = "Var_STRUCT_ARR", DataType = typeof(byte), Option = "LBound=2,Elem=2", IsArray = true, IsError = false, IsStruct = true, TestMode = FrmTest.TestMode.ReadOnly, InitValue = new byte[2 * 17 * 8], WriteValue = new byte[2 * 17], VaridValue = new object[8] { new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17] } }, new TestInfoWithOption() { Name = "Var_STRUCT_ARR", DataType = typeof(byte), Option = "Elem=2", IsArray = true, IsError = false, IsStruct = true, TestMode = FrmTest.TestMode.ReadOnly, InitValue = new byte[2 * 17 * 8], WriteValue = new byte[2 * 17], VaridValue = new object[8] { new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17] } }, new TestInfoWithOption() { Name = "Var_STRUCT_ARR", DataType = typeof(byte), Option = "Elem=0", IsArray = true, IsError = false, IsStruct = true, TestMode = FrmTest.TestMode.ReadOnly, InitValue = new byte[2 * 17 * 8], WriteValue = new byte[2 * 17], VaridValue = new object[8] { new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17], new byte[2 * 17] } }, }; try { int okCount = 0; int ngCount = 0; lstResult.Items.Clear(); using (StreamWriter sw = new StreamWriter(txtLog.Text, false, Encoding.UTF8)) { string[] ConnStrs = { "CONN=TCP:{0},DELAYINIT=1,OpenMode=0", "CONN=TCP:{0},DELAYINIT=1,OpenMode=1", "CONN=TCP:{0},DELAYINIT=1,OpenMode=2" }; foreach (string conStr in ConnStrs) { sw.WriteLine(string.Format("Test for {0} ------------------------------------------------", string.Format(conStr, txtAddress.Text))); var ctrl = _engine.Workspaces[0].AddController("NJ", "CaoProv.OMRON.NJ", null, string.Format(conStr, txtAddress.Text)); foreach (var info in NO_OPTION_TEST_LIST) { try { sw.WriteLine(string.Format("Test of {0}.", info.Name)); bool getOk = false; bool putOk = false; bool varidateOk = false; bool execPutOk = false; bool execGetOk = false; bool execVaridateOk = false; string option = string.IsNullOrEmpty(info.Path) ? string.Empty : string.Format("PATH={0}", info.Path); var var = ctrl.AddVariable(info.Name, option); try { sw.Write("put_Value : "); if (info.IsArray) { Array arr = Array.CreateInstance(info.DataType, info.ArrayLength); if (info.DataType != typeof(string) && info.DataType != typeof(byte) && info.DataType != typeof(bool)) { for (int i = 0; i < arr.Length; i++) { arr.SetValue(Convert.ChangeType(i, info.DataType), i); } } var.Value = arr; } else if (info.DataType == typeof(string)) { var.Value = "A"; } else { var.Value = Activator.CreateInstance(info.DataType); } putOk = !info.IsError; if (!putOk) { throw new Exception("Oops. Put succeeded..."); } sw.WriteLine("OK"); okCount++; } catch (Exception ex3) { putOk = info.IsError; if (putOk) { sw.WriteLine("OK"); okCount++; } else { sw.WriteLine("NG"); sw.WriteLine(ex3.ToString()); ngCount++; } } object v = null; try { sw.Write("get_Value : "); v = var.Value; getOk = true; sw.WriteLine("OK"); okCount++; } catch (Exception ex3) { sw.WriteLine("NG"); sw.WriteLine(ex3.ToString()); ngCount++; } try { sw.Write("Varidate : "); if (info.IsArray) { varidateOk = (v is Array) && ((Array)v).GetLength(0) == info.ArrayLength && (((Array)v).GetValue(0).GetType() == info.DataType); if (varidateOk && info.DataType != typeof(string) && info.DataType != typeof(byte) && info.DataType != typeof(bool)) { for (int i = 0; i < ((Array)v).Length; i++) { varidateOk = Convert.ToInt32(((Array)v).GetValue(i)) == i; if (!varidateOk) break; } } } else { varidateOk = (v.GetType() == info.DataType); } if (!varidateOk) { throw new Exception("Data type unmatch."); } sw.WriteLine("OK"); okCount++; } catch (Exception ex3) { sw.WriteLine("NG"); sw.WriteLine(ex3.ToString()); ngCount++; } string accessPath = string.IsNullOrEmpty(info.Path) ? info.Name : info.Path; try { sw.Write("put_Value(Execute) : "); if (info.IsArray) { Array arr = Array.CreateInstance(info.DataType, info.ArrayLength); if (info.DataType != typeof(string) && info.DataType != typeof(byte) && info.DataType != typeof(bool)) { for (int i = 0; i < arr.Length; i++) { arr.SetValue(Convert.ChangeType(i * 2, info.DataType), i); } } ctrl.Execute("PutValue", new object[] { accessPath, arr }); } else if (info.DataType == typeof(string)) { ctrl.Execute("PutValue", new object[] { accessPath, "A" }); } else { ctrl.Execute("PutValue", new object[] { accessPath, Activator.CreateInstance(info.DataType) }); } execPutOk = !info.IsError; if (!execPutOk) { throw new Exception("Oops. Put succeeded..."); } sw.WriteLine("OK"); okCount++; } catch (Exception ex3) { execPutOk = info.IsError; if (execPutOk) { sw.WriteLine("OK"); okCount++; } else { sw.WriteLine("NG"); sw.WriteLine(ex3.ToString()); ngCount++; } } try { sw.Write("get_Value(Execute) : "); v = ctrl.Execute("GetValue", accessPath); execGetOk = true; sw.WriteLine("OK"); okCount++; } catch (Exception ex3) { sw.WriteLine("NG"); sw.WriteLine(ex3.ToString()); ngCount++; } try { sw.Write("Varidate(Execute) : "); if (info.IsArray) { execVaridateOk = (v is Array) && ((Array)v).GetLength(0) == info.ArrayLength && (((Array)v).GetValue(0).GetType() == info.DataType); if (execVaridateOk && info.DataType != typeof(string) && info.DataType != typeof(byte) && info.DataType != typeof(bool)) { for (int i = 0; i < ((Array)v).Length; i++) { execVaridateOk = Convert.ToInt32(((Array)v).GetValue(i)) == i * 2; if (!execVaridateOk) break; } } } else { execVaridateOk = (v.GetType() == info.DataType); } if (!varidateOk) { throw new Exception("Data type unmatch."); } sw.WriteLine("OK"); okCount++; } catch (Exception ex3) { sw.WriteLine("NG"); sw.WriteLine(ex3.ToString()); ngCount++; } string lstItem = string.Format("{0} : Put[{1}], Get[{2}], Varidate[{3}], Put(Execute)[{6}], Get(Execute)[{4}], Varidate(Execute)[{5}]", info.Name.PadRight(25), putOk ? "OK" : "NG", getOk ? "OK" : "NG", varidateOk ? "OK" : "NG", execGetOk ? "OK" : "NG", execVaridateOk ? "OK" : "NG", execPutOk ? "OK" : "NG"); lstResult.Items.Add(lstItem); ctrl.Variables.Remove(var.Name); } catch (Exception ex2) { if (info.IsError) { string lstItem = string.Format("{0} : Error(SUCCEEDED)", info.Name.PadRight(25)); lstResult.Items.Add(lstItem); sw.WriteLine("Error occured.(OK)"); okCount++; } else { string lstItem = string.Format("{0} : Error(FAILED)", info.Name.PadRight(25)); lstResult.Items.Add(lstItem); sw.WriteLine(ex2.ToString()); ngCount++; } } } // TODO : オプションありテスト実施 for (int i = 0; i < 8; i++) { CCaoVariable v = ctrl.AddVariable("Var_STRUCT_ARR[" + i.ToString() + "]", null); v.Value = new byte[2 * 17]; ctrl.Variables.Remove(v.Name); } foreach (TestInfoWithOption info in WITH_OPTION_TEST_LIST) { CCaoVariable testVar = ctrl.AddVariable(info.Name, info.Option); CCaoVariable fullVar = ctrl.AddVariable(info.Name + "_FULL", "Path=" + info.Name); sw.Write(string.Format("Test of {0} : {1}.", info.Name, info.Option)); try { if (!info.IsStruct) { fullVar.Value = info.InitValue; } if (info.TestMode == TestMode.ReadWrite) { testVar.Value = info.WriteValue; } object testValue = testVar.Value; object fullValue = fullVar.Value; bool result = true; if (info.TestMode == TestMode.ReadWrite) { result = Varidate(info.DataType, info.WriteValue, testValue) && Varidate(info.DataType, info.VaridValue, fullValue); } if (!result) { lstResult.Items.Add(string.Format("{0} : {1} : NG", info.Name.PadRight(25), info.Option.PadRight(25))); sw.WriteLine("NG"); ngCount++; } else { lstResult.Items.Add(string.Format("{0} : {1} : OK", info.Name.PadRight(25), info.Option.PadRight(25))); sw.WriteLine("OK"); okCount++; } } catch (Exception ex2) { if (info.IsError) { string lstItem = string.Format("{0} : {1} : Error(SUCCEEDED)", info.Name.PadRight(25), info.Option.PadRight(25)); lstResult.Items.Add(lstItem); sw.WriteLine("Error occured.(OK)"); okCount++; } else { string lstItem = string.Format("{0} : {1} : Error(FAILED)", info.Name.PadRight(25), info.Option.PadRight(25)); lstResult.Items.Add(lstItem); sw.WriteLine(ex2.ToString()); ngCount++; } } ctrl.Variables.Remove(fullVar.Name); ctrl.Variables.Remove(testVar.Name); } try { _engine.Workspaces[0].Controllers.Remove("NJ"); } catch { } } sw.Flush(); } MessageBox.Show(string.Format("テスト完了:OK件数 {0}, NG件数 {1}", okCount, ngCount)); } catch (Exception ex) { MessageBox.Show(this, ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }