internal byte[] ReadDLMSPacket(GXDLMSClient cosem, Gurux.Common.IGXMedia media, byte[] data, int wt) { if (data == null) { return null; } ReceiveParameters<byte[]> args = new ReceiveParameters<byte[]>() { Eop = (byte)0x7E, Count = 5, WaitTime = wt }; if (cosem.InterfaceType == InterfaceType.Net) { args.Eop = null; args.Count = 8; args.AllData = true; } int pos = 0; bool succeeded = false; lock (media.Synchronous) { media.Send(data, null); while (!succeeded && pos != 3) { succeeded = media.Receive(args); if (!succeeded) { //Try to read again... if (++pos != 3) { System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3"); continue; } string err = "Failed to receive reply from the device in given time."; GXLogWriter.WriteLog(err, (byte[])args.Reply); throw new Exception(err); } } //Loop until whole m_Cosem packet is received. while (!(succeeded = cosem.IsDLMSPacketComplete(args.Reply))) { if (!media.Receive(args)) { //Try to read again... if (++pos != 3) { System.Diagnostics.Debug.WriteLine("Data receive failed. Try to resend " + pos.ToString() + "/3"); continue; } string err = "Failed to receive reply from the device in given time."; GXLogWriter.WriteLog(err, (byte[])args.Reply); throw new Exception(err); } } } object[,] errors = cosem.CheckReplyErrors(data, args.Reply); if (errors != null) { int error = (int)errors[0, 0]; throw new GXDLMSException(error); } return args.Reply; }
/// <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 byte[] ReadDLMSPacket(byte[] data, int tryCount) { if (data == null) { return(null); } object eop = (byte)0x7E; //In network connection terminator is not used. if (m_Cosem.InterfaceType == InterfaceType.Net && Media is GXNet && !Parent.UseRemoteSerial) { eop = null; } int pos = 0; bool succeeded = false; ReceiveParameters <byte[]> p = new ReceiveParameters <byte[]>() { AllData = false, Eop = eop, Count = 5, WaitTime = Parent.WaitTime * 1000, }; lock (Media.Synchronous) { if (data != null) { Media.Send(data, null); } while (!succeeded && pos != 3) { succeeded = Media.Receive(p); if (!succeeded) { //Try to read again... if (++pos != tryCount) { //If Eop is not set read one byte at time. if (p.Eop == null) { p.Count = 1; } System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3"); Media.Send(data, null); continue; } string err = "Failed to receive reply from the device in given time."; GXLogWriter.WriteLog(err, p.Reply); throw new Exception(err); } } //Loop until whole COSEM packet is received. while (!m_Cosem.IsDLMSPacketComplete(p.Reply)) { //If Eop is not set read one byte at time. if (p.Eop == null) { p.Count = 1; } if (!Media.Receive(p)) { //Try to read again... if (++pos != tryCount) { System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3"); continue; } string err = "Failed to receive reply from the device in given time."; GXLogWriter.WriteLog(err, p.Reply); throw new Exception(err); } } } GXLogWriter.WriteLog("Received data", p.Reply); object errors = m_Cosem.CheckReplyErrors(data, p.Reply); if (errors != null) { object[,] arr = (object[, ])errors; int error = (int)arr[0, 0]; if (error == -1) { //If data is reply to the previous packet sent. //This might happens sometimes. if (m_Cosem.IsPreviousPacket(data, p.Reply)) { return(ReadDLMSPacket(null)); } else { throw new Exception(arr[0, 1].ToString()); } } throw new GXDLMSException(error); } return(p.Reply); }
/// <summary> /// Read DLMS Data from the device. /// </summary> /// <param name="data">Data to send.</param> /// <returns>Received data.</returns> public byte[] ReadDLMSPacket(byte[] data) { if (data == null) { return(null); } object eop = (byte)0x7E; //In network connection terminator is not used. if (Client.InterfaceType == InterfaceType.Net && Media is GXNet) { eop = null; } int pos = 0; bool succeeded = false; ReceiveParameters <byte[]> p = new ReceiveParameters <byte[]>() { AllData = true, 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."); } } //Loop until whole Cosem packet is received. while (!Client.IsDLMSPacketComplete(p.Reply)) { //If Eop is not set read one byte at time. if (p.Eop == null) { p.Count = 1; } if (!Media.Receive(p)) { //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."); } } } WriteTrace("-> " + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(p.Reply, true)); object errors = Client.CheckReplyErrors(data, p.Reply); if (errors != null) { object[,] arr = (object[, ])errors; int error = (int)arr[0, 0]; throw new GXDLMSException(error); } return(p.Reply); }