chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
 
namespace OpenTap.UnitTests
{
    [TestFixture]
    public class ScpiInstrumentTest
    {
        class DummyScpiIo : IScpiIO2
        {
            public ScpiIOResult DeviceClear()
            {
                errors.Clear();   
                return ScpiIOResult.Success;
            }
 
            public ScpiIOResult ReadSTB(ref byte stb)
            {
                throw new NotSupportedException();
            }
 
            public ScpiIOResult Read(ArraySegment<byte> buffer, int count, ref bool eoi, ref int read)
            {
                int offset = 0;
                while (responses.Count > 0 && offset < count)
                {
                    ((IList<byte>)buffer)[offset] = responses.Dequeue();
                    offset += 1;
                }
 
                read = offset;
 
                return ScpiIOResult.Success;
            }
 
            public ScpiIOResult Write(ArraySegment<byte> buffer, int count, ref int written)
            {
                var cmd = UTF8Encoding.UTF8.GetString(buffer.ToArray());
                written = count;
                HandleCommand(cmd);
                return ScpiIOResult.Success;
            }
 
            void response(string response)
            {
                foreach(var b in UTF8Encoding.UTF8.GetBytes(response))
                    responses.Enqueue(b);
            }
 
            TraceSource log = Log.CreateSource("ScpiIo");
            Queue<string> errors = new Queue<string>();
 
            public void PushError(int code, string err)
            {
                errors.Enqueue(string.Format("{0},\"{1}\"", code, err));
            }
            void HandleCommand(string cmd)
            {
                if (cmd == "*IDN?")
                {
                    response("DummyInstrument");
                }else if (cmd == "*RST")
                {
                    log.Debug("Reset");
                }else if (cmd == "SYST:ERR?")
                {
                    if (errors.Count == 0)
                        PushError(0, "No Error");
                    response(errors.Dequeue());
                    
                } else if (cmd == "*CLS")
                {
                    errors.Clear();
                    responses.Clear();
                }
                else
                {
                    PushError(100, "Unknown command.");
                }
            }
            
            Queue<byte> responses = new Queue<byte>();
 
            public ScpiIOResult Lock(ScpiLockType lockType, string sharedKey = null)
            {
                return ScpiIOResult.Success;
            }
 
            public ScpiIOResult Unlock()
            {
                return ScpiIOResult.Success;
            }
 
            public bool SendEnd { get; set; }
            public int IOTimeoutMS { get; set; }
            public int LockTimeoutMS { get; set; }
            public byte TerminationCharacter { get; set; }
            public bool UseTerminationCharacter { get; set; }
            public string ResourceClass { get; } = "instr";
            public ScpiIOResult Open(string visaAddress, bool @lock)
            {
                return ScpiIOResult.Success;
            }
 
            public ScpiIOResult Close()
            {
                return ScpiIOResult.Success;
            }
 
            public int ID { get; }
#pragma warning disable 67
            public event ScpiIOSrqDelegate SRQ;
#pragma warning restore 67
            public void OpenSRQ()
            {
                
            }
 
            public void CloseSRQ()
            {
                
            }
        }
        
        class DummyScpiInstrument : ScpiInstrument
        {
            public DummyScpiInstrument() : base(new DummyScpiIo())
            {
                IO.SRQ += sender => Log.Debug("SRQ"); // unused.
            }
            
            public DummyScpiIo IO => ((IScpiInstrument)this).IO as DummyScpiIo;
        }
 
        [Test]
        public void ScpiInstrumentOpenCloseNoStbError()
        {
            var scpi = new DummyScpiInstrument();
            scpi.Open();
            var eer = scpi.QueryErrors();
            Assert.AreEqual(0, eer.Count);
            scpi.IO.PushError(42, "Test Error");
            eer = scpi.QueryErrors();
            Assert.AreEqual(1, eer.Count);
            Assert.AreEqual(42, eer[0].Code);
            
            eer = scpi.QueryErrors();
            Assert.AreEqual(0, eer.Count);
            scpi.Close();
        }        
    }
}