Beispiel #1
0
        /// <summary>
        /// Read DLMS Data from the device.
        /// </summary>
        /// <remarks>
        /// If access is denied return null.
        /// </remarks>
        /// <param name="data">Data to send.</param>
        /// <returns>Received data.</returns>
        public void ReadDLMSPacket(byte[] data, GXReplyData reply)
        {
            if ((data == null || data.Length == 0) && !reply.IsStreaming())
            {
                return;
            }
            GXReplyData notify = new GXReplyData();

            reply.Error = 0;
            object eop = (byte)0x7E;

            //In network connection terminator is not used.
            if (client.InterfaceType == InterfaceType.WRAPPER && !parent.UseRemoteSerial)
            {
                eop = null;
            }
            ReceiveParameters <byte[]> p = new ReceiveParameters <byte[]>()
            {
                AllData  = false,
                Eop      = eop,
                Count    = 5,
                WaitTime = parent.WaitTime * 1000,
            };
            var          answer = Send(data);
            GXByteBuffer rd     = new GXByteBuffer(answer.Body);

            try
            {
                //Loop until whole COSEM packet is received.
                while (!client.GetData(rd, reply, notify))
                {
                    p.Reply = null;
                    if (notify.Data.Size != 0)
                    {
                        // Handle notify.
                        if (!notify.IsMoreData)
                        {
                            rd.Trim();
                            notify.Clear();
                            p.Eop = eop;
                        }
                        continue;
                    }
                    //If Eop is not set read one byte at time.
                    if (p.Eop == null)
                    {
                        p.Count = client.GetFrameSize(rd);
                    }
                    rd.Set(p.Reply);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (reply.Error != 0)
            {
                throw new GXDLMSException(reply.Error);
            }
        }
Beispiel #2
0
        static public void WriteLog(string text, byte[] value)
        {
            string str;

            if (GXDLMSDirector.Properties.Settings.Default.LogTime)
            {
                str = DateTime.Now.ToLongTimeString() + " " + text;
            }
            else
            {
                str = text;
            }
            //Show data as hex.
            if ((LogLevel & 1) != 0)
            {
                if (value != null)
                {
                    str += Environment.NewLine + GXCommon.ToHex(value, true);
                }
                System.Diagnostics.Trace.WriteLine(str);
            }
            //Show data as xml.
            if (value != null && (LogLevel & 2) != 0)
            {
                receivedTraceData.Set(value);
                try
                {
                    GXByteBuffer  pdu  = new GXByteBuffer();
                    InterfaceType type = GXDLMSTranslator.GetDlmsFraming(receivedTraceData);
                    while (translator.FindNextFrame(receivedTraceData, pdu, type))
                    {
                        System.Diagnostics.Trace.WriteLine(translator.MessageToXml(receivedTraceData));
                        receivedTraceData.Trim();
                    }
                }
                catch (Exception)
                {
                    receivedTraceData.Clear();
                }
            }
        }