-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
584 lines (573 loc) · 24.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
using System;
using System.Collections.Generic;
using Ivi.Visa.Interop;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.Text.RegularExpressions;
namespace ArbScripter {
public static class Strings {
public const string usage =
"Usage: ArbScripter <parameter options>\n"
+ "where <parameter options> is 0 or more of:\n"
+ " -i <instrument address or alias>\n"
+ " -e <python code to be evaluated>\n"
+ " -f <filename of script to be interpreted>\n"
+ "\n"
+ "Script files are line oriented. Use '\' at line end for continuation.\n"
+ "\n"
+ "The leading character determines action taken for a logical line:\n"
+ " '!' => send following text immediately as an instrument command. [a]\n"
+ " '&' => send following text as a buffered instrument command. [a]\n"
+ " '*' => send following text with length-prefixed parameter block. [a,b]\n"
+ " '?' => send following text immediately as an instrument query. [c]\n"
+ " '=' => evaluate following text or block as IronPython code. [d]\n"
+ " '@' => send following text appended by vector as binary block. [a,e]\n"
+ " '>' => output following text with brace-enclosed variables to console.\n"
+ " '<' => treat following text as filename and interpret file's lines.\n"
+ " '%' => following text is comment, no action to be taken.\n"
+ " '.' => terminate script execution. (good for debugging)\n"
+ "\n"
+ "[a. Within the text, brace-enclosed identifiers will be replaced,\n"
+ " (along with the braces), with the string representation of an object\n"
+ " referenced in the Python global namespace by the given identifier.\n"
+ " It is an abort-inducing error for the identifier to be undefined. ]\n"
+ "\n"
+ "[b. Parameter block after '#' will be prefixed by its length coded in\n"
+ " Keysight's strange <digit><length> format, where <digit> is a single\n"
+ " digit specifying the number of digits in the base-10 ASCII <length>,\n"
+ " which specifies the number of characters in remaining command text. ]\n"
+ "\n"
+ "[c. Text returned by instrument sent to stdout without other effect. ]\n"
+ "\n"
+ "[d. Text on same line is treated as a single-line code block. When the\n"
+ " remaining line is blank, following lines, up to but not including a\n"
+ " lone '_' character, are collected and evaluated as the code block. ]\n"
+ "\n"
+ "[e. The vector name may be specified with a brace-enclosed identifier\n"
+ " at the text's end, or the default name, 'samples', will be used. ]\n"
+ "\n"
+ "Python code block executions share the same global namespace, and may\n"
+ "access an instrument access object named 'arb' having these members:\n"
+ " void Command(string cmd, bool flush = true)\n"
+ " string Query(string cmd)\n"
+ " bool SendFloats(string lead, List<float> vf)\n"
+ " void SendBlock(string lead, float[] z)\n"
+ " bool SendWithParamLength(string text)\n"
+ " const string noErrString\n"
;
public static Regex varName = new Regex("\\{(\\w+)\\}");
public static Regex tailVarName = new Regex("\\{(\\w+)\\}$");
}
public class FloatList : List<Single> {
public static FloatList New(int capacity = 0){
return (capacity > 0)? new FloatList(capacity) : new FloatList();
}
FloatList() {
}
FloatList(int capacity) : base(capacity) {
}
public override string ToString() {
var sb = new System.Text.StringBuilder(6 + Count * 9 + (Count/8) + 1);
sb.Append("[\n");
for (int line = 0; line * 8 < Count; ++line) {
for (int i = line*8; i < Count && i < line*8 + 8; ++i) {
sb.AppendFormat(" {0:+0.00000; 0.00000;-0.00000}", this[i]);
}
sb.Append("\n");
}
sb.Append("]\n");
return sb.ToString();
}
}
public class Instrument : IDisposable {
IResourceManager IRM;
IVisaSession IVS;
IFormattedIO488 oFio;
bool pendingCmd;
bool faking;
public Instrument() {
IRM = new Ivi.Visa.Interop.ResourceManager();
oFio = new FormattedIO488();
pendingCmd = false;
faking = false;
}
public bool Open(string instAddress, int wtime = 5000) {
if (instAddress.ToLower() == "fake") {
faking = true;
return true;
}
try {
IVS = IRM.Open(instAddress, AccessMode.NO_LOCK);
if (IVS != null) {
oFio.IO = (IMessage)IVS;
oFio.IO.Timeout = wtime;
return true;
}
}
catch (Exception) {
}
return false;
}
public void Close() {
if (!faking)
{
IVS.Close();
IVS = null;
oFio.IO = null;
}
else
faking = false;
pendingCmd = false;
}
public bool IsOpen {
get { return IVS != null || faking; }
}
protected virtual void Dispose(bool disposing) {
if (disposing && IsOpen)
Close();
}
public void Dispose() {
Dispose(true);
}
public void Command(string cmd, bool flush = true) {
if (pendingCmd && cmd.Length > 0 && !faking) {
oFio.WriteString("\n", false);
}
if (!faking)
oFio.WriteString(cmd, flush);
pendingCmd = !flush;
}
public string Query(string cmd, string sayIfFake = noErrString) {
Command(cmd, true);
if (!faking)
return oFio.ReadString();
else
return sayIfFake;
}
public bool SendFloats(string lead, List<float> vf) {
if (IsOpen) {
SendBlock(lead, vf.ToArray());
return true;
}
else
return false;
}
public void SendBlock(string lead, float[] z) {
if (faking)
return;
if (pendingCmd) {
oFio.WriteString("\n", false);
}
if ((z == null) || z.Length == 0)
return;
oFio.WriteIEEEBlock(lead + " ", z, true);
// Wait for the operation to complete before moving on.
oFio.WriteString("*WAI", true);
pendingCmd = false;
}
public bool SendWithParamLength(string text) {
if (IsOpen) {
int isharp = text.IndexOf('#');
if (isharp >= 0) {
string lead = text.Substring(0, isharp + 1);
string tail = text.Substring(isharp + 1);
int ntc = tail.Length;
string nPrefix = ntc.ToString();
string npp = (nPrefix.Length).ToString();
string cmd = lead + npp + nPrefix + tail;
Command(cmd, true);
return true;
}
}
return false;
}
public const string noErrString = "+0,\"No error\"\n";
}
#if USE_FUNKY_VARS
public class Vars {
public double Pi { get { return Math.PI; } }
public Int32 TimeZeroSampleIndex { get; set; }
public Int32 GeneratorSampleFrequency { get; set; }
public double t(Int32 ns) {
return ((double)(ns - TimeZeroSampleIndex)) / GeneratorSampleFrequency;
}
public double Amplitude { get; set; }
public double F { get; set; }
public double Fs { get; set; }
public double Fo { get; set; }
public double Fd { get; set; }
public double W {
get { return 2.0 * Pi * F; }
set { F = value / (2.0 * Pi); }
}
public double Ws {
get { return 2.0 * Pi * Fs; }
set { Fs = value / (2.0 * Pi); }
}
public double Wo {
get { return 2.0 * Pi * Fo; }
set { Fo = value / (2.0 * Pi); }
}
public double Wd {
get { return 2.0 * Pi * Fd; }
set { Fd = value / (2.0 * Pi); }
}
public FloatList samples = new FloatList();
}
#endif
class Program : IDisposable {
private ScriptEngine m_engine = Python.CreateEngine();
private ScriptScope m_scope;
#if USE_FUNKY_VARS
Vars vars;
#endif
Instrument arb;
private int compileCount = 0;
string varSubstitute(string sIn) {
var m = Strings.varName.Matches(sIn);
int locAdjust = 0;
foreach (Match vn in m) {
string vname = ((System.Text.RegularExpressions.Match)(vn.Captures[0])).Groups[1].Value;
int vloc = vn.Index + locAdjust;
dynamic vval = null;
if (m_scope.TryGetVariable(vname, out vval)) {
string vput = vval.ToString();
sIn = sIn.Remove(vloc, vn.Value.Length).Insert(vloc, vput);
locAdjust += vput.Length - vn.Value.Length;
}
}
return sIn;
}
float[] tailArrayFetch(string tail, out string strippedTail, string ifAbsent = null) {
string an = ifAbsent;
Match m = Strings.tailVarName.Match(tail);
if (m == null) {
strippedTail = tail;
if (an == null)
return null;
}
else {
strippedTail = tail.Remove(m.Index, m.Value.Length);
an = m.Groups[1].Value;
}
dynamic vval;
if (m_scope.TryGetVariable(an, out vval)) {
try {
List<float> fa = vval as List<float>;
return fa.ToArray(); // Might be null.
}
catch (Exception) {
// Who cares, it was wrong!
}
}
return null;
}
bool Interpret(System.IO.TextReader lineSource, string sourceTag) {
bool done = false;
bool rv = true;
int lineCount = 0;
while (!done) {
var line = lineSource.ReadLine();
++lineCount;
while (line != null && line.Length > 0 && line[line.Length - 1] == '\\') {
line = line.Remove(line.Length - 1);
var addendum = lineSource.ReadLine();
++lineCount;
if (addendum != null)
line = line + addendum;
}
done = line == null || (line.Length == 1 && line[0] == '.');
if (!done && line.Length > 0) {
char c = line[0];
string tail = (line.Substring(1)).Trim();
switch (c) {
case '.':
done = true;
break;
case '*':
arb.SendWithParamLength(varSubstitute(tail));
break;
case '!': case '&':
if (arb.IsOpen)
arb.Command(varSubstitute(tail), c == '!');
break;
case '@':
if (arb.IsOpen) {
#if USE_FUNKY_VARS
arb.SendBlock(tail, vars.samples.ToArray());
#else
string strippedTail;
float[] fa = tailArrayFetch(tail, out strippedTail, "samples");
if (fa != null)
arb.SendBlock(varSubstitute(strippedTail), fa);
else
Console.WriteLine("Error: Lookup failed in '{0}'", tail);
#endif
}
break;
case '?':
if (arb.IsOpen) {
string reply = arb.Query(tail);
Console.WriteLine("Reply: {0}", reply);
}
break;
case '>': {
Console.WriteLine("{0}", varSubstitute(tail));
}
break;
case '=':
{
var code = tail;
var lineSay = lineCount;
if (code.Length == 0)
{
var lines = new List<string>();
string lin;
do
{
lin = lineSource.ReadLine();
++lineCount;
if (lin != null)
{
if (lin.Length > 0 && lin[0] == '_')
break;
lines.Add(lin);
}
} while (lin != null);
if (lines.Count > 0 && lin != null)
code = String.Join("\n", lines);
}
if (code.Length > 0)
{
try
{
object o = m_engine.Execute(code, m_scope);
}
catch (Microsoft.Scripting.SyntaxErrorException see)
{
Console.WriteLine("Error: Bad syntax in line {0}, column {1}", see.Line, see.Column);
Console.WriteLine("Code at line {0} of {1}, block #{2}.", lineSay, sourceTag, compileCount);
Console.WriteLine("(Could not compile |{0}|.)", see.SourceCode);
}
catch (IronPython.Runtime.Exceptions.SystemExitException see)
{
Console.WriteLine("Error exit: {0}", see.Message);
done = true;
rv = false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
break;
case '%':
break;
case '<':
if (tail.Length > 0) {
System.IO.TextReader fin = null;
try {
fin = System.IO.File.OpenText(tail);
}
catch (System.IO.FileNotFoundException e) {
rv = false;
done = true;
Console.WriteLine("ERROR: {0}", e.Message);
}
if (fin != null) {
using (fin) {
if (!Interpret(fin, tail))
{
rv = false;
done = true;
}
}
fin.Close();
}
}
break;
default:
continue;
}
}
}
return rv;
}
// This delegate acts like a FloatList class contructor in the IronPython execution context.
// There, it allows the expression 'FloatList()' or 'FloatList(capacity)' to return an object
// acceptable as a List<Single> and whose string representation shows the contained values.
public delegate FloatList NewFloatList(int cap = 0);
Program() {
m_scope = m_engine.CreateScope();
arb = new Instrument();
m_scope.SetVariable("arb", arb);
m_scope.SetVariable("FloatList", new NewFloatList(FloatList.New));
#if USE_FUNKY_VARS
vars = new Vars();
m_scope.SetVariable("vars", vars);
#else
m_scope.SetVariable("samples", FloatList.New());
#endif
string xpath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string xdir = System.IO.Directory.GetParent(xpath).FullName;
var spath = new List<string>();
spath.Add(xdir);
string ipaths = System.Environment.GetEnvironmentVariable("IRONPYTHONPATH");
if (ipaths != null && ipaths.Length > 0)
{
string[] ipa = ipaths.Split(';');
foreach (string d in ipa)
{
if (d.Length > 0)
spath.Add(d);
}
}
m_engine.SetSearchPaths(spath.ToArray());
}
bool Run(string[] args) {
bool rv = true;
bool done = false;
for (int i = 0; !done && i < args.Length; ++i) {
if (!(i + 1 < args.Length)) {
Console.WriteLine("Option {0} has no parameter.", args[i]);
break;
}
string param = args[++i];
switch (args[i-1]) {
case "-e":
try {
object o = m_engine.Execute(param, m_scope);
}
catch (IronPython.Runtime.Exceptions.SystemExitException see) {
Console.WriteLine("Error exit: {0}", see.Message);
done = true;
rv = false;
}
catch (Microsoft.Scripting.SyntaxErrorException see) {
Console.WriteLine("Error: Bad syntax in argument {0}, column {1}", i, see.Column);
Console.WriteLine("(Could not compile |{0}|.)", see.SourceCode);
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
break;
case "-f":
if (param == "-")
rv = rv && Interpret(Console.In, "<StdInput>");
else {
System.IO.TextReader fin = null;
try {
fin = System.IO.File.OpenText(param);
}
catch (System.IO.FileNotFoundException e) {
rv = false;
done = true;
Console.WriteLine("ERROR: {0}", e.Message);
}
if (fin != null) {
using (fin) {
if (!Interpret(fin, param)) {
rv = false;
done = true;
}
}
fin.Close();
}
}
break;
case "-i":
if (arb.IsOpen)
arb.Close();
if (!arb.Open(param)) {
Console.Error.WriteLine("Cannot open {0}", param);
return false;
}
break;
case "-?":
case "-h":
case "--help":
Console.WriteLine(Strings.usage);
done = true;
break;
default:
break;
}
}
if (rv && !arb.IsOpen) {
Console.Error.WriteLine("Instrument not opened.");
return false;
}
return rv;
}
static int Main(string[] args) {
if (args.Length == 1)
{
var reHelp = new System.Text.RegularExpressions.Regex("^((--?)|/)(([hH](elp)?)|\\?)$");
if (reHelp.IsMatch(args[0]))
{
Console.Write(Strings.usage);
return 0;
}
}
using (var p = new Program())
return p.Run(args) ? 0 : 2;
}
protected virtual void Dispose(bool disposing) {
if (disposing && arb.IsOpen)
arb.Close();
}
public void Dispose() {
Dispose(true);
}
//public static void BinaryArb()
//{
// var inst = new Instrument();
// const int NUM_DATA_POINTS = 100000;
// float[] z = new float[NUM_DATA_POINTS];
// //Create simple ramp waveform
// for (int i = 0; i < NUM_DATA_POINTS; i++)
// z[i] = (i - 1) / (float)NUM_DATA_POINTS;
// string instAddress = "USB0::2391::19207::MY53400207::0::INSTR";
// instAddress = "USB_Arb33612A";
// if (inst.Open(instAddress)) {
// try {
// string reply;
// reply = inst.Query("*IDN?");
// Console.WriteLine("Instrument Identity String: " + reply);
// //Clear and reset instrument
// inst.Query("*CLS;*RST;*OPC?");
// //Clear volatile memory
// inst.Command("SOURce1:DATA:VOLatile:CLEar", true);
// // swap the endian format
// inst.Command("FORM:BORD NORM", true);
// //Downloading
// Console.WriteLine("Downloading Waveform...");
// inst.SendBlock("SOURce1:DATA:ARBitrary testarb,", z);
// Console.WriteLine("Download Complete", true);
// //Set desired configuration
// inst.Command("SOURce1:FUNCtion:ARBitrary testarb", false); // set current arb waveform to defined arb pulse
// inst.Command("SOURce1:FUNCtion ARB", false); // turn on arb function
// inst.Command("SOURCE1:FUNCtion:ARB:SRATe 400000", false); // set sample rate
// inst.Command("SOURCE1:VOLT 2", false); // set max waveform amplitude to 2 Vpp
// inst.Command("SOURCE1:VOLT:OFFSET 0", false); // set offset to 0 V
// inst.Command("OUTPUT1:LOAD 50", false); // set output load to 50 ohms
// //Enable Output
// inst.Command("OUTPUT1 ON", true); // turn on channel 1 output
// //Read Error/s
// reply = inst.Query("SYSTEM:ERROR?");
// if (reply == Instrument.noErrString) {
// Console.WriteLine("Output set without any error\n");
// }
// else {
// Console.WriteLine("Error reported: " + reply);
// }
// }
// finally {
// inst.Close();
// }
// }
//}
}
}