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();
     }            
 }