Esempio n. 1
0
        private async void buttonSendMessage_Click(object sender, EventArgs e)
        {
            if (_secsGem == null || _secsGem.State != SECS.ConnectionState.Selected)
            {
                MessageBox.Show("SECS State Must be Selected!!");
                return;
            }

            //if (string.IsNullOrWhiteSpace(txtSendPrimary.Text))
            //{
            //    MessageBox.Show("Send Message SML is Null!!");
            //    return;
            //}


            try
            {
                //string test = "QueryLoadPortAccessMode:'S1F3' W    <L[3]    <U2[1] 18011>    <U2[1] 18012>    <U2[1] 18013>    >";
                //var reply = await _secsGem.SendAsync(test.ToSecsMessage());
                string str   = txtSendPrimary.Text;
                var    reply = await _secsGem.SendAsync(txtSendPrimary.Text.ToSecsMessage());

                //txtRecvSecondary.Text = reply.ToSml();
            }
            catch (SecsException ex)
            {
                richTextBox2.AppendText(ex.Message + "\r\n");
            }
        }
Esempio n. 2
0
        public static Task <SecsMessage> SendMessage(this SecsGem secsGem, short deviceId, byte s, byte f, bool replyExpected, int systemBytes, Item item = null, string key = "", int value = 0)
        {
            SecsMessage secsMessage = new SecsMessage(s, f, replyExpected, GetFunctionName(s, f, key, value), item);

            CIMBASE.WriteLog(AATS.Log.LogLevel.INFO, "\r\n" + secsMessage.ToSml());
            if (systemBytes == -1)
            {
                return(secsGem.SendAsync(secsMessage));
            }
            else
            {
                return(secsGem.SendAsync(secsMessage, systemBytes));
            }
        }
Esempio n. 3
0
    public async Task SecsGem_SendAsync_And_Return_Secondary_Message()
    {
        var options = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 0,
        });

        using var secsGem1 = new SecsGem(options, pipeConnection1, Substitute.For <ISecsGemLogger>());
        using var secsGem2 = new SecsGem(options, pipeConnection2, Substitute.For <ISecsGemLogger>());

        var ping = new SecsMessage(s: 1, f: 13)
        {
            SecsItem = A("Ping"),
        };

        var pong = new SecsMessage(s: 1, f: 14, replyExpected: false)
        {
            SecsItem = A("Pong"),
        };

        using var cts = new CancellationTokenSource();
        _             = pipeConnection1.StartAsync(cts.Token);
        _             = pipeConnection2.StartAsync(cts.Token);
        _             = Task.Run(async() =>
        {
            var msg = await secsGem2.GetPrimaryMessageAsync(cts.Token).FirstAsync(cts.Token);
            msg.PrimaryMessage.Should().BeEquivalentTo(ping);
            await msg.TryReplyAsync(pong);
        });

        var reply = await secsGem1.SendAsync(ping, cts.Token);

        reply.Should().NotBeNull().And.BeEquivalentTo(pong);
    }
Esempio n. 4
0
        private void btnEnable_Click(object sender, EventArgs e)
        {
            _secsGem?.Dispose();
            _secsGem = new SecsGem(
                radioActiveMode.Checked,
                radioActiveMode.Checked?IPAddress.Parse(txtAddress.Text):IPAddress.Any,
                (int)numPort.Value,
                (int)numBufferSize.Value)
            {
                Logger = _logger, DeviceId = (ushort)numDeviceId.Value
            };
            _secsGem.LinkTestEnable     = true;
            _secsGem.LinkTestInterval   = 60000;
            _secsGem.ConnectionChanged += delegate
            {
                this.Invoke((MethodInvoker) delegate
                {
                    lbStatus.Text = _secsGem.State.ToString();
                    if (_secsGem.State == ConnectionState.Selected && _secsGem.IsActive)
                    {
                        _secsGem.SendAsync(new SecsMessage(1, 13, "Online Request", Item.L(Item.A(), Item.A())));
                    }
                });
            };

            Settings1.Default.ActiveMode = radioActiveMode.Checked;
            Settings1.Default.Save();

            _secsGem.PrimaryMessageReceived += PrimaryMessageReceived;

            btnEnable.Enabled = false;
            _secsGem.Start();
            btnDisable.Enabled = true;
        }
Esempio n. 5
0
 async void btnSend_Click(object sender, EventArgs e)
 {
     if (_secsGem == null)
     {
         MessageBox.Show("SECS/GEM not enable!");
         return;
     }
     try
     {
         EapLogger.Notice("Send by operator");
         await _secsGem.SendAsync(txtMsg.Text.ToSecsMessage());
     }
     catch (Exception ex)
     {
         EapLogger.Error(ex);
     }
 }
Esempio n. 6
0
        private async void btnSendPrimary_Click(object sender, EventArgs e)
        {
            if (_secsGem == null || _secsGem.State != ConnectionState.Selected)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(txtSendPrimary.Text))
            {
                return;
            }

            try
            {
                var reply = await _secsGem.SendAsync(txtSendPrimary.Text.ToSecsMessage());

                txtRecvSecondary.Text = reply.ToSml();
            }
            catch (SecsException ex)
            {
                txtRecvSecondary.Text = ex.Message;
            }
        }
Esempio n. 7
0
    public void SecsGem_SendAsync_With_Different_Device_Id()
    {
        var options1 = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 0,
        });
        var options2 = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 1,
        });

        using var secsGem1 = new SecsGem(options1, pipeConnection1, Substitute.For <ISecsGemLogger>());
        using var secsGem2 = new SecsGem(options2, pipeConnection2, Substitute.For <ISecsGemLogger>());

        var ping = new SecsMessage(s: 1, f: 13)
        {
            SecsItem = A("Ping"),
        };

        using var cts = new CancellationTokenSource();
        _             = pipeConnection1.StartAsync(cts.Token);
        _             = pipeConnection2.StartAsync(cts.Token);

        var receiver = Substitute.For <Action <SecsMessage> >();

        _ = Task.Run(async() =>
        {
            await foreach (var a in secsGem2.GetPrimaryMessageAsync(cts.Token))
            {
                // can't receive any message, reply S9F1 internally
                receiver(a.PrimaryMessage);
            }
        });

        Func <Task> sendAsync = async() =>
        {
            var reply = await secsGem1.SendAsync(ping, cts.Token);
        };

        sendAsync.Should().ThrowAsync <SecsException>().WithMessage(Resources.S9F1);
        receiver.DidNotReceive();
    }
Esempio n. 8
0
    public void SecsGem_SendAsync_With_T3_Timeout()
    {
        var options1 = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 0,
            T3       = 500,
        });

        using var secsGem1 = new SecsGem(options1, pipeConnection1, Substitute.For <ISecsGemLogger>());
        using var secsGem2 = new SecsGem(options1, pipeConnection2, Substitute.For <ISecsGemLogger>());

        var ping = new SecsMessage(s: 1, f: 13)
        {
            SecsItem = A("Ping"),
        };
        var pong = new SecsMessage(s: 1, f: 14, replyExpected: false)
        {
            SecsItem = A("Pong"),
        };

        using var cts = new CancellationTokenSource();

        _ = Task.Run(async() =>
        {
            await foreach (var a in secsGem2.GetPrimaryMessageAsync(cts.Token))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(options1.Value.T3 + 100)); // process delay over T3
                await a.TryReplyAsync(pong);
            }
        });

        var         receiver  = Substitute.For <Action>();
        Func <Task> sendAsync = async() =>
        {
            var reply = await secsGem1.SendAsync(ping, cts.Token);

            receiver();
        };

        sendAsync.Should().ThrowAsync <SecsException>().WithMessage(Resources.T3Timeout);
        receiver.DidNotReceive();
    }
Esempio n. 9
0
        void menuItemGemEnable_Click(object sender, EventArgs e)
        {
            EapLogger.Info("SECS/GEM Start");
            gemStatusLabel.Text = "Start";
            _secsGem?.Dispose();
            _secsGem = new SecsGem(
                EAPConfig.Instance.Mode == ConnectionMode.Active,
                IPAddress.Parse(EAPConfig.Instance.IP),
                EAPConfig.Instance.TcpPort, EAPConfig.Instance.SocketRecvBufferSize)
            {
                DeviceId         = EAPConfig.Instance.DeviceId,
                LinkTestInterval = EAPConfig.Instance.LinkTestInterval,
                T3     = EAPConfig.Instance.T3,
                T5     = EAPConfig.Instance.T5,
                T6     = EAPConfig.Instance.T6,
                T7     = EAPConfig.Instance.T7,
                T8     = EAPConfig.Instance.T8,
                Logger = new SecsLogger()
            };
            _secsGem.ConnectionChanged += delegate
            {
                this.Invoke((MethodInvoker) delegate
                {
                    EapLogger.Info("SECS/GEM " + _secsGem.State);
                    gemStatusLabel.Text        = _secsGem.State.ToString();
                    eqpAddressStatusLabel.Text = "Device IP: " + _secsGem.DeviceAddress;
                    if (_secsGem.State == ConnectionState.Selected)
                    {
                        _secsGem.SendAsync(new SecsMessage(1, 13, "TestCommunicationsRequest", Item.L()));
                    }
                });
            };

            _secsGem.PrimaryMessageReceived += PrimaryMsgHandler;
            _secsGem.Start();
            menuItemGemDisable.Enabled = true;
            menuItemGemEnable.Enabled  = false;
        }
Esempio n. 10
0
    private void SendAsyncManyMessagesAtOnce(ISecsConnection connection1, ISecsConnection connection2, CancellationToken cancellation)
    {
        var secsGem1 = new SecsGem(optionsActive, connection1, Substitute.For <ISecsGemLogger>());
        var secsGem2 = new SecsGem(optionsPassive, connection2, Substitute.For <ISecsGemLogger>());

        _ = Task.Run(async() =>
        {
            var pong = new SecsMessage(s: 1, f: 14, replyExpected: false)
            {
                SecsItem = A("Pong"),
            };

            await foreach (var a in secsGem2.GetPrimaryMessageAsync(cancellation))
            {
                await a.TryReplyAsync(pong);
            }
        });

        Func <Task> sendAsync = async() =>
        {
            var ping = new SecsMessage(s: 1, f: 13)
            {
                SecsItem = A("Ping"),
            };

            var sendCount  = 10000;
            var totalTasks = new List <Task <SecsMessage> >(capacity: sendCount);
            for (var g = 0; g < sendCount; g++)
            {
                totalTasks.Add(secsGem1.SendAsync(ping, cancellation).AsTask());
            }
            await Task.WhenAll(totalTasks.ToArray());
        };

        sendAsync.Should().NotThrowAsync();
    }
Esempio n. 11
0
 private void EstablishCommButton_Click(object sender, EventArgs e)
 {
     _secsGem.SendAsync(new SecsMessage(1, 13, "TestCommunicationsRequest", Item.L()));
 }
Esempio n. 12
0
 private void btnSendPrimaryMsg_Click(object sender, EventArgs e)
 {
     secsGem.SendAsync(this.primaryMsgToSend.Text.ToSecsMessage());
 }