|
| 1 | +using Python.Runtime; |
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +namespace TLKCoreExample |
| 7 | +{ |
| 8 | + class ExampleMain |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + // Getting Python execute environment from environment: "Path" |
| 13 | + string env = Environment.GetEnvironmentVariable("Path"); |
| 14 | + string[] array = env.Split(new[] { ";" }, StringSplitOptions.None); |
| 15 | + string pathToVirtualEnv = ""; |
| 16 | + // Asign your Python version |
| 17 | + string PyVer = "38"; |
| 18 | + foreach (var path in array) |
| 19 | + { |
| 20 | + if (path.Contains("Python"+ PyVer+"\\") && !path.Contains("Script")) { |
| 21 | + pathToVirtualEnv = path; |
| 22 | + break; |
| 23 | + } |
| 24 | + } |
| 25 | + Console.WriteLine($"Python installed path: {pathToVirtualEnv}\n"); |
| 26 | + |
| 27 | + // Setting relative environment for execute Python, please update parameters from your real Python version |
| 28 | + Runtime.PythonDLL = Path.Combine(pathToVirtualEnv, "python"+ PyVer + ".dll"); |
| 29 | + PythonEngine.PythonHome = Path.Combine(pathToVirtualEnv, "python.exe"); |
| 30 | + |
| 31 | + // Set default Python lib path and path to import TLKCore |
| 32 | + PythonEngine.PythonPath = $".;lib;{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib;{pathToVirtualEnv}\\DLLs"; |
| 33 | + |
| 34 | + PythonEngine.Initialize(); |
| 35 | + using (Py.GIL()) |
| 36 | + { |
| 37 | + // Import modules which we need |
| 38 | + dynamic tlkcoreIns = Py.Import("TLKCoreService"); |
| 39 | + dynamic tmy_public = Py.Import("TMYPublic"); |
| 40 | + |
| 41 | + // Please keep this instance |
| 42 | + dynamic service = tlkcoreIns.TLKCoreService(); |
| 43 | + Console.WriteLine("TLKCore version: v" + service.queryTLKCoreVer()); |
| 44 | + |
| 45 | + dynamic dev_interface = tmy_public.DevInterface.ALL; |
| 46 | + dynamic ret = service.scanDevices(dev_interface.value); |
| 47 | + dynamic scanlist = ret.RetData; |
| 48 | + Console.WriteLine("Scanned device list: " + scanlist); |
| 49 | + |
| 50 | + ExampleMain example = new ExampleMain(); |
| 51 | + foreach (string sn_addr_type in scanlist) |
| 52 | + { |
| 53 | + dynamic element = sn_addr_type.Split(','); |
| 54 | + string sn = element[0]; |
| 55 | + string addr = element[1]; |
| 56 | + string dev_type = element[2]; |
| 57 | + ret = service.initDev(sn); |
| 58 | + if (ret.RetCode.value != tmy_public.RetCode.OK.value) |
| 59 | + { |
| 60 | + Console.WriteLine("Init failed, skip it"); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + string dev_name = service.getDevTypeName(sn); |
| 65 | + if (dev_name.Contains("BBox")) { |
| 66 | + dev_name = "BBox"; |
| 67 | + } |
| 68 | + // Invoke example methods |
| 69 | + Object[] param = { sn, service }; |
| 70 | + Console.WriteLine("Going to test " + dev_name); |
| 71 | + example.GetType().InvokeMember("Test"+dev_name, BindingFlags.InvokeMethod, Type.DefaultBinder, example, param); |
| 72 | + } |
| 73 | + |
| 74 | + Console.WriteLine("Presss any key to exit ..."); |
| 75 | + Console.ReadKey(); |
| 76 | + } |
| 77 | + } |
| 78 | + public void TestBBox(string sn, dynamic service) |
| 79 | + { |
| 80 | + dynamic tmy_public = Py.Import("TMYPublic"); |
| 81 | + |
| 82 | + dynamic mode = tmy_public.RFMode.TX; |
| 83 | + dynamic ret = service.setRFMode(sn, mode); |
| 84 | + Console.WriteLine("Set RF mode: {0}", ret.RetCode); |
| 85 | + ret = service.getRFMode(sn); |
| 86 | + Console.WriteLine("Get RF mode: {0}", ret); |
| 87 | + |
| 88 | + // Convert Python: list to C# array [] |
| 89 | + dynamic freqList = service.getFrequencyList(sn).RetData; |
| 90 | + Console.WriteLine("Freq list: " + freqList); |
| 91 | + |
| 92 | + // Please edit your target freq |
| 93 | + Double targetFreq = 28.0; |
| 94 | + bool found = false; |
| 95 | + foreach (dynamic f in freqList) |
| 96 | + { |
| 97 | + if (f == targetFreq) |
| 98 | + { |
| 99 | + found = true; |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + if (!found) |
| 104 | + { |
| 105 | + Console.WriteLine("Not support your target freq:{0} in freq list!", targetFreq); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + ret = service.setOperatingFreq(sn, targetFreq); |
| 110 | + if (ret.RetCode.value != tmy_public.RetCode.OK.value) |
| 111 | + { |
| 112 | + Console.WriteLine("Set freq failed: " + ret); |
| 113 | + return; |
| 114 | + } |
| 115 | + Console.WriteLine("Set freq: {0}", ret); |
| 116 | + |
| 117 | + // Gain setting for BBoxOne/Lite |
| 118 | + dynamic rng = service.getDR(sn, mode).RetData; |
| 119 | + Console.WriteLine("DR range: " + rng); |
| 120 | + |
| 121 | + // Select AAKit, please call getAAKitList() to fetch all AAKit list in files/ |
| 122 | + bool aakit_selected = false; |
| 123 | + string[] aakitList = (string[])service.getAAKitList(sn).RetData; |
| 124 | + foreach (string aakit in aakitList) |
| 125 | + { |
| 126 | + if (aakit.Contains("4x4")) |
| 127 | + { |
| 128 | + Console.WriteLine("Select AAKit: {0}: {1}", aakit, service.selectAAKit(sn, aakit).name); |
| 129 | + aakit_selected = true; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + if (!aakit_selected) |
| 134 | + Console.WriteLine("PhiA mode"); |
| 135 | + |
| 136 | + // ------- Get basic informations ------- |
| 137 | + Double gain_max = rng[1]; |
| 138 | + |
| 139 | + // Set IC channel gain, we use board 1 (its index in com_dr is 0) as example |
| 140 | + dynamic board_count = service.getBoardCount(sn).RetData; |
| 141 | + int board = 1; |
| 142 | + Console.WriteLine("Selected board: {0}/{1}", board, board_count); |
| 143 | + |
| 144 | + dynamic com_dr = service.getCOMDR(sn).RetData; |
| 145 | + dynamic common_gain_rng = com_dr[mode.value][board - 1]; |
| 146 | + // Here we takes the maximum common gain as example |
| 147 | + dynamic common_gain_max = common_gain_rng[1]; |
| 148 | + dynamic ele_dr_limit = service.getELEDR(sn).RetData[mode.value][board - 1]; |
| 149 | + Console.WriteLine("Board:{0} common gain range: {1}, and element gain limit: {2}", board, common_gain_rng, ele_dr_limit); |
| 150 | + |
| 151 | + // ------- Beam control example ------- |
| 152 | + if (aakit_selected) |
| 153 | + { |
| 154 | + //Passing: gain, theta, phi |
| 155 | + Console.WriteLine("SetBeamAngle: " + service.setBeamAngle(sn, gain_max, 0, 0)); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + Console.WriteLine("PhiA mode cannot process beam steering"); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public void TestUDBox(string sn, dynamic service) |
| 164 | + { |
| 165 | + dynamic tmy_public = Py.Import("TMYPublic"); |
| 166 | + dynamic UDState = tmy_public.UDState; |
| 167 | + |
| 168 | + Console.WriteLine("PLO state: " + service.getUDState(sn, UDState.PLO_LOCK).RetData); |
| 169 | + Console.WriteLine("All state: " + service.getUDState(sn).RetData); |
| 170 | + |
| 171 | + Console.WriteLine(service.setUDState(sn, 0, UDState.CH1)); |
| 172 | + Console.WriteLine("Wait for CH1 OFF"); |
| 173 | + Console.ReadKey(); |
| 174 | + Console.WriteLine(service.setUDState(sn, 1, UDState.CH1)); |
| 175 | + Console.WriteLine("Check CH1 is ON"); |
| 176 | + Console.ReadKey(); |
| 177 | + // Other setate options |
| 178 | + Console.WriteLine(service.setUDState(sn, 1, UDState.CH2)); |
| 179 | + Console.WriteLine(service.setUDState(sn, 1, UDState.OUT_10M)); |
| 180 | + Console.WriteLine(service.setUDState(sn, 1, UDState.OUT_100M)); |
| 181 | + Console.WriteLine(service.setUDState(sn, 1, UDState.PWR_5V)); |
| 182 | + Console.WriteLine(service.setUDState(sn, 1, UDState.PWR_9V)); |
| 183 | + |
| 184 | + // Passing: LO, RF, IF, Bandwidth with kHz |
| 185 | + Double LO = 24e6; |
| 186 | + Double RF = 28e6; |
| 187 | + Double IF = 4e6; |
| 188 | + Double BW = 1e5; |
| 189 | + // A check function, should be false -> not harmonic |
| 190 | + Console.WriteLine("Check harmonic: " + service.getHarmonic(sn, LO, RF, IF, BW).RetData); |
| 191 | + // SetUDFreq also includes check function |
| 192 | + dynamic ret = service.setUDFreq(sn, LO, RF, IF, BW); |
| 193 | + Console.WriteLine("Freq config: " + ret); |
| 194 | + } |
| 195 | + public void TestUDM(string sn, dynamic service) |
| 196 | + { |
| 197 | + dynamic tmy_public = Py.Import("TMYPublic"); |
| 198 | + |
| 199 | + dynamic ret = service.getUDState(sn); |
| 200 | + if (ret.RetCode.value != tmy_public.RetCode.OK.value) |
| 201 | + { |
| 202 | + Console.WriteLine("Error to get UDM state:" + ret); |
| 203 | + return; |
| 204 | + } |
| 205 | + Console.WriteLine("UDM state: " + ret); |
| 206 | + |
| 207 | + // Passing: LO, RF, IF, Bandwidth with kHz |
| 208 | + Double LO = 7e6; |
| 209 | + Double RF = 10e6; |
| 210 | + Double IF = 3e6; |
| 211 | + Double BW = 1e5; |
| 212 | + ret = service.setUDFreq(sn, LO, RF, IF, BW); |
| 213 | + Console.WriteLine("Set UDM freq to {0}: {1}", LO, ret.RetCode); |
| 214 | + |
| 215 | + ret = service.getUDFreq(sn); |
| 216 | + Console.WriteLine("UDM current freq: " + ret); |
| 217 | + |
| 218 | + dynamic source = tmy_public.UDM_REF.INTERNAL; |
| 219 | + |
| 220 | + // A case to set internal source to output |
| 221 | + dynamic supported = service.getRefFrequencyList(sn, source).RetData; |
| 222 | + Console.WriteLine("Supported internal reference clock(KHz): {0}", supported); |
| 223 | + dynamic output_freq = supported[0]; |
| 224 | + Console.WriteLine("Enable UDM ref output({0}KHz): {1}", output_freq, service.setOutputReference(sn, true, output_freq)); |
| 225 | + Console.WriteLine("Get UDM ref ouput: {0}", service.getOutputReference(sn)); |
| 226 | + |
| 227 | + Console.WriteLine("Press ENTER to disable output"); |
| 228 | + Console.ReadKey(); |
| 229 | + |
| 230 | + Console.WriteLine("Disable UDM ref output({0}KHz): {1}", output_freq, service.setOutputReference(sn, true, output_freq)); |
| 231 | + Console.WriteLine("Get UDM ref ouput: {0}", service.getOutputReference(sn)); |
| 232 | + |
| 233 | + // A case to change reference source to EXTERNAL |
| 234 | + source = tmy_public.UDM_REF.EXTERNAL; |
| 235 | + // Get external reference source supported list |
| 236 | + supported = service.getRefFrequencyList(sn, source).RetData; |
| 237 | + Console.WriteLine("Supported external reference clock(KHz): {0}", supported); |
| 238 | + // Try to change reference source to external: 10M |
| 239 | + ret = service.setRefSource(sn, source, supported[0]); |
| 240 | + Console.WriteLine("Change UDM ref source to {0} -> {1} with freq: {2}", source, ret, supported[0]); |
| 241 | + |
| 242 | + Console.WriteLine("\r\nWaiting for external reference clock input...\n"); |
| 243 | + Console.ReadKey(); |
| 244 | + |
| 245 | + // Check last state |
| 246 | + dynamic refer = tmy_public.UDMState.REF_LOCK; |
| 247 | + dynamic lock_state = service.getUDState(sn, refer.value); |
| 248 | + Console.WriteLine("UDM current reference status: {0}", lock_state); |
| 249 | + } |
| 250 | + } |
| 251 | +} |
0 commit comments