示例#1
0
 /// <summary>
 /// Send data.
 /// </summary>
 /// <param name="eventSender"></param>
 /// <param name="eventArgs"></param>
 private void SendBtn_Click(System.Object eventSender, System.EventArgs eventArgs)
 {
     try
     {
         ReceivedText.Text = string.Empty;
         if (SyncBtn.Checked) // Sends data synchronously.
         {
             if (HexCB.Checked)
             {
                 // Sends data as byte array.
                 lock (Net1.Synchronous)
                 {
                     Gurux.Common.ReceiveParameters <byte[]> p = new Gurux.Common.ReceiveParameters <byte[]>()
                     {
                         WaitTime = Convert.ToInt32(WaitTimeTB.Text),
                         Eop      = EOPText.Text,
                     };
                     Net1.Send(ASCIIEncoding.ASCII.GetBytes(SendText.Text), null);
                     if (Net1.Receive(p))
                     {
                         ReceivedText.Text = Convert.ToString(p.Reply);
                     }
                 }
             }
             else
             {
                 // Sends data as ASCII string.
                 lock (Net1.Synchronous)
                 {
                     Gurux.Common.ReceiveParameters <string> p = new Gurux.Common.ReceiveParameters <string>()
                     {
                         WaitTime = Convert.ToInt32(WaitTimeTB.Text),
                         Eop      = EOPText.Text,
                     };
                     Net1.Send(SendText.Text, null);
                     if (Net1.Receive(p))
                     {
                         ReceivedText.Text = Convert.ToString(p.Reply);
                     }
                 }
             }
         }
         else // Sends data asynchronously.
         {
             if (HexCB.Checked)
             {
                 // Sends data as byte array.
                 Net1.Send(ASCIIEncoding.ASCII.GetBytes(SendText.Text), null);
             }
             else
             {
                 // Sends data as ASCII string.
                 Net1.Send(SendText.Text, null);
             }
         }
     }
     catch (Exception Ex)
     {
         //disable timer is exists
         if (IntervalTimer.Enabled)
         {
             IntervalBtn_Click(IntervalBtn, new System.EventArgs());
         }
         MessageBox.Show(Ex.Message);
     }
 }
示例#2
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);
                }
            }
        }