예제 #1
0
        /// <summary>
        /// Initializes a new instance of the DlmsTypeWizardDlg class.
        /// </summary>
        public IEC62056TypeWizardDlg(GXTable table)
        {
            InitializeComponent();

            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            UpdateResources();
            m_Table = (GXIEC62056Table)table;
        }
		/// <summary>
		/// Initializes a new instance of the DlmsTypeWizardDlg class.
		/// </summary>
		public IEC62056TypeWizardDlg(GXTable table)
		{
			InitializeComponent();

			System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
			UpdateResources();
			m_Table = (GXIEC62056Table)table;
		}
        private void ReadTableData(object sender, GXPacket packet)
        {
            GXIEC62056Table table = sender as GXIEC62056Table;
            DateTime        start, end;

            (sender as IGXPartialRead).GetStartEndTime(out start, out end);
            packet.AppendData(IEC62056Parser.GenerateReadTable(table.Data, start, end, table.Parameters, table.ReadMode, table.ReadCount));
        }
 private void ReadTableDataReply(object sender, GXPacket[] packets)
 {
     if ((sender as GXIEC62056Table).ReadMode != 6)
     {
         GXIEC62056Table table = (GXIEC62056Table)sender;
         object[][]      rows  = Parser.IEC62056Parser.ParseTableData(packets[packets.Length - 1].ExtractPacket(), ref Columns, ref Status, ref Time, ref Add, table.Data);
         table.AddRows(table.RowCount, new List <object[]>(rows), false);
     }
 }
 private bool IsTableRead(object sender, GXPacket packet)
 {
     if ((sender as GXIEC62056Table).ReadMode == 6)
     {
         GXIEC62056Table table = (GXIEC62056Table)sender;
         object[][]      rows  = Parser.IEC62056Parser.ParseTableData(packet.ExtractPacket(), ref Columns, ref Status, ref Time, ref Add, table.Data);
         System.Diagnostics.Debug.WriteLine("Mikko " + rows.Length);
         table.AddRows(table.RowCount, new List <object[]>(rows), false);
     }
     return((byte)packet.Eop != 0x4);
 }
        public void ExecuteSendCommand(object sender, string command, GXPacket packet)
        {
            switch (command)
            {
            case "ReadTableData":
                Status  = 0;
                Add     = 0;
                Columns = null;
                GXIEC62056Table table = (GXIEC62056Table)sender;
                table.ClearRows();
                ReadTableData(sender, packet);
                break;

            case "ReadTableDataNext":
                ReadTableDataNext(sender, packet);
                break;

            case "Connect":
                GXIEC62056Device dev = GetDevice(sender);
                Connect(dev.SerialNumber, dev.Password, dev.Mode, dev.GXClient.Media, dev.WaitTime);
                break;

            case "Disconnect":
                Disconnect(GetDevice(sender), packet);
                break;

            case "Readout":
                Readout(sender, packet);
                break;

            case "ReadData":
                ReadData(sender, packet);
                break;

            case "KeepAlive":
                KeepAlive(sender as GXIEC62056Device, packet);
                break;

            default:
                throw new Exception("ExecuteCommand failed. Unknown command: " + command);
            }
        }
 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();
     }            
 }
예제 #8
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();
            }
        }