/// <summary> /// Convert PDU to XML. /// </summary> private void PduToXmlBtn_Click(object sender, EventArgs e) { string xml = null; try { if (tabControl1.SelectedIndex == 3) { translator.DataToXml(DataPdu.Text, out xml); } else { UpdateSecurity(); XmlTB.Text = translator.PduToXml(PduTB.Text); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); } finally { DataXml.Text = xml; } }
/// <summary> /// Convert value data to XML. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ConvertMnu_Click(object sender, EventArgs e) { string xml = null; UpdateSecuritySettings(); try { translator.DataToXml(DataPdu.Text, out xml); } catch (Exception ex) { MessageBox.Show(this, ex.Message); } finally { DataXml.Text = xml; } }
/// <summary> /// Client has send data. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnReceived(object sender, Gurux.Common.ReceiveEventArgs e) { try { lock (this) { if (trace) { Console.WriteLine("<- " + Gurux.Common.GXCommon.ToHex((byte[])e.Data, true)); } reply.Set((byte[])e.Data); //Example handles only notify messages. GXReplyData data = new GXReplyData(); client.GetData(reply, data, notify); // If all data is received. if (notify.IsComplete && !notify.IsMoreData) { try { //Show data as XML. string xml; GXDLMSTranslator t = new GXDLMSTranslator(TranslatorOutputType.SimpleXml); t.DataToXml(notify.Data, out xml); Console.WriteLine(xml); // Print received data. PrintData(notify.Value, 0); //Example is sending list of push messages in first parameter. if (notify.Value is object[]) { object[] tmp = notify.Value as object[]; List <KeyValuePair <GXDLMSObject, int> > objects = client.ParsePushObjects((object[])tmp[0]); //Remove first item because it's not needed anymore. objects.RemoveAt(0); //Update clock. int Valueindex = 1; foreach (KeyValuePair <GXDLMSObject, int> it in objects) { client.UpdateValue(it.Key, it.Value, tmp[Valueindex]); ++Valueindex; //Print value Console.WriteLine(it.Key.ObjectType + " " + it.Key.LogicalName + " " + it.Value + ":" + it.Key.GetValues()[it.Value - 1]); } } Console.WriteLine("Server address:" + notify.ServerAddress + " Client Address:" + notify.ClientAddress); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { notify.Clear(); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <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 && !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) { eop = null; } int pos = 0; bool succeeded = false; ReceiveParameters <byte[]> p = new ReceiveParameters <byte[]>() { Eop = eop, WaitTime = WaitTime, }; if (eop == null) { p.Count = 8; } else { p.Count = 5; } GXByteBuffer rd = new GXByteBuffer(); lock (Media.Synchronous) { while (!succeeded && pos != 3) { if (!reply.IsStreaming()) { WriteTrace("TX:\t" + DateTime.Now.ToLongTimeString() + "\t" + GXCommon.ToHex(data, true)); Media.Send(data, null); } succeeded = Media.Receive(p); if (!succeeded) { if (++pos >= RetryCount) { throw new Exception("Failed to receive reply from the device in given time."); } //If Eop is not set read one byte at time. if (p.Eop == null) { p.Count = 1; } //Try to read again... System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3"); } } rd = new GXByteBuffer(p.Reply); try { pos = 0; //Loop until whole COSEM packet is received. while (!Client.GetData(rd, reply, notify)) { p.Reply = null; if (notify.IsComplete && notify.Data.Data != null) { //Handle notify. if (!notify.IsMoreData) { //Show received push message as XML. string xml; GXDLMSTranslator t = new GXDLMSTranslator(TranslatorOutputType.SimpleXml); t.DataToXml(notify.Data, out xml); Console.WriteLine(xml); notify.Clear(); continue; } } if (p.Eop == null) { p.Count = Client.GetFrameSize(rd); } while (!Media.Receive(p)) { if (++pos >= RetryCount) { throw new Exception("Failed to receive reply from the device in given time."); } //If echo. if (rd == null || rd.Size == data.Length) { Media.Send(data, null); } //Try to read again... System.Diagnostics.Debug.WriteLine("Data send failed. Try to resend " + pos.ToString() + "/3"); } rd.Set(p.Reply); } } catch (Exception ex) { WriteTrace("RX:\t" + DateTime.Now.ToLongTimeString() + "\t" + rd); throw ex; } } WriteTrace("RX:\t" + DateTime.Now.ToLongTimeString() + "\t" + rd); if (reply.Error != 0) { if (reply.Error == (short)ErrorCode.Rejected) { Thread.Sleep(1000); ReadDLMSPacket(data, reply); } else { throw new GXDLMSException(reply.Error); } } }