NxN行列の複素行列クラス
ベクトルとの掛け算、逆行行列演算を実装しているので、
大学のレポート・研究で役立つかも。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; namespace MatrixNxN { [Serializable] public class ComplexMatrix { public ComplexMatrix(int n) { sMatrix = new Complex[n, n]; } public ComplexMatrix(Complex[,] Matrix) { int n = Matrix.GetLength(0); sMatrix = new Complex[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sMatrix[i, j] = sMatrix[i, j] + Matrix[i, j]; } } } public ComplexMatrix(Complex[][] Matrix, int n) { sMatrix = new Complex[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sMatrix[i, j] = sMatrix[i, j] + Matrix[i][j]; } } } public Complex[,] sMatrix; public Complex Get(int i, int j) { return this.sMatrix[i, j]; } public int GetRank() { return sMatrix.GetLength(0); } public void Set(int i, int j, Complex Aij) { this.sMatrix[i, j] = new Complex(0, 0) + Aij; } private static ComplexMatrix inv_complex(ComplexMatrix Mat) { int n = Mat.sMatrix.GetLength(0); ComplexMatrix Resultsmat = new ComplexMatrix(Mat.sMatrix); Complex[,] a = Resultsmat.sMatrix; Complex p, q; int i, j, k; for (k = 0; k < n; k++) { p = a[k, k]; a[k, k] = new Complex(1, 0); for (j = 0; j < n; j++) a[k, j] /= p; for (i = 0; i < n; i++) if (i != k) { q = a[i, k]; a[i, k] = new Complex(0, 0); for (j = 0; j < n; j++) a[i, j] -= q * a[k, j]; } } return Resultsmat; } public ComplexMatrix Conjugate() { ComplexMatrix Result = new ComplexMatrix(this.sMatrix.GetLength(0)); for (int i = 0; i < this.sMatrix.GetLength(0); i++) { for (int j = 0; j < this.sMatrix.GetLength(0); j++) { Complex s; s = Complex.Conjugate(sMatrix[i, j]); Result.sMatrix[i, j] = Result.sMatrix[i, j] + s; } } return Result; } public ComplexMatrix Re() { ComplexMatrix Result = new ComplexMatrix(this.sMatrix.GetLength(0)); for (int i = 0; i < this.sMatrix.GetLength(0); i++) { for (int j = 0; j < this.sMatrix.GetLength(0); j++) { Complex s; s = sMatrix[i, j]; Result.sMatrix[i, j] = Result.sMatrix[i, j] + s.Real; } } return Result; } public ComplexMatrix Sqrt() { ComplexMatrix Result = new ComplexMatrix(this.sMatrix.GetLength(0)); for (int i = 0; i < this.sMatrix.GetLength(0); i++) { for (int j = 0; j < this.sMatrix.GetLength(0); j++) { Complex s; s = Complex.Conjugate(sMatrix[i, j]); Result.sMatrix[i, j] = Result.sMatrix[i, j] + Complex.Sqrt(s); } } return Result; } public ComplexMatrix Inv() { ComplexMatrix Results = new ComplexMatrix(this.sMatrix.GetLength(0)); Results = Results + ComplexMatrix.inv_complex(this); return Results; } private static void mul_complex(Complex[,] a, Complex[,] b, Complex[,] c) { int n = a.GetLength(0); Complex s; int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); for (k = 0; k < n; k++) s += a[i, k] * b[k, j]; c[i, j] = s; } } private static void mul_complex(ComplexMatrix Mata, ComplexMatrix Matb, ComplexMatrix Matc) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); Matc = new ComplexMatrix(n); //for (int ia = 0; ia < n; ia++) //{ // for (int ja = 0; ja < n; ja++) // { // Matc.sMatrix[ia,ja]=Add(new Complex()); // } //} Complex s; int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); for (k = 0; k < n; k++) s += a[i, k] * b[k, j]; Matc.sMatrix[i, j] = s; } } public static ComplexMatrix operator *(ComplexMatrix Mata, ComplexMatrix Matb) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j, k; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); for (k = 0; k < n; k++) s += a[i, k] * b[k, j]; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator +(ComplexMatrix Mata, ComplexMatrix Matb) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + a[i, j] + b[i, j]; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator -(ComplexMatrix Mata, ComplexMatrix Matb) { Complex[,] a = Mata.sMatrix; Complex[,] b = Matb.sMatrix; int n = a.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + a[i, j] - b[i, j]; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator *(Complex dA, ComplexMatrix Matb) { Complex[,] b = Matb.sMatrix; int n = b.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + b[i, j] * dA; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexMatrix operator *(ComplexMatrix Matb, Complex dA) { Complex[,] b = Matb.sMatrix; int n = b.GetLength(0); ComplexMatrix Matc = new ComplexMatrix(n); Complex s; int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { s = new Complex(0, 0); s = s + b[i, j] * dA; Matc.sMatrix[i, j] = s; } return Matc; } public static ComplexVectorH operator *(ComplexVectorH Vect, ComplexMatrix Mata) { int n = Mata.sMatrix.GetLength(0); ComplexVectorH ResultsVect = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); for (int j = 0; j < n; j++) { s = s + Vect.sVectorH[j] * Mata.sMatrix[j, i]; } ResultsVect.sVectorH[i] = s; } return ResultsVect; } public static ComplexVectorV operator *(ComplexMatrix Mata, ComplexVectorV Vect) { int n = Mata.sMatrix.GetLength(0); ComplexVectorV ResultsVect = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); for (int j = 0; j < n; j++) { s = s + Mata.sMatrix[i, j] * Vect.sVectorV[j]; } ResultsVect.sVectorV[i] = s; } return ResultsVect; } public static ComplexMatrix IdentityMatix(int n) { ComplexMatrix Matc = new ComplexMatrix(n); Complex s = new Complex(1, 0); ; int i; for (i = 0; i < n; i++) { Matc.sMatrix[i, i] = s; } return Matc; } public static ComplexMatrix ZeroMatrix(int n) { ComplexMatrix Matc = new ComplexMatrix(n); return Matc; } } public class ComplexVectorV { public Complex[] sVectorV; public ComplexVectorV(int n) { sVectorV = new Complex[n]; } public ComplexVectorV(Complex[] VectorV, int n) { sVectorV = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + VectorV[i]; sVectorV[i] = s; } } public ComplexVectorV(Complex[] LstVectV) { int n = LstVectV.Length; sVectorV = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + LstVectV[i]; sVectorV[i] = s; } } public ComplexVectorH T() { ComplexVectorH sVector = new ComplexVectorH(this.sVectorV.Length); this.sVectorV.CopyTo(sVector.sVectorH, 0); return sVector; } public static ComplexVectorV operator +(ComplexVectorV Vct1, ComplexVectorV Vct2) { Complex[] v1 = Vct1.sVectorV; Complex[] v2 = Vct2.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] + v2[i]; Vc.sVectorV[i] = s; } return Vc; } public static ComplexVectorV operator -(ComplexVectorV Vct1, ComplexVectorV Vct2) { Complex[] v1 = Vct1.sVectorV; Complex[] v2 = Vct2.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] - v2[i]; Vc.sVectorV[i] = s; } return Vc; } public static ComplexVectorV operator *(ComplexVectorV Vct1, Complex dA) { Complex[] v1 = Vct1.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorV[i] = s; } return Vc; } public static ComplexVectorV operator *(Complex dA, ComplexVectorV Vct1) { Complex[] v1 = Vct1.sVectorV; int n = v1.Length; ComplexVectorV Vc = new ComplexVectorV(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorV[i] = s; } return Vc; } public static Complex operator *(ComplexVectorV VectorV, ComplexVectorH VectorH) { Complex s = new Complex(0, 0); int n = VectorV.sVectorV.Length; for (int i = 0; i < n; i++) { s = s + VectorV.sVectorV[i] * VectorH.sVectorH[i]; } return s; } public static Complex operator *(ComplexVectorH VectorH, ComplexVectorV VectorV) { Complex s = new Complex(0, 0); int n = VectorV.sVectorV.Length; for (int i = 0; i < n; i++) { s = s + VectorV.sVectorV[i] * VectorH.sVectorH[i]; } return s; } } public class ComplexVectorH { public Complex[] sVectorH; public ComplexVectorH(int n) { sVectorH = new Complex[n]; } public ComplexVectorH(Complex[] VectorH, int n) { sVectorH = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + VectorH[i]; sVectorH[i] = s; } } public ComplexVectorH(Complex[] LstVectH) { int n = LstVectH.Length; sVectorH = new Complex[n]; for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + LstVectH[i]; sVectorH[i] = s; } } public ComplexVectorV T() { int n = this.sVectorH.Length; ComplexVectorV sVectorV = new ComplexVectorV(n); this.sVectorH.CopyTo(sVectorV.sVectorV, 0); return sVectorV; } public static ComplexVectorH operator +(ComplexVectorH Vct1, ComplexVectorH Vct2) { Complex[] v1 = Vct1.sVectorH; Complex[] v2 = Vct2.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] + v2[i]; Vc.sVectorH[i] = s; } return Vc; } public static ComplexVectorH operator -(ComplexVectorH Vct1, ComplexVectorH Vct2) { Complex[] v1 = Vct1.sVectorH; Complex[] v2 = Vct2.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] - v2[i]; Vc.sVectorH[i] = s; } return Vc; } public static ComplexVectorH operator *(ComplexVectorH Vct1, Complex dA) { Complex[] v1 = Vct1.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorH[i] = s; } return Vc; } public static ComplexVectorH operator *(Complex dA, ComplexVectorH Vct1) { Complex[] v1 = Vct1.sVectorH; int n = v1.Length; ComplexVectorH Vc = new ComplexVectorH(n); for (int i = 0; i < n; i++) { Complex s = new Complex(0, 0); s = s + v1[i] * dA; Vc.sVectorH[i] = s; } return Vc; } } }
いろいろなサイトからコードコピペしているので、引用先の方ごめんなさい。
エラーだらけのVNA通信ルーチン。
ENAとの通信でどうしてもうまくいかない。
ブレークポイント入れると動くという気持ち悪い動作が治らない。
一説にはVS2012なら動くという噂もある。こういうのは嫌いだ。
追記。
KeysightのVBAのサンプルプログラムもSCPIバスを観察してると、
同じコマンド構成になってたが、正常に動いてた。
結局、実行速度が遅くないとE5071Cが反応できないという結論に至り、
コマンドごとにスリープを入れたら動きました。あほらしい。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Diagnostics; namespace IVICommunicator { public class VNATalker : IDisposable { private Ivi.Visa.Interop.ResourceManager RM; private Ivi.Visa.Interop.FormattedIO488 DMM; public string VesaAdress; public string IDN; public enum InstTyep { PNA, ENA, ZNB, SNP, Unkown }; private InstTyep InstrumentType; // Dispose したかどうか private bool _disposed = false; // IDisposable に必須のメソッドの実装 public void Dispose() { Dispose(true); // Dispose() によってリソースの解放を行ったので、 // GC での解放が必要が無いことを GC に通知します。 GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // Dispose がまだ実行されていないときだけ実行 if (!_disposed) { if (DMM.IO != null) { DMM.IO.Close(); } // disposing が true の場合(Dispose() が実行された場合)は // マネージリソースも解放します。 if (disposing) { // マネージリソースの解放 IDN = null; VesaAdress = null; } // アンマネージリソースの解放 System.Runtime.InteropServices.Marshal.ReleaseComObject(DMM); System.Runtime.InteropServices.Marshal.ReleaseComObject(RM); DMM = null; RM = null; _disposed = true; } } // ファイナライザー(デストラクター) // // Dispose() が呼び出されていない場合のみ // 実行されます。 ~VNATalker() { Dispose(false); } private void CheckDisposed() { if (_disposed) throw new ObjectDisposedException(GetType().FullName); } public void SetInstrumentAdress(string strAdress) { if (strAdress == null) { VesaAdress = ""; } else { RM = new Ivi.Visa.Interop.ResourceManager(); DMM = new Ivi.Visa.Interop.FormattedIO488(); VesaAdress = strAdress; } } public string ConnectInstrument() { { try { int sysTimeOut = 2000; DMM.IO = (Ivi.Visa.Interop.IMessage)RM.Open(VesaAdress, Ivi.Visa.Interop.AccessMode.NO_LOCK, sysTimeOut); //都度通信終了メッセージを送る。(LowSecketの場合、ENDMessageが規格上送れないので、下の処理をする。) DMM.IO.SendEndEnabled = false; if (VesaAdress.IndexOf("SOCKET") >= 0) { //データの終端を示す文字は、ASCII の 10、ラインフィード <LF> DMM.IO.TerminationCharacter = 10; //ソケット接続時のみ、終端文字で読み取りを終了させる設定です。GPIB や USB、LAN(VXI-11 プロトコル)で接続する場合は、この設定は不要 DMM.IO.TerminationCharacterEnabled = true; } DMM.WriteString("*IDN?"); IDN = DMM.ReadString(); DMM.IO.Timeout = 30000; int TIMEOUT = DMM.IO.Timeout; if (IDN.IndexOf("E50") > 0) { InstrumentType = InstTyep.ENA; } else if (IDN.IndexOf("N52") > 0) { InstrumentType = InstTyep.PNA; } else if (IDN.IndexOf("ZNB") > 0) { InstrumentType = InstTyep.ZNB; } else { InstrumentType = InstTyep.Unkown; } if (InstrumentType == InstTyep.PNA) { //Sパラ保存形式を設定。 DMM.WriteString(":MMEM:STOR:TRAC:FORM:SNP DB"); } else if (InstrumentType == InstTyep.ENA) { //Sパラ保存形式を設定。 DMM.WriteString(":MMEM:STOR:SNP:FORM DB"); } return IDN; } catch (Exception ex) { return ex.Message; } } } public void DisConnectInstrument() { CheckDisposed(); DMM.IO.Close(); } public MemoryStream StoreSparaMater(int PortNum, int Chnnel) { string Chan = Chnnel.ToString(); MemoryStream ResSnP = null; if (InstrumentType == InstTyep.ENA) { ResSnP = StoreSparaMaterENA(PortNum, Chan); } else if (InstrumentType == InstTyep.PNA) { ResSnP = StoreSparaMaterPNA(PortNum, Chan); } else if (InstrumentType == InstTyep.ZNB) { ResSnP = StoreSparaMaterZNBZ50(PortNum, Chan); } else { ResSnP = null; } return ResSnP; } public MemoryStream StoreSparaMaterZNBSpecialImp(int PortNum, int Chnnel) { string Chan = Chnnel.ToString(); MemoryStream ResSnP = null; if (InstrumentType == InstTyep.ENA) { ResSnP = StoreSparaMaterENA(PortNum, Chan); } else if (InstrumentType == InstTyep.PNA) { ResSnP = StoreSparaMaterPNA(PortNum, Chan); } else if (InstrumentType == InstTyep.ZNB) { ResSnP = StoreSparaMaterZNBPortImp(PortNum, Chan); } else { ResSnP = null; } return ResSnP; } private MemoryStream StoreSparaMaterPNA(int PortNum, string Chan) { string readfile = @"'D:\pna_temp_data_s" + PortNum.ToString() + "p.s" + PortNum.ToString() + "p'"; string SelectedPort = "1"; for (int i = 1; i < PortNum; i++) { SelectedPort = SelectedPort + "," + ((int)(i + 1)).ToString(); } //Sパラを本体に保存 //DMM.WriteString(":MMEM:STOR " + readfile); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); DMM.WriteString(":CALC" + Chan + ":PAR:CAT:EXT?"); string DefinedCatalogs = DMM.ReadString(); string DefinedCatalog = DefinedCatalogs.Substring(0, DefinedCatalogs.IndexOf(",")); DefinedCatalog = DefinedCatalog.Substring(DefinedCatalog.IndexOf("CH"), DefinedCatalog.Length - DefinedCatalog.IndexOf("CH")); DMM.WriteString(":CALC" + Chan + ":PAR:SEL '" + DefinedCatalog + "'"); DMM.WriteString(":CALC" + Chan + ":DATA:SNP:PORT:Save '" + SelectedPort + "'," + readfile); //Keysight推奨の新しい形式 DMM.WriteString(":MMEM:TRAN? " + readfile); //ファイル転送 Byte[] SparaData = DMM.ReadIEEEBlock(Ivi.Visa.Interop.IEEEBinaryType.BinaryType_UI1); DMM.WriteString("*OPC?"); sts = DMM.ReadString(); //非同期でファイル削除命令発行 Task.Factory.StartNew(() => { DMM.WriteString(":MMEM:DEL " + readfile); }); DMM.WriteString("*OPC?"); sts = DMM.ReadString(); //バイトデータをメモリストリームに変換 MemoryStream Mst = new MemoryStream(SparaData); return Mst; } private MemoryStream StoreSparaMaterZNBZ50(int PortNum, string Chan) { string readfile = @"'temp_data_s" + PortNum.ToString() + "p.s" + PortNum.ToString() + "p'"; string SelectedPort = "1"; for (int i = 1; i < PortNum; i++) { SelectedPort = SelectedPort + "," + ((int)(i + 1)).ToString(); } //Sパラを本体に保存 //DMM.WriteString(":MMEM:STOR " + readfile); DMM.WriteString(":MMEM:STOR:TRAC:PORT " + Chan + ", " + readfile + ", LOGPhase, CIMPedance , " + SelectedPort); DMM.WriteString(":MMEM:DATA? " + readfile); //ファイル転送 Byte[] SparaData = DMM.ReadIEEEBlock(Ivi.Visa.Interop.IEEEBinaryType.BinaryType_UI1); DMM.WriteString("*OPC?"); int sts = (int)DMM.ReadNumber(); //非同期でファイル削除命令発行 Task.Factory.StartNew(() => { DMM.WriteString(":MMEM:DEL " + readfile); }); //バイトデータをメモリストリームに変換 MemoryStream Mst = new MemoryStream(SparaData); return Mst; } private MemoryStream StoreSparaMaterZNBPortImp(int PortNum, string Chan) { string readfile = @"'temp_data_s" + PortNum.ToString() + "p.s" + PortNum.ToString() + "p'"; string SelectedPort = "1"; for (int i = 1; i < PortNum; i++) { SelectedPort = SelectedPort + "," + ((int)(i + 1)).ToString(); } //Sパラを本体に保存 //DMM.WriteString(":MMEM:STOR " + readfile); DMM.WriteString(":MMEM:STOR:TRAC:PORT " + Chan + ", " + readfile + ", LOGPhase, PIMPedance , " + SelectedPort); DMM.WriteString(":MMEM:DATA? " + readfile); //ファイル転送 Byte[] SparaData = DMM.ReadIEEEBlock(Ivi.Visa.Interop.IEEEBinaryType.BinaryType_UI1); DMM.WriteString("*OPC?"); int sts = (int)DMM.ReadNumber(); //非同期でファイル削除命令発行 Task.Factory.StartNew(() => { DMM.WriteString(":MMEM:DEL " + readfile); }); //バイトデータをメモリストリームに変換 MemoryStream Mst = new MemoryStream(SparaData); return Mst; } private MemoryStream StoreSparaMaterENA(int PortNum, string Chan) { string readfile = @"D:\temp_dataS" + PortNum.ToString() + "P.s" + PortNum.ToString() + "p"; readfile = @"""" + readfile + @""""; string SelectedPort = "1"; for (int i = 1; i < PortNum; i++) { SelectedPort = SelectedPort + "," + ((int)(i + 1)).ToString(); } //Sパラを本体に保存 //DMM.WriteString(":MMEM:STOR " + readfile); DMM.WriteString(":DISP:WIND" + Chan + ":ACT",true); Trace.WriteLine(":DISP:WIND" + Chan + ":ACT"); DMM.WriteString(":MMEM:STOR:SNP:TYPE:S" + PortNum.ToString() + "P " + SelectedPort, true); Trace.WriteLine(":MMEM:STOR:SNP:TYPE:S" + PortNum.ToString() + "P " + SelectedPort); DMM.WriteString(":MMEM:STOR:SNP:DATA " + readfile, true); Trace.WriteLine(":MMEM:STOR:SNP:DATA " + readfile); DMM.FlushWrite(true); MemoryStream Mst = null; if (ErrorCheck()) { DMM.WriteString(":MMEM:TRAN? " + readfile, true); Trace.WriteLine(":MMEM:TRAN? " + readfile); //ファイル転送 Byte[] SparaData = DMM.ReadIEEEBlock(Ivi.Visa.Interop.IEEEBinaryType.BinaryType_UI1, false, true); DMM.WriteString("*OPC?",true); Trace.WriteLine("*OPC?"); int sts = (int)DMM.ReadNumber(); Trace.WriteLine("> " + sts.ToString()); DMM.WriteString(":MMEM:DEL " + readfile,false); Trace.WriteLine(":MMEM:DEL " + readfile); Mst = new MemoryStream(SparaData); } return Mst; } private bool ErrorCheck() { string sErr; string[] vErrNo; string sResponse; bool res = true; DMM.WriteString(":SYST:ERR?", true); Trace.WriteLine(":SYST:ERR?"); sErr = DMM.ReadString(); Trace.WriteLine("> " + sErr); vErrNo = sErr.Split(','); if(vErrNo[0] != "0") { res = false; sResponse = vErrNo[1]; foreach(string errstr in vErrNo) { Trace.WriteLine("err> "+ errstr); } } return res; } /// <summary> /// ポートエクステンションを有効化して、ポートエクステンション値を設定 /// </summary> /// <param name="psecArray"></param> /// <param name="ChanNo"></param> /// <returns></returns> public int SetPortExt(double[] psecArray, int ChanNo) { if (InstrumentType == InstTyep.ENA) { //ポートエクステンション on DMM.WriteString(":SENS" + ChanNo.ToString() + ":CORR:EXT ON"); for (int i = 1; i <= psecArray.Length; i++) { //DMM.WriteString(":SENS" + ChanNo.ToString() + ":PORT" + i.ToString() + ":ZREF 50, 0"); double psec = psecArray[i - 1] / (Math.Pow(10d, 12d)); string sec = psec.ToString("0.00000E0"); DMM.WriteString(":SENS" + ChanNo.ToString() + "CORR:EXT:PORT" + i.ToString() + ":TIME " + sec); } DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.PNA) { //ポートエクステンション on DMM.WriteString(":SENS" + ChanNo.ToString() + ":CORR:EXT ON"); for (int i = 1; i <= psecArray.Length; i++) { double psec = psecArray[i - 1] / (Math.Pow(10d, 12d)); string sec = psec.ToString("0.00000E0"); DMM.WriteString(":SENS" + ChanNo.ToString() + ":CORR:EXT:PORT" + i.ToString() + ":TIME " + sec); } DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.ZNB) { for (int i = 1; i <= psecArray.Length; i++) { double psec = psecArray[i - 1] / (Math.Pow(10d, 12d)); string sec = psec.ToString("0.00000E0"); DMM.WriteString(":SENS" + ChanNo.ToString() + ":CORR:EDEL" + i.ToString() + ":TIME " + sec); } DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else { return -1; } } public int LoadCalPNA(string CalfileName) { DMM.WriteString(":MMEM:LOAD '" + CalfileName + "'"); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } public int LoadCalENA(string CalfileName) { DMM.WriteString(":MMEM:LOAD '" + CalfileName + "'"); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } public int LoadCalZNB(string CalfileName) { DMM.WriteString("MMEM:LOAD:STAT 1,'" + CalfileName + "'"); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } public int SaveCalPNA(string CalfileName) { DMM.WriteString(":MMEM:DEL '" + CalfileName + "'"); DMM.WriteString(":MMEM:STOR:CSAR '" + CalfileName + "'"); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } public int SaveCalENA(string CalfileName) { DMM.WriteString(":MMEM:DEL '" + CalfileName + "'"); DMM.WriteString(":MMEM:STOR '" + CalfileName + "'"); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } public int SaveCalZNB(string CalfileName) { DMM.WriteString(":MMEM:DEL '" + CalfileName + "'"); DMM.WriteString("MMEM:STOR:STAT 1,'" + CalfileName + "'"); DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } /// <summary> /// ネットアナに設定されているポートエクステンションを持ってくる。 /// </summary> /// <param name="ports"></param> /// <param name="ChanNo"></param> /// <returns></returns> public double[] GetPortExt(int ports, int ChanNo) { double[] Res = new double[ports]; if (InstrumentType == InstTyep.ENA) { for (int i = 1; i <= ports; i++) { DMM.WriteString(":SENS" + ChanNo.ToString() + "CORR:EXT:PORT" + i.ToString() + ":TIME?"); double sec = Convert.ToDouble(DMM.ReadNumber()); Res[i - 1] = sec * Math.Pow(10d, 12d); DMM.WriteString("*OPC?"); int sts = (int)DMM.ReadNumber(); } return Res; } else if (InstrumentType == InstTyep.PNA) { for (int i = 1; i <= ports; i++) { DMM.WriteString(":SENS" + ChanNo.ToString() + ":CORR:EXT:PORT" + i.ToString() + ":TIME?"); double sec = Convert.ToDouble(DMM.ReadNumber()); Res[i - 1] = sec * Math.Pow(10d, 12d); DMM.WriteString("*OPC?"); int sts = (int)DMM.ReadNumber(); } return Res; } else if (InstrumentType == InstTyep.ZNB) { for (int i = 1; i <= ports; i++) { DMM.WriteString(":SENS" + ChanNo.ToString() + ":CORR:EDEL" + i.ToString() + ":TIME?"); double sec = Convert.ToDouble(DMM.ReadNumber()); Res[i - 1] = sec * Math.Pow(10d, 12d); DMM.WriteString("*OPC?"); int sts = (int)DMM.ReadNumber(); } return Res; } else { for (int i = 1; i <= ports; i++) { Res[i - 1] = -1; } return Res; } } private string[] FixtureOnOff = new string[50]; private string[] PortZConvOnOff = new string[50]; private string[] MatchCirOnOff = new string[50]; public int PresrveFixtureState(int ChanNo) { if (InstrumentType == InstTyep.ENA) { //DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:STAT ON"); DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:STAT?"); string fsimonoff = DMM.ReadString(); if (fsimonoff.IndexOf("1") > -1) { FixtureOnOff[ChanNo - 1] = "1"; } else { FixtureOnOff[ChanNo - 1] = "0"; } DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:PMC:STAT?"); //portmattcing string fsimMatchCir = DMM.ReadString(); if (fsimMatchCir.IndexOf("1") > -1) { MatchCirOnOff[ChanNo - 1] = "1"; } else { MatchCirOnOff[ChanNo - 1] = "0"; } DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:ZCON:STAT?"); //PortZCon string PortZComv = DMM.ReadString(); if (PortZComv.IndexOf("1") > -1) { PortZConvOnOff[ChanNo - 1] = "1"; } else { PortZConvOnOff[ChanNo - 1] = "0"; } DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.PNA) { DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:STAT?"); string fsimonoff = DMM.ReadString(); if (fsimonoff.IndexOf("1") > -1) { FixtureOnOff[ChanNo - 1] = "1"; } else { FixtureOnOff[ChanNo - 1] = "0"; } DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:PMC:STAT?"); //portmattcing string fsimMatchCir = DMM.ReadString(); if (fsimMatchCir.IndexOf("1") > -1) { MatchCirOnOff[ChanNo - 1] = "1"; } else { MatchCirOnOff[ChanNo - 1] = "0"; } DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:ZCON:STAT?"); //PortZCon string PortZComv = DMM.ReadString(); if (PortZComv.IndexOf("1") > -1) { PortZConvOnOff[ChanNo - 1] = "1"; } else { PortZConvOnOff[ChanNo - 1] = "0"; } DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.ZNB) { FixtureOnOff[ChanNo - 1] = ""; MatchCirOnOff[ChanNo - 1] = ""; PortZConvOnOff[ChanNo - 1] = ""; return 0; } else { return -1; } } public int RestoreFixtureState(int ChanNo) { if (InstrumentType == InstTyep.ENA) { DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:STAT " + FixtureOnOff[ChanNo - 1]); DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:PMC:STAT " + MatchCirOnOff[ChanNo - 1]); //portmattcing DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:ZCON:STAT " + PortZConvOnOff[ChanNo - 1]); //PortZCon DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.PNA) { DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:STAT " + FixtureOnOff[ChanNo - 1]); DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:PMC:STAT " + MatchCirOnOff[ChanNo - 1]); //portmattcing DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:ZCON:STAT " + PortZConvOnOff[ChanNo - 1]); //PortZCon DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else { return -1; } } public int NormalizeFixtureState(int ChanNo) { if (InstrumentType == InstTyep.ENA) { DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:PMC:STAT OFF"); //portmattcing DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:ZCON:STAT OFF"); //PortZCon DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.PNA) { DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:PMC:STAT OFF"); //portmattcing DMM.WriteString(":CALC" + ChanNo.ToString() + ":FSIM:SEND:ZCON:STAT OFF"); //PortZCon DMM.WriteString("*OPC?"); string sts = DMM.ReadString(); return 0; } else if (InstrumentType == InstTyep.ZNB) { //フィクスチャ切らなくてもいつでもZ0=50のデータが手に入る。 return 0; } else { return -1; } } } }
Outlook予定表を読み取り起動するエヴァ風タイマーのソースコード<C#>
Clock.xaml
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="スケジューラー" Height="354" Width="276" ShowInTaskbar="False" Closing="Window_Closing" Loaded="Window_Loaded"> <Grid> <Button x:Name="button" Content="再取得" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="78" Click="button_Click" Margin="180,10,0,0"/> <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="286" Margin="0,37,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="268"/> <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="おはようございます。" VerticalAlignment="Top"/> <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="本日のご予定は以下になります。" VerticalAlignment="Top" Margin="0,16,0,0"/> </Grid> </Window>
MainWindow.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { private DispatcherTimer m_Timer = null; private List<Schedule> ScheduleList = new List<Schedule>(); public MainWindow() { InitializeComponent(); // タイマーを作成する m_Timer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher); m_Timer.Interval = TimeSpan.FromMinutes(1); m_Timer.Tick += M_Timer_Tick; // タイマーの実行開始 m_Timer.Start(); Clock clk = new Clock(DateTime.Now.AddSeconds(45),"第三応接室にて会議は行われます。"); clk.Show(); this.Top = 0; this.Left = 0; } private void M_Timer_Tick(object sender, EventArgs e) { for(int i =0; i< ScheduleList.Count; i++) { Schedule sch = ScheduleList[i]; if (!sch.IsHandled && sch.Start.Ticks-DateTime.Now.Ticks < 1200000000) { //移動先を帰る //クロックを出す if (sch.Start.Ticks - DateTime.Now.Ticks > 0) { Clock clk = new Clock(sch.Start, sch.Location); clk.Show(); } sch.IsHandled = true; } else if(sch.IsHandled && DateTime.Now.Ticks- sch.Stop.Ticks > 0) { //在室に変える。 ScheduleList.Remove(sch); i--; } } } private void button_Click(object sender, RoutedEventArgs e) { textBox.Text = ""; Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = outlook.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder oFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); Microsoft.Office.Interop.Outlook.Items oItems = oFolder.Items; Microsoft.Office.Interop.Outlook.AppointmentItem oAppoint = oItems.GetFirst(); while (oAppoint != null) { if ( (oAppoint.Start <= DateTime.Today & DateTime.Today.AddDays(1) <= oAppoint.End) || //終日の予定? ((DateTime.Today < oAppoint.Start & oAppoint.Start < DateTime.Today.AddDays(1)) || (DateTime.Today < oAppoint.End & oAppoint.End < DateTime.Today.AddDays(1))) ) { textBox.Text += "Subject: " + oAppoint.Subject + "\r\n"; textBox.Text += "場所: " + oAppoint.Location + "\r\n"; textBox.Text += "開始時刻: " + oAppoint.Start.ToString("MM/dd")+" " + oAppoint.Start.ToShortTimeString() + "\r\n"; textBox.Text += "終了時刻: " + oAppoint.End.ToString("MM/dd") + " " + oAppoint.End.ToShortTimeString() + "\r\n"; textBox.Text += "---\r\n"; Schedule sch = new Schedule(); sch.Start = oAppoint.Start; sch.Stop = oAppoint.End; if (oAppoint.Location != "") { sch.Location = oAppoint.Location; } else { sch.Location = "不明"; } sch.Subject = oAppoint.Subject; ScheduleList.Add(sch); } oAppoint = oItems.GetNext(); } } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.Hide(); } private async void Window_Loaded(object sender, RoutedEventArgs e) { this.button_Click(sender, e); await Task.Run(() => { System.Threading.Thread.Sleep(5000); }); this.Hide(); } } public class Schedule { public DateTime Start; public DateTime Stop; public string Location; public string Subject; public bool IsHandled = false; } }
Clock.xaml
<Window x:Class="WpfApplication1.Clock" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="Clock" Height="52.75" Width="151.25" WindowStyle="None" Background="Transparent" Loaded="Window_Loaded" ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True"> <Grid x:Name="MainGrid" Opacity="1" Height="53" VerticalAlignment="Top" HorizontalAlignment="Left" Width="151"> <Grid.Background> <LinearGradientBrush EndPoint="1,0" MappingMode="RelativeToBoundingBox" StartPoint="0,0"> <GradientStop Color="#FF00D11C" Offset="1"/> <GradientStop Color="#FFD66B11"/> <GradientStop Color="#FFFFDB3A" Offset="0.325"/> <GradientStop Color="#FFFAFA37" Offset="0.548"/> <GradientStop Color="#FFE3F533" Offset="0.652"/> <GradientStop Color="#FFFCF338" Offset="0.423"/> </LinearGradientBrush> </Grid.Background> <Rectangle Fill="Black" HorizontalAlignment="Left" Height="27" Margin="11,16,0,0" Stroke="Black" VerticalAlignment="Top" Width="134"/> <Rectangle Fill="Black" HorizontalAlignment="Left" Height="12" Margin="93,4,0,0" Stroke="Black" VerticalAlignment="Top" Width="52"/> <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="会議時間まで" FontSize="10" VerticalAlignment="Top"/> <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="あと" VerticalAlignment="Top" FontSize="7" Margin="0,16,0,0"/> <TextBlock x:Name="Time" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding RemainingTime}" VerticalAlignment="Top" FontSize="30" Margin="-4,8,0,0" FontFamily="Quartz MS" Width="114" RenderTransformOrigin="0.5,0.5" Foreground="#FFEAE018"> <TextBlock.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="0.7"/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </TextBlock.RenderTransform> </TextBlock> <TextBlock x:Name="Locate" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Location}" VerticalAlignment="Top" FontSize="10" Margin="85,5,0,0" FontFamily="Quartz MS" Width="68" RenderTransformOrigin="0.5,0.5" Foreground="#FFEAE018" Height="36"> <TextBlock.RenderTransform> <TransformGroup> <ScaleTransform ScaleX="0.7"/> <SkewTransform/> <RotateTransform/> <TranslateTransform/> </TransformGroup> </TextBlock.RenderTransform> </TextBlock> </Grid> </Window>
Clock.xaml.cs
//Clock.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.ComponentModel; namespace WpfApplication1 { /// <summary> /// Clock.xaml の相互作用ロジック /// </summary> public partial class Clock : Window { public DateTime Start; public string Basyo; public Clock(DateTime StartTime,string Location) { Basyo = Location; Start = StartTime; InitializeComponent(); this.Top = 0; this.Left = 0; } private async void Window_Loaded(object sender, RoutedEventArgs e) { //文回せ DispItems DI = new DispItems(); DI.Location = Basyo; DI.RemainingTime = ""; this.DataContext = DI; bool IsEnd = false; await Task.Run(() => { string TimeString = ""; while (!IsEnd) { long RemainTics = Start.Ticks - DateTime.Now.Ticks; int min = (int)(RemainTics / 600000000); int sec = (int)((RemainTics - 600000000 * min) / 10000000); double subsec = (RemainTics - 600000000 * min - 10000000 * sec) / 100000; TimeString = min.ToString() + ":" + sec.ToString("00") + ":" + subsec.ToString("00"); DI.RemainingTime = TimeString; if (RemainTics < 0) { IsEnd = true; } System.Threading.Thread.Sleep(10); if (RemainTics < 300000000) { this.Dispatcher.Invoke(new Action(() => { Locate.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); Time.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); MainGrid.Background = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)); })); } } this.Dispatcher.Invoke(new Action(() => { Locate.Foreground = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50)); Time.Foreground = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50)); MainGrid.Background = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50)); })); DI.RemainingTime = "0:00:00"; System.Threading.Thread.Sleep(2000); }); this.Close(); } } public class DispItems: INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } private string _RemainingTime; public string RemainingTime { get { return this._RemainingTime; } set { if (value != this._RemainingTime) { this._RemainingTime = value; NotifyPropertyChanged("RemainingTime"); } } } private string _Location; public string Location { get { return this._Location; } set { if (value != this._Location) { this._Location = value; NotifyPropertyChanged("Location"); } } } } }