private void Disconnect(object sender, GXPacket packet)
        {
            GXIEC62056Device dev = null;

            if (sender is GXIEC62056Device)
            {
                dev = sender as GXIEC62056Device;
            }
            else if (sender is GXProperty)
            {
                dev = (sender as GXProperty).Device as GXIEC62056Device;
            }
            else
            {
                throw new Exception("InitRead failed. Unknown sender: " + sender.ToString());
            }
            if (dev.Mode != Protocol.ModeA)
            {
                packet.ResendCount = -1;
                packet.Eop         = null;
                packet.ChecksumSettings.Position = -3;
                packet.ChecksumSettings.Count    = -3;
                packet.AppendData("B0");
                packet.AppendData((byte)3);
            }
        }
		/// <summary>
		/// Initializes a new instance of the IEC62056DeviceModeWizardDlg class.
		/// </summary>
		public IEC62056DeviceModeWizardDlg(GXDevice device)
		{
			InitializeComponent();
            typeCb.Items.Add(Protocol.ModeA);
            typeCb.Items.Add(Protocol.ModeC);
			System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
			UpdateResources();
			m_Device = (GXIEC62056Device)device;
			this.TopLevel = false;
			this.FormBorderStyle = FormBorderStyle.None;
		}
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the IEC62056DeviceModeWizardDlg class.
 /// </summary>
 public IEC62056DeviceModeWizardDlg(GXDevice device)
 {
     InitializeComponent();
     typeCb.Items.Add(Protocol.ModeA);
     typeCb.Items.Add(Protocol.ModeC);
     System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
     UpdateResources();
     m_Device             = (GXIEC62056Device)device;
     this.TopLevel        = false;
     this.FormBorderStyle = FormBorderStyle.None;
 }
 private void KeepAlive(GXIEC62056Device device, GXPacket packet)
 {
     if (device.AliveMode == 0)
     {
         packet.AppendData("R2");
         packet.AppendData((byte)2);
         packet.AppendData("0.0.0()");
     }
     else
     {
         packet.AppendData("R" + device.AliveMode.ToString());
         packet.AppendData((byte)2);
         packet.AppendData(device.AliveData + "(" + device.AliveParameters + ")");
     }
 }
        GXIEC62056Device GetDevice(object sender)
        {
            GXIEC62056Device dev = null;

            if (sender is GXIEC62056Device)
            {
                dev = sender as GXIEC62056Device;
            }
            else if (sender is GXProperty)
            {
                dev = (sender as GXProperty).Device as GXIEC62056Device;
            }
            else
            {
                throw new Exception("InitRead failed. Unknown sender: " + sender.ToString());
            }
            return(dev);
        }
Пример #6
0
 public override Type[] GetCategoryTypes(object parent)
 {
     if (parent is GXDevice)
     {
         GXIEC62056Device dev = parent as GXIEC62056Device;
         if (dev.Mode == Protocol.ModeA)
         {
             return(new Type[] { typeof(GXIEC62056ReadoutCategory) });
         }
         else
         {
             return(new Type[] { typeof(GXIEC62056Category) });
         }
     }
     else
     {
         return(new Type[] { typeof(GXIEC62056ReadoutCategory), typeof(GXIEC62056Category) });
     }
 }
        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);
            }
        }
        private void KeepAliveReply(GXIEC62056Device sender, GXPacket[] packets)
        {
            List <byte> fullData = new List <byte>();

            foreach (GXPacket packet in packets)
            {
                object tmp = null;
                tmp = packet.ExtractData(typeof(byte[]), 0, -1);
                if (tmp is byte[])
                {
                    fullData.AddRange(tmp as byte[]);
                }
            }

            string val   = ASCIIEncoding.ASCII.GetString(fullData.ToArray());
            int    start = val.IndexOf('(');
            int    end   = val.LastIndexOf(')');

            if (start != -1 && end != -1)
            {
                val = val.Substring(start + 1, end - start - 1);
            }
        }
        private void KeepAliveReply(GXIEC62056Device sender, GXPacket[] packets)
        {
            List<byte> fullData = new List<byte>();
            foreach (GXPacket packet in packets)
            {
                object tmp = null;
                tmp = packet.ExtractData(typeof(byte[]), 0, -1);
                if (tmp is byte[])
                {
                    fullData.AddRange(tmp as byte[]);
                }
            }

            string val = ASCIIEncoding.ASCII.GetString(fullData.ToArray());
            int start = val.IndexOf('(');
            int end = val.LastIndexOf(')');
            if (start != -1 && end != -1)
            {
                val = val.Substring(start + 1, end - start - 1);
            }
        }
 private void KeepAlive(GXIEC62056Device device, GXPacket packet)
 {
     if (device.AliveMode == 0)
     {
         packet.AppendData("R2");
         packet.AppendData((byte)2);
         packet.AppendData("0.0.0()");
     }
     else
     {
         packet.AppendData("R" + device.AliveMode.ToString());
         packet.AppendData((byte)2);
         packet.AppendData(device.AliveData + "(" + device.AliveParameters + ")");
     }
 }
Пример #11
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();
            }
        }