public void Initialize() { Server = new CAServer(IPAddress.Parse("127.0.0.1")); Client = new CAClient(); Client.Configuration.SearchAddress = "127.0.0.1"; Client.Configuration.WaitTimeout = TIMEOUT; var countChange = new AutoResetEvent(false); long count = 3; IntArrayRecord = Server.CreateArrayRecord <CAIntArrayRecord>(IntArrayChannelName, 20); IntSubArrayRecord = Server.CreateSubArrayRecord(IntSubArrayChannelName, IntArrayRecord); for (var i = 0; i < IntArrayRecord.Value.Length; i++) { IntArrayRecord.Value[i] = i; } FloatSubArrayRecord = Server.CreateSubArrayRecord <CAFloatSubArrayRecord>(FloatSubArrayChannelName, 10); for (byte i = 0; i < FloatSubArrayRecord.FullArray.Length; i++) { FloatSubArrayRecord.FullArray[i] = i; } ByteSubArrayRecord = Server.CreateSubArrayRecord <CAByteSubArrayRecord>(ByteSubArrayChannelName, 35); var str = "Hello world"; var bytes = Encoding.ASCII.GetBytes(str); for (byte i = 0; i < bytes.Length; i++) { ByteSubArrayRecord.FullArray[i] = bytes[i]; } ByteSubArrayRecord.Scan = Constants.ScanAlgorithm.HZ10; void ProcessedHandler(object obj, EventArgs args) { Interlocked.Decrement(ref count); countChange.Set(); }; IntArrayRecord.RecordProcessed += ProcessedHandler; FloatSubArrayRecord.RecordProcessed += ProcessedHandler; ByteSubArrayRecord.RecordProcessed += ProcessedHandler; while (Interlocked.Read(ref count) > 0) { if (!countChange.WaitOne(TIMEOUT)) { Server.Dispose(); throw new Exception("Timed out"); } } Server.Start(); }
public static void Main(string[] args) { using (var server = new CAServer()) { var intArrChannel = server.CreateArrayRecord <CAIntArrayRecord>(ArrayChannel, 20); var subArrChannel = server.CreateSubArrayRecord(SubArrayChannel, intArrChannel); subArrChannel.Scan = ScanAlgorithm.SEC1; // Fill array for (var i = 0; i < 20; i++) { intArrChannel.Value[i] = i; } subArrChannel.Index = 0; subArrChannel.Length = 10; // Shift subarray position and length every second var counter = 0; subArrChannel.PrepareRecord += (s, e) => { counter++; counter = counter > 5 ? 0 : counter; subArrChannel.Index = counter; subArrChannel.Length = 10 + counter; }; server.Start(); Console.WriteLine("Server started"); using (var client = new CAClient()) { var arrayChannel = client.CreateChannel <int[]>(ArrayChannel); var subArrayChannel = client.CreateChannel <int[]>(SubArrayChannel); void Handler(Channel <int[]> s, int[] v) { Console.WriteLine($"{s.ChannelName}: {string.Join(", ", v)}"); }; arrayChannel.MonitorChanged += Handler; subArrayChannel.MonitorChanged += Handler; Console.ReadKey(); subArrayChannel.MonitorChanged -= Handler; arrayChannel.MonitorChanged -= Handler; } } }