예제 #1
0
        /// <summary>
        /// MelsecNet Driver Open Method
        /// </summary>
        public int MelsecNetDriverOpen(CConnectionString cs)
        {
            //_portCompolet = new CIPPortCompolet();

            //_class1 = new VariableCompolet();
            //_class1.Active = true;
            //_class1.PlcEncoding = Encoding.UTF8;

            //_class3 = new CommonCompolet();
            //_class3.PeerAddress = "192.168.123.8";
            //_class3.Active = false;
            //_class3.ConnectionType = ConnectionType.Class3;
            //_class2.DeviceType = 0;
            //_class3.HeartBeatTimer = 1000;
            //_class2.IsConnected = false;
            //_class3.LocalPort = 2;
            //_class2.MajorRevision = 0;
            //_class2.MinorRevision = 0;
            //_class3.OnHeartBeatTimer += new EventHandler(_class2_OnHeartBeatTimer);
            //_class3.PlcEncoding = Encoding.UTF8;
            //_class2.ProductCode = 0;
            //_class3.ReceiveTimeLimit = 5000;
            //_class3.RoutePath = @"2%192.168.123.8\1%0";
            //_class2.SerialNumber = 0;
            //_class2.TypeName = "";
            //_class3.UseRoutePath = false;
            //_class2.VariableNames = new string[5];
            //_class2.VendorID = 0;

            int            portID           = cs.GetValue <int>("PORTID", 2);
            string         plcEncoding      = cs.GetValue <string>("PLC_ENCODING", "UTF8");
            string         peerAddress      = cs.GetValue <string>("PEER_ADDRESS", "192.168.123.42");
            ConnectionType connectionType   = cs.GetValue <ConnectionType>("CONNECTION_TYPE", ConnectionType.Class3);
            int            heartbeatTimer   = cs.GetValue <int>("HEARTBEAT_TIMER", 1000);
            int            receiveTimeLimit = cs.GetValue <int>("RECEIVE_TIME_LIMIT", 1000);
            bool           useRoutePath     = cs.GetValue <bool>("USE_ROUTE_PATH", false);
            string         routePath        = cs.GetValue <string>("ROUTE_PATH", @"2%192.168.123.42\1%0");

            int iRet = 0;

            PortInformation portInfo = _portCompolet.GetPortInformation(portID);

            if (portInfo == null)
            {
                iRet = -99;
            }
            else
            {
                if (!portInfo.StartupOpen && !_portCompolet.IsOpened(portID))
                {
                    _portCompolet.Open(portID);
                }
                else
                {
                    //already open
                }
            }

            try
            {
                _class1.Active = true;
            }
            catch (Exception ex)
            {
                CLogManager.Instance.LogError(string.Format("Class1 active error"), ex);
            }

            try
            {
                _class3.PeerAddress    = peerAddress;
                _class3.ConnectionType = ConnectionType.Class3;
                _class3.HeartBeatTimer = heartbeatTimer;
                _class3.LocalPort      = portID;

                Encoding encoding = Encoding.UTF8;
                try
                {
                    encoding = Encoding.GetEncoding(plcEncoding);
                }
                catch (Exception ex)
                {
                    CLogManager.Instance.LogError(string.Format("Class3 {0} name error (default = UTF8)", plcEncoding), ex);
                }

                _class3.PlcEncoding      = encoding;
                _class3.ReceiveTimeLimit = receiveTimeLimit;
                _class3.RoutePath        = string.Format(@"2%{0}\1%0", peerAddress);
                _class3.UseRoutePath     = false;
                _class3.Active           = true;
            }
            catch (Exception ex)
            {
                CLogManager.Instance.LogError(string.Format("Class3 active error"), ex);
            }

            if (_portCompolet.IsOpened(portID) && _class1.Active && _class3.IsConnected)
            {
                iRet = 0;
            }
            else
            {
                iRet = -99;
            }

            if (iRet == 0)
            {
                _core.Log((new CMelsecLogFormat(Catagory.Debug, "SYSMAC GATEWAY CONNECT", "MelsecNetDriverOpen()", true)));
                _melsecConnect = enumMelsecConnect.CONNECT;

                if (IOManagerOpen != null)
                {
                    IOManagerOpen(this, true);
                }
            }
            else
            {
                _core.Log((new CMelsecLogFormat(Catagory.Debug, "SYSMAC GATEWAY DISCONNECT RETRUN CODE=" + iRet.ToString(), "MelsecNetDriverOpen()", true)));
                _melsecConnect = enumMelsecConnect.DISCONNECT;

                if (IOManagerOpen != null)
                {
                    IOManagerOpen(this, false);
                }
            }

            return(iRet);
        }
예제 #2
0
 public int MelsecNetDriverOpen(string connectionString)
 {
     return(this.MelsecNetDriverOpen(CConnectionString.Parser(connectionString)));
 }