Example #1
0
        public static void main()
        {
            //Haussteuerung
            {
                Channel ch  = new Ethernet("192.168.47.12", 20257, EthProtocol.TCP);
                PLC     plc = PLCFactory.GetPLC(ch, 0);
                Console.WriteLine(plc.PlcName);
                plc.SetExecuter(OperandsExecuterType.ExecuterPartialBinaryMix);

                Console.WriteLine(plc.Version + "\t");
                Console.WriteLine(plc.RTC + "\tPLC Timer \n" + System.DateTime.Now + "\tUTC Time");

                plc_Kompressor     plcKompressor = new plc_Kompressor(ch);
                ReadWriteRequest[] rw            = plcKompressor.init();


                int j = 0;
                while (j < 30)
                {
                    plcKompressor.readData();
                    Thread.Sleep(1000);
                    j++;
                }
            }
        }
        internal void Listen()
        {
            if (Status == ConnectionStatus.Disconnected)
            {
                m_ConnectionFlag = ConnectionFlag.None;

                try
                {
                    socket.Listen(m_LocalPort);
                    Status = ConnectionStatus.Listening;
                    string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);
                    try
                    {
                        ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                           Unitronics.ComDriver.ConnectionStatus.Listening.ToString());
                    }
                    catch
                    {
                    }
                }
                catch
                {
                    throw new ComDriveExceptions(
                              "Failed binding local port " + m_LocalPort.ToString() +
                              ". Please check that the port is not in use", ComDriveExceptions.ComDriveException.PortInUse);
                }
            }
            else
            {
                //GetPLC was called. Therefore since we already listening then there are 2 options:
                //1. Socket is not connected and it is listening. When connection will arive then the PLC will be returned
                //2. Socket is connected. Therefore On Connect event will not happen and we need to return the PLC right now.
                Socket worker = socket.GetWorker();
                if (Status == ConnectionStatus.Connected)
                {
                    try
                    {
                        PLC plc = PLCFactory.GetPLC(this, 0);
                        if (OnListenerConnectionAccepted != null)
                        {
                            OnListenerConnectionAccepted(plc);
                        }
                    }
                    catch
                    {
                        try
                        {
                            worker.Close();
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    System.Diagnostics.Debug.Print("Listen request denied... Connection Status: " + Status.ToString());
                }
            }
        }
        private void onConnect(IAsyncResult ar)
        {
            try
            {
                socket.BeginAccept(onConnect, null);
            }
            catch
            {
                Status = EthernetListener.ConnectionStatus.Disconnected;
            }

            Socket worker = null;

            try
            {
                worker = socket.EndAccept(ar);
                ListenerClient client = new ListenerClient(worker, LocalPort, base.Retry, base.TimeOut);

                System.Threading.Thread.Sleep(1000);
                PLC plc = PLCFactory.GetPLC(client, 0);
                if (OnConnectionAccepted != null)
                {
                    OnConnectionAccepted(plc);
                }
            }
            catch
            {
                if (worker != null)
                {
                    worker.Close();
                }
            }
        }
Example #4
0
 public plc_Kompressor(Channel ch)
 {
     init();
     this.ch = ch;
     plc     = PLCFactory.GetPLC(ch, 0);
     plc.SetExecuter(OperandsExecuterType.ExecuterPartialBinaryMix);
     rw = init();
 }
        void socket_OnConnect(object sender, EventArgs e)
        {
            Status = ConnectionStatus.Connected;
            Socket worker     = socket.GetWorker();
            string channelLog = Utils.HelperComDriverLogger.GetLoggerChannel(this);

            try
            {
                ComDriverLogger.LogConnectionState(DateTime.Now, channelLog,
                                                   Unitronics.ComDriver.ConnectionStatus.ConnectionOpened.ToString());
            }
            catch (Exception ex)
            {
                // The PLC did not reply... Close the connection and return to Listen mode.
                {
                    worker.Close();
                    Console.Write(ex.Message);
                }
            }

            try
            {
                System.Threading.Thread.Sleep(1000);
                PLC plc = PLCFactory.GetPLC(this, 0);
                if (OnListenerConnectionAccepted != null)
                {
                    OnListenerConnectionAccepted(plc);
                }
            }
            catch (Exception ex)
            {
                // The PLC did not reply... Close the connection and return to Listen mode.
                {
                    worker.Close();
                    Console.Write(ex.Message);
                }
            }
        }
Example #6
0
        public static void main()
        {
            //Haussteuerung
            {
                Channel ch  = new Ethernet("192.168.47.6", 20257, EthProtocol.TCP);
                PLC     plc = PLCFactory.GetPLC(ch, 0);
                Console.WriteLine(plc.PlcName);
                plc.SetExecuter(OperandsExecuterType.ExecuterPartialBinaryMix);

                Console.WriteLine(plc.Version + "\t");
                Console.WriteLine(plc.RTC + "\tPLC Timer \n" + System.DateTime.Now + "\tUTC Time");
//Until here
                plc_haussterung    plcHaussterung = new plc_haussterung(ch);
                ReadWriteRequest[] rw             = plcHaussterung.init();

                while (true)
                {
                    plcHaussterung.readData();
                    Thread.Sleep(60000);
                }
            }


            //Kompressor

            /*  {
             *    Channel ch = new Ethernet("192.168.47.12", 20256, EthProtocol.TCP);
             *    PLC plc = PLCFactory.GetPLC(ch, 0);
             *    Console.WriteLine(plc.PlcName);
             *    plc.SetExecuter(OperandsExecuterType.ExecuterPartialBinaryMix);
             *
             *    Console.WriteLine(plc.Version);
             *    Console.WriteLine(plc.PLCChannel);
             *    Console.WriteLine(plc.RTC + " \t" + System.DateTime.Now);
             *
             *    object[] values = new object[2048];
             *    for (int i = 0; i < values.Length; i++)
             *    {
             *        values[i] = (object) i;
             *    }
             *
             *    ReadWriteRequest[] rw = new ReadWriteRequest[1];
             *
             *    ReadWriteRequest getCurr = new ReadOperands
             *    {
             *        NumberOfOperands = 1,
             *        OperandType = OperandTypes.TimerPreset,
             *        StartAddress = 0,
             *        TimerValueFormat = TimerValueFormat.TimeFormat,
             *    };
             *    rw[0] = getCurr;
             *    try
             *    {
             *        plc.ReadWrite(ref rw);
             *        Console.WriteLine(rw[0].ResponseValues);
             *    }
             *    catch (Exception e)
             *    {
             *        Console.WriteLine(e.Message + ":" + e.StackTrace);
             *        throw;
             *    }
             *
             * }*/
        }