Пример #1
0
 /// <summary>
 /// Show received data.
 /// </summary>
 private void Net1_OnReceived(object sender, ReceiveEventArgs e)
 {
     try
     {
         if (this.InvokeRequired)
         {
             this.BeginInvoke(new ReceivedEventHandler(Net1_OnReceived), new object[] { sender, e });
         }
         else
         {
             //Echo received text.
             if (EchoCB.Checked)
             {
                 Net1.Send(e.Data, e.SenderInfo);
                 ReceivedText.Text = "";
             }
             //We receive byte array from Gurux.Net and this must be changed to chars.
             if (HexCB.Checked)
             {
                 ++cnt;
                 ReceivedText.Text += BitConverter.ToString((byte[])e.Data);
             }
             else
             {
                 // Gets received data as string.
                 ReceivedText.Text += System.Text.Encoding.ASCII.GetString((byte[])e.Data);
             }
         }
     }
     catch (Exception Ex)
     {
         ErrorList.Items.Add(Ex.Message);
     }
 }
Пример #2
0
        void SendPush(GXDLMSPushSetup target)
        {
            int pos = target.Destination.IndexOf(':');

            if (pos == -1)
            {
                throw new ArgumentException("Invalid destination.");
            }
            GXDLMSNotify notify = new GXDLMSNotify(true, 1, 1, InterfaceType.WRAPPER);

            byte[][] data = notify.GeneratePushSetupMessages(DateTime.MinValue, target);
            string   host = target.Destination.Substring(0, pos);
            int      port = int.Parse(target.Destination.Substring(pos + 1));
            GXNet    net  = new GXNet(NetworkType.Tcp, host, port);

            try
            {
                net.Open();
                foreach (byte[] it in data)
                {
                    net.Send(it, null);
                }
            }
            finally
            {
                net.Close();
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            try
            {
                int   port  = 4059;
                GXNet media = new GXNet(NetworkType.Tcp, "localhost", port);
                GXDLMSSecureNotify notify = new GXDLMSSecureNotify(true, 16, 1, InterfaceType.WRAPPER);
                // Un-comment this if you want to send encrypted push messages.
                // notify.Ciphering.Security = Security.AuthenticationEncryption;
                GXDLMSPushSetup p     = new GXDLMSPushSetup();
                GXDLMSClock     clock = new GXDLMSClock();
                p.PushObjectList.Add(new KeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(p, new GXDLMSCaptureObject(2, 0)));
                p.PushObjectList.Add(new KeyValuePair <GXDLMSObject, GXDLMSCaptureObject>(clock, new GXDLMSCaptureObject(2, 0)));

                ///////////////////////////////////////////////////////////////////////
                //Create Gurux DLMS server component for Short Name and start listen events.
                GXDLMSPushListener pushListener = new GXDLMSPushListener(port);
                Console.WriteLine("Listening DLMS Push IEC 62056-47 messages on port " + port + ".");
                Console.WriteLine("Press X to close and Enter to send a Push message.");
                ConsoleKey key;
                while ((key = Console.ReadKey().Key) != ConsoleKey.X)
                {
                    if (key == ConsoleKey.Enter)
                    {
                        Console.WriteLine("Sending Push message.");
                        media.Open();
                        clock.Time = DateTime.Now;
                        foreach (byte[] it in notify.GeneratePushSetupMessages(DateTime.MinValue, p))
                        {
                            media.Send(it, null);
                        }
                        Thread.Sleep(100);
                        media.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            try
            {
                int port = 4059;
                GXNet media = new GXNet(NetworkType.Tcp, "localhost", port);
                GXDLMSNotify cl = new GXDLMSNotify(true, 1, 1, InterfaceType.WRAPPER);
                GXDLMSPushSetup p = new GXDLMSPushSetup();
                GXDLMSClock clock = new GXDLMSClock();
                p.PushObjectList.Add(new KeyValuePair<GXDLMSObject, GXDLMSCaptureObject>(p, new GXDLMSCaptureObject(2, 0)));
                p.PushObjectList.Add(new KeyValuePair<GXDLMSObject, GXDLMSCaptureObject>(clock, new GXDLMSCaptureObject(2, 0)));

                ///////////////////////////////////////////////////////////////////////
                //Create Gurux DLMS server component for Short Name and start listen events.
                GXDLMSPushListener pushListener = new GXDLMSPushListener(port);
                Console.WriteLine("Listening DLMS Push IEC 62056-47 messages on port " + port + ".");
                Console.WriteLine("Press X to close and Enter to send a Push message.");
                ConsoleKey key;
                while ((key = Console.ReadKey().Key) != ConsoleKey.X)
                {
                    if (key == ConsoleKey.Enter)
                    {
                        Console.WriteLine("Sending Push message.");
                        media.Open();
                        clock.Time = DateTime.Now;
                        foreach (byte[] it in cl.GeneratePushSetupMessages(DateTime.MinValue, p))
                        {
                            media.Send(it, null);
                        }
                        Thread.Sleep(100);
                        media.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }        
Пример #5
0
 void SendPush(GXDLMSPushSetup target)
 {
     int pos = target.Destination.IndexOf(':');
     if (pos == -1)
     {
         throw new ArgumentException("Invalid destination.");
     }
     GXDLMSNotify notify = new GXDLMSNotify(true, 1, 1, InterfaceType.WRAPPER);
     byte[][] data = notify.GeneratePushSetupMessages(DateTime.MinValue, target);
     string host = target.Destination.Substring(0, pos);
     int port = int.Parse(target.Destination.Substring(pos + 1));
     GXNet net = new GXNet(NetworkType.Tcp, host, port);
     try
     {
         net.Open();
         foreach (byte[] it in data)
         {
             net.Send(it, null);
         }
     }
     finally
     {
         net.Close();
     }
 }
Пример #6
0
        /// <summary>
        /// Read DLMS Data from the device.
        /// </summary>
        /// <param name="data">Data to send.</param>
        /// <returns>Received data.</returns>
        public void ReadDLMSPacket(byte[] data, GXReplyData reply)
        {
            if (data == null)
            {
                return;
            }
            reply.Error = 0;
            object eop = (byte)0x7E;

            //In network connection terminator is not used.
            if (Client.InterfaceType == InterfaceType.WRAPPER && Media is GXNet)
            {
                eop = null;
            }
            int  pos       = 0;
            bool succeeded = false;
            ReceiveParameters <byte[]> p = new ReceiveParameters <byte[]>()
            {
                Eop      = eop,
                Count    = 5,
                WaitTime = WaitTime,
            };

            lock (Media.Synchronous)
            {
                while (!succeeded && pos != 3)
                {
                    WriteTrace("<- " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(data, true));
                    Media.Send(data, null);
                    succeeded = Media.Receive(p);
                    if (!succeeded)
                    {
                        //If Eop is not set read one byte at time.
                        if (p.Eop == null)
                        {
                            p.Count = 1;
                        }
                        //Try to read again...
                        if (++pos != 3)
                        {
                            System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3");
                            continue;
                        }
                        throw new Exception("Failed to receive reply from the device in given time.");
                    }
                }
                try
                {
                    //Loop until whole COSEM packet is received.
                    while (!Client.GetData(p.Reply, reply))
                    {
                        //If Eop is not set read one byte at time.
                        if (p.Eop == null)
                        {
                            p.Count = 1;
                        }
                        while (!Media.Receive(p))
                        {
                            //If echo.
                            if (p.Reply.Length == data.Length)
                            {
                                Media.Send(data, null);
                            }
                            //Try to read again...
                            if (++pos != 3)
                            {
                                System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3");
                                continue;
                            }
                            throw new Exception("Failed to receive reply from the device in given time.");
                        }
                    }
                }
                catch (Exception ex)
                {
                    WriteTrace("-> " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(p.Reply, true));
                    throw ex;
                }
            }
            WriteTrace("-> " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(p.Reply, true));
            if (reply.Error != 0)
            {
                if (reply.Error == (short)ErrorCode.Rejected)
                {
                    Thread.Sleep(1000);
                    ReadDLMSPacket(data, reply);
                }
                else
                {
                    throw new GXDLMSException(reply.Error);
                }
            }
        }