Пример #1
0
 public override void Close()
 {
     base.Close();
     if (Media != null)
     {
         Media.Close();
     }
 }
 /// <summary>
 /// Initialize network connection settings.
 /// </summary>
 /// <returns></returns>
 void InitNet()
 {
     try
     {
         if (Parent.UseRemoteSerial)
         {
             InitializeIEC();
         }
         else
         {
             Media.Open();
         }
     }
     catch (Exception Ex)
     {
         if (Media != null)
         {
             Media.Close();
         }
         throw Ex;
     }
 }
Пример #3
0
 public void Disconnect()
 {
     if (media != null && media.IsOpen)
     {
         try
         {
             GXReplyData reply = new GXReplyData();
             ReadDLMSPacket(DisconnectRequest(), 1, reply);
         }
         finally
         {
             if (media != null)
             {
                 media.Close();
                 //Restore old settings.
                 if (media is GXSerial)
                 {
                     media.Settings = parent.StartMediaSettings;
                 }
             }
         }
     }
 }
 /// <summary>
 /// New media is selected.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void MediasCB_SelectedIndexChanged(object sender, System.EventArgs e)
 {
     try
     {
         if (MediasCB.SelectedIndex == -1)
         {
             return;
         }
         MediaFrame.Controls.Clear();                
         string mediaName = MediasCB.Items[MediasCB.SelectedIndex].ToString();
         if (!AvailableMedias.ContainsKey(mediaName))
         {
             SelectedMedia = m_GXDevice.GXClient.SelectMedia(mediaName);
             if (SelectedMedia == null)
             {
                 throw new Exception(mediaName + " media not found.");
             }
         }
         else
         {
             SelectedMedia = AvailableMedias[mediaName];
             SelectedMedia.Close();
         }
         if (m_GXDevice.GXClient.PacketParser != null)
         {
             m_GXDevice.GXClient.PacketParser.InitializeMedia(m_GXDevice.GXClient, SelectedMedia);
         }
         PropertiesForm = SelectedMedia.PropertiesForm;
         Gurux.Device.GXMediaTypeCollection mediatypes = m_GXDevice.AllowedMediaTypes;                
         Gurux.Device.GXMediaType tp = mediatypes[SelectedMedia.MediaType];
         try
         {
             string settings = null;
             //Find used media.
             if (Gurux.DeviceSuite.Properties.Settings.Default.EditorSelectedMedia != null &&
                 Gurux.DeviceSuite.Properties.Settings.Default.EditorSelectedMediaSettings != null)
             {
                 string newKey = m_GXDevice.ProtocolName + m_GXDevice.DeviceProfile;
                 newKey = newKey.GetHashCode().ToString();
                 foreach (string it in Gurux.DeviceSuite.Properties.Settings.Default.EditorSelectedMedia)
                 {
                     string[] tmp = it.Split(new char[] { '=' });
                     string key = tmp[0];                            
                     if (string.Compare(newKey, key) == 0)
                     {
                         newKey = m_GXDevice.ProtocolName + m_GXDevice.DeviceProfile + SelectedMedia.MediaType;
                         newKey = newKey.GetHashCode().ToString();
                         //Update media settings.
                         foreach (string it2 in Gurux.DeviceSuite.Properties.Settings.Default.EditorSelectedMediaSettings)
                         {
                             tmp = it2.Split(new char[] { '=' });
                             key = tmp[0];
                             if (string.Compare(newKey, key) == 0)
                             {
                                 settings = tmp[1];
                                 break;
                             }
                         }
                         break;
                     }
                 }
             }
             if (settings == null && tp != null)
             {
                 settings = tp.DefaultMediaSettings;
             }
             //Set default settings if set.
             if (settings != null)
             {
                 SelectedMedia.Settings = settings;
             }                   
         }
         catch
         {
             //It's OK if this fails.
         }
         ((IGXPropertyPage)PropertiesForm).Initialize();
         while (PropertiesForm.Controls.Count != 0)
         {
             Control ctr = PropertiesForm.Controls[0];
             if (ctr is Panel)
             {
                 if (!ctr.Enabled)
                 {
                     PropertiesForm.Controls.RemoveAt(0);
                     continue;
                 }
             }
             MediaFrame.Controls.Add(ctr);
         }
     }
     catch (Exception Ex)
     {
         GXCommon.ShowError(this.Parent, Ex);
     }
 }
Пример #5
0
        public override void ImportFromDevice(Control[] addinPages, GXDevice device, Gurux.Common.IGXMedia media)
        {
            GXIEC62056Device   dev = (GXIEC62056Device)device;
            ImportSelectionDlg dlg = addinPages[1] as ImportSelectionDlg;
            string             deviceSerialNumber = dlg.DeviceSerialNumber;
            int waittime = dev.WaitTime;

            media.Open();
            try
            {
                string data  = "/?" + deviceSerialNumber + "!\r\n";
                byte[] reply = IEC62056Parser.Identify(media, data, '\0', waittime);
                if (reply[0] != '/')
                {
                    throw new Exception("Invalid reply.");
                }
                char     baudRate   = (char)reply[4];
                string   CModeBauds = "0123456789";
                string   BModeBauds = "ABCDEFGHI";
                Protocol mode;
                if (CModeBauds.IndexOf(baudRate) != -1)
                {
                    mode = Protocol.ModeC;
                }
                else if (BModeBauds.IndexOf(baudRate) != -1)
                {
                    mode = Protocol.ModeB;
                }
                else
                {
                    mode     = Protocol.ModeA;
                    baudRate = '0';
                }
                if (reply[0] != '/')
                {
                    throw new Exception("Import failed. Invalid reply.");
                }
                //If mode is not given.
                if (dev.Mode == Protocol.None)
                {
                    dev.Mode = mode;
                }
                data = ASCIIEncoding.ASCII.GetString(reply.ToArray());
                string manufacturer = new string(new char[] { (char)reply[1], (char)reply[2], (char)reply[3] });
                if (dev.Mode == Protocol.ModeA)
                {
                    data = (char)0x06 + "0" + baudRate + "0\r\n";
                }
                else
                {
                    data = (char)0x06 + "0" + baudRate + "1\r\n";
                }
                //Note this sleep is in standard. Do not remove.
                if (media.MediaType == "Serial")
                {
                    System.Threading.Thread.Sleep(200);
                }
                reply = IEC62056Parser.ParseHandshake(media, data, baudRate, waittime);
                string header, frame;
                IEC62056Parser.GetPacket(new List <byte>(reply), true, out header, out frame);
                System.Diagnostics.Debug.WriteLine(frame);
                if (header == "B0")
                {
                    throw new Exception("Connection failed. Meter do not accept connection at the moment.");
                }
                //Password is asked.
                if (header == "P0")
                {
                    System.Diagnostics.Debug.WriteLine("Password is asked.");
                }
                //Note this sleep is in standard. Do not remove.
                if (media.MediaType == "Serial")
                {
                    System.Threading.Thread.Sleep(200);
                }
                if (dev.Mode == Protocol.ModeA)
                {
                    GXCategory defaultCategory = new GXIEC62056ReadoutCategory();
                    defaultCategory.Name = "Readout";
                    device.Categories.Add(defaultCategory);
                }
                else
                {
                    GXCategory defaultCategory = null;
                    defaultCategory      = new GXIEC62056Category();
                    defaultCategory.Name = "Properties";
                    device.Categories.Add(defaultCategory);
                    foreach (string it in IEC62056Parser.GetGeneralOBISCodes())
                    {
                        try
                        {
                            //Note this sleep is in standard. Do not remove.
                            if (media is Gurux.Serial.GXSerial)
                            {
                                System.Threading.Thread.Sleep(200);
                            }
                            if (!it.StartsWith("P."))
                            {
                                string value = IEC62056Parser.ReadValue(media, waittime, it + "()", 2);
                                if (!Convert.ToString(value).StartsWith("ER"))
                                {
                                    GXIEC62056Property prop = new GXIEC62056Property();
                                    prop.AccessMode = AccessMode.Read;
                                    prop.ReadMode   = dev.ReadMode;
                                    prop.WriteMode  = dev.WriteMode;
                                    prop.Name       = IEC62056Parser.GetDescription(it);
                                    prop.Data       = it;
                                    prop.DataType   = IEC62056Parser.GetDataType(it);
                                    if (prop.DataType == DataType.DateTime ||
                                        prop.DataType == DataType.Date ||
                                        prop.DataType == DataType.Time)
                                    {
                                        prop.ValueType = typeof(DateTime);
                                    }
                                    defaultCategory.Properties.Add(prop);
                                    TraceLine("Property " + prop.Name + " added.");
                                }
                            }
                            else
                            {
                                object[][] rows;
                                //Try to read last hour first.
                                TimeSpan add     = new TimeSpan(1, 0, 0);
                                DateTime start   = DateTime.Now.Add(-add);
                                string[] columns = null;
                                do
                                {
                                    try
                                    {
                                        rows = IEC62056Parser.ReadTable(media, waittime, it, start, DateTime.Now, null, 5, 1, out columns);
                                    }
                                    catch
                                    {
                                        //If media is closed.
                                        if (!media.IsOpen)
                                        {
                                            break;
                                        }
                                        rows = new object[0][];
                                    }
                                    if (rows.Length == 0)
                                    {
                                        if (add.TotalHours == 1)
                                        {
                                            //Try to read last day.
                                            add   = new TimeSpan(1, 0, 0, 0);
                                            start = DateTime.Now.Add(-add).Date;
                                        }
                                        else if (add.TotalHours == 24)
                                        {
                                            //Try to read last week.
                                            add   = new TimeSpan(7, 0, 0, 0);
                                            start = DateTime.Now.Add(-add).Date;
                                        }
                                        else if (add.TotalDays == 7)
                                        {
                                            //Try to read last month.
                                            add   = new TimeSpan(31, 0, 0, 0);
                                            start = DateTime.Now.Add(-add).Date;
                                        }
                                        else if (add.TotalDays == 31)
                                        {
                                            //Read all.
                                            add   = new TimeSpan(100, 0, 0, 0);
                                            start = DateTime.MinValue;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                        //Note this sleep is in standard. Do not remove.
                                        if (media is Gurux.Serial.GXSerial)
                                        {
                                            System.Threading.Thread.Sleep(200);
                                        }
                                    }
                                    else
                                    {
                                        GXIEC62056Table table = new GXIEC62056Table();
                                        table.Name       = IEC62056Parser.GetDescription(it);
                                        table.AccessMode = AccessMode.Read;
                                        table.Data       = it;
                                        table.ReadMode   = 6;
                                        int index = -1;
                                        foreach (string col in columns)
                                        {
                                            GXIEC62056Property prop = new GXIEC62056Property();
                                            prop.Name = col;
                                            //Mikko prop.Name = IEC62056Parser.GetDescription(col);
                                            prop.Data      = col;
                                            prop.ValueType = rows[0][++index].GetType();
                                            table.Columns.Add(prop);
                                        }
                                        device.Tables.Add(table);
                                        TraceLine("Property " + table.Name + " added.");
                                        break;
                                    }
                                }while (rows.Length == 0);
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }
                    }
                }
            }
            finally
            {
                if (media.MediaType == "Serial" || media.MediaType == "Terminal")
                {
                    IEC62056Parser.Disconnect(media, 2);
                }
                media.Close();
            }
        }