Ejemplo n.º 1
0
 internal void CreateColumns(Gurux.Common.IGXMedia media, GXDLMSDevice Device, GXManufacturer man, int wt, GXDLMSClient cosem, IGXManufacturerExtension Extension, GXCategory dataItems, GXCategory registers, GXDLMSObject it, GXDLMSTable table)
 {            
 }
        void InitRead(object sender, GXPacket GXPacket)
        {
            LastNotifiedTransactionProgress = 0;
            parser = new Gurux.DLMS.GXDLMSClient();
            GXDLMSDevice device = sender as GXDLMSDevice;
            parser.UseLogicalNameReferencing = device.UseLogicalNameReferencing;
            Gurux.Common.IGXMedia media = device.GXClient.Media as Gurux.Common.IGXMedia;
            SupportNetworkSpecificSettings = device.SupportNetworkSpecificSettings && media is GXNet;            
            if (SupportNetworkSpecificSettings)
            {
                parser.InterfaceType = Gurux.DLMS.InterfaceType.Net;
            }
            else
            {
                media.Eop = (byte)0x7E;
            }
            if (device.Manufacturers == null)
            {
                device.Manufacturers = new GXManufacturerCollection();
                GXManufacturerCollection.ReadManufacturerSettings(device.Manufacturers);
            }
            GXManufacturer man = device.Manufacturers.FindByIdentification(device.Identification);
			if (man == null)
			{
				throw new Exception("Unknown DLMS manufacturer type: " + device.Identification);
			}
            if (!string.IsNullOrEmpty(man.Extension))
            {
                Type t = Type.GetType(man.Extension);
                Extension = Activator.CreateInstance(t) as IGXManufacturerExtension;
            }            
            if (media is GXSerial && device.StartProtocol == StartProtocolType.IEC)
            {                                
                byte Terminator = 0xA;
                GXSerial serial = media as GXSerial;
                serial.Eop = Terminator;
                ReceiveParameters<string> p = new ReceiveParameters<string>()
                {
                    Eop = Terminator,                    
                    WaitTime = device.WaitTime * 1000                    
                };
                lock (media.Synchronous)
                {
                    //Init IEC connection. This must done first with serial connections.
                    string data = "/?!\r\n";
                    if (device.HDLCAddressing == HDLCAddressType.SerialNumber)
                    {
                        data = "/?" + device.SerialNumber + "!\r\n";
                    }
                    media.Send(data, null);
                    if (!media.Receive(p))
                    {
                        //Try to move away from mode E.
                        //TODO: this.ReadDLMSPacket(this.DisconnectRequest());
                        data = "Failed to receive reply from the device in given time.";
                        GXLogWriter.WriteLog(data);
                        throw new Exception(data);
                    }
                    //If echo is used.
                    if (p.Reply == data)
                    {
                        p.Reply = null;
                        if (!media.Receive(p))
                        {
                            //Try to move away from mode E.
                            //TODO: this.ReadDLMSPacket(this.DisconnectRequest());
                            data = "Failed to receive reply from the device in given time.";
                            GXLogWriter.WriteLog(data);
                            throw new Exception(data);
                        }
                    }
                }    
                string manufactureID = p.Reply.Substring(1, 3);
                char baudrate = p.Reply[4];
                int bitrate = 0;
                switch (baudrate)
                {
                    case '0':
                        bitrate = 300;
                        break;
                    case '1':
                        bitrate = 600;
                        break;
                    case '2':
                        bitrate = 1200;
                        break;
                    case '3':
                        bitrate = 2400;
                        break;
                    case '4':
                        bitrate = 4800;
                        break;
                    case '5':
                        bitrate = 9600;
                        break;
                    case '6':
                        bitrate = 19200;
                        break;
                    default:
                        throw new Exception("Unknown baud rate.");
                }                
                //Send ACK
                //Send Protocol control character
                byte controlCharacter = (byte)'2';// "2" HDLC protocol procedure (Mode E)
                //Send Baudrate character
                //Mode control character 
                byte ModeControlCharacter = (byte)'2';//"2" //(HDLC protocol procedure) (Binary mode)
                //We are not receive anything.
                lock (media.Synchronous)
                {
                    media.Send(new byte[] { 0x06, controlCharacter, (byte)baudrate, ModeControlCharacter, 0x8D, 0x0A }, null);
                    System.Threading.Thread.Sleep(1000);
                    serial.BaudRate = bitrate;
                    ReceiveParameters<byte[]> args = new ReceiveParameters<byte[]>();
                    args.Eop = (byte)0x0A;
                    args.WaitTime = 500;
                    media.Receive(args);
                }
                serial.DataBits = 8;
                serial.Parity = System.IO.Ports.Parity.None;
                serial.StopBits = System.IO.Ports.StopBits.One;
                serial.ResetSynchronousBuffer();
                serial.Eop = (byte)0x7E;
            }            
        }