Beispiel #1
0
        /// <summary>
        /// Change frequency to accomplish desired flow rate
        /// </summary>
        /// <param name="desiredFlowRate">Desired flow rate</param>
        public static void freqConversion(object desiredFlowRateObj)
        {
            //convert obj into float
            float desiredFlowRate = (float)desiredFlowRateObj;

            //point variables
            PointVal        currentFlowValue;
            PointVal        currentFreqValue;
            List <PointVal> freqList;
            List <PointVal> flowList;

            //Set up OSI PI
            OSIPI myPI = new OSIPI();

            myPI.connect_Server("ESMARTSERVER-PC");

            //put current flow rate in a saved PI point
            flowList         = myPI.searchPiPoints("SP14VICE_Flow", DateTime.Now.ToString(), DateTime.Now.ToString());
            currentFlowValue = flowList.ElementAt(0);

            //Check if flow varies by +/- 1 GPM
            while (float.Parse(currentFlowValue.value) > (desiredFlowRate + 1) || float.Parse(currentFlowValue.value) > (desiredFlowRate - 1))
            {
                //put current frequency in a saved PI point
                freqList         = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                currentFreqValue = freqList.ElementAt(0);

                //set new frequency
                if (float.Parse(currentFlowValue.value) > (desiredFlowRate + 5))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) + 10, 6));
                }
                else if (float.Parse(currentFlowValue.value) < (desiredFlowRate - 5))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) - 10, 6));
                }
                else if (float.Parse(currentFlowValue.value) > (desiredFlowRate + 2))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) + 5, 6));
                }
                else if (float.Parse(currentFlowValue.value) < (desiredFlowRate - 2))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) - 5, 6));
                }
                else if (float.Parse(currentFlowValue.value) > (desiredFlowRate + 1))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) + 2, 6));
                }
                else if (float.Parse(currentFlowValue.value) < (desiredFlowRate - 1))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) - 2, 6));
                }

                Thread.Sleep(6000); // sleep 6 sec for flow rate to change

                //put current flow rate in a saved PI point -- hopefully it changes in 8 sec
                flowList         = myPI.searchPiPoints("SP14VICE_Flow", DateTime.Now.ToString(), DateTime.Now.ToString());
                currentFlowValue = flowList.ElementAt(0);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Change frequency to accomplish desired flow rate
        /// </summary>
        /// <param name="desiredFlowRate">Desired flow rate</param>
        public static void freqConversion(object desiredFlowRateObj)
        {
            //convert obj into float
            float desiredFlowRate = (float)desiredFlowRateObj;

            //point variables
            PointVal currentFlowValue;
            PointVal currentFreqValue;
            List<PointVal> freqList;
            List<PointVal> flowList;

            //Set up OSI PI
            OSIPI myPI = new OSIPI();
            myPI.connect_Server("ESMARTSERVER-PC");

            //put current flow rate in a saved PI point
            flowList = myPI.searchPiPoints("SP14VICE_Flow", DateTime.Now.ToString(), DateTime.Now.ToString());
            currentFlowValue = flowList.ElementAt(0);

            //Check if flow varies by +/- 1 GPM
            while (float.Parse(currentFlowValue.value) > (desiredFlowRate + 1) || float.Parse(currentFlowValue.value) > (desiredFlowRate - 1))
            {
                //put current frequency in a saved PI point
                freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                currentFreqValue = freqList.ElementAt(0);

                //set new frequency
                if (float.Parse(currentFlowValue.value) > (desiredFlowRate + 5))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) + 10, 6));
                }
                else if (float.Parse(currentFlowValue.value) < (desiredFlowRate - 5))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) - 10, 6));
                }
                else if (float.Parse(currentFlowValue.value) > (desiredFlowRate + 2))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) + 5, 6));
                }
                else if (float.Parse(currentFlowValue.value) < (desiredFlowRate - 2))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) - 5, 6));
                }
                else if (float.Parse(currentFlowValue.value) > (desiredFlowRate + 1))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) + 2, 6));
                }
                else if (float.Parse(currentFlowValue.value) < (desiredFlowRate - 1))
                {
                    myPI.setPiPointValue("SP14VICE_Freq", myTruncate(float.Parse(currentFreqValue.value) - 2, 6));
                }

                Thread.Sleep(6000); // sleep 6 sec for flow rate to change

                //put current flow rate in a saved PI point -- hopefully it changes in 8 sec
                flowList = myPI.searchPiPoints("SP14VICE_Flow", DateTime.Now.ToString(), DateTime.Now.ToString());
                currentFlowValue = flowList.ElementAt(0);

            }
        }
Beispiel #3
0
        /// <summary>
        /// Read incoming data and send ACK
        /// </summary>
        /// <param name="ar"></param>
        public static void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;

            _IpTest = new DataMGT();
            _myPI   = new OSIPI();

            Console.WriteLine("In ReadCallBack...");

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                Console.WriteLine("Bytes>0...");
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                content = state.sb.ToString();
                string msg1 = "<ACK>";
                string msg2 = "<,RT=0,RB=0,TM=1,>";
                if (content.IndexOf("<EOF>") > -1)
                {
                    // All the data has been read from the

                    DateTime time   = DateTime.Now;            // Use current time
                    string   format = "MMM ddd d HH:mm yyyy";  // Use this format
                    Console.WriteLine(time.ToString(format));
                    Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
                    // Echo the data back to the client
                    try
                    {
                        _IpTest.gotString(content);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("IPTest Error: {0} \n", ex.Message);
                    }


                    Send(handler, msg1);
                    //Send(handler, content);
                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                }
            }
        }
Beispiel #4
0
        public static void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;
            _IpTest = new DataMGT();
            _myPI = new OSIPI();

            Console.WriteLine("In ReadCallBack...");

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                Console.WriteLine("Bytes>0...");
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                content = state.sb.ToString();
                string msg1 = "<ACK>";
                string msg2 = "<,RT=0,RB=0,TM=1,>";
                if (content.IndexOf("<EOF>") > -1)
                {
                    // All the data has been read from the

                    DateTime time = DateTime.Now;              // Use current time
                    string format = "MMM ddd d HH:mm yyyy";    // Use this format
                    Console.WriteLine(time.ToString(format));
                    Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
                    // Echo the data back to the client
                    try
                    {
                        _IpTest.gotString(content);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("IPTest Error: {0} \n",ex.Message);
                    }

                    Send(handler, msg1);
                    //Send(handler, content);

                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Monitor PI points for changes
        /// </summary>
        /// <param name="ar"></param>
        public static void osiReadCallback(IAsyncResult ar)
        {
            Console.WriteLine("In osiReadCallBack...");

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;
            /****Search current VFD State and Frequency PI values*****/

            //initailize OSI PI and server
            OSIPI myPI = new OSIPI();

            myPI.connect_Server("ESMARTSERVER-PC");

            //Create PI values and Lists for each
            PointVal        savedVfdState      = null;
            PointVal        currentVfdState    = null;
            PointVal        savedDesiredFlow   = null;
            PointVal        currentDesiredFlow = null;
            List <PointVal> vfdStateList;
            List <PointVal> desiredFlowList;
            bool            firstRun = false;

            if (myPI.check_connection())
            {
                //put VFD state, frequency, lock, and desired floe rate in a saved PI points
                vfdStateList  = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedVfdState = currentVfdState = vfdStateList.ElementAt(0);

                desiredFlowList  = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedDesiredFlow = currentDesiredFlow = desiredFlowList.ElementAt(0);
            }


            while (true)
            {
                //get current OSI PI value for VFD, frequency, lock, and desire flow rate
                if (myPI.check_connection())
                {
                    vfdStateList    = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentVfdState = vfdStateList.ElementAt(0);

                    desiredFlowList    = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentDesiredFlow = desiredFlowList.ElementAt(0);
                }

                //check for first run to send initail DesiredFlow value
                if (firstRun == false)
                {
                    //Send to Rasp PI to initiate function to change flow via frequency
                    savedDesiredFlow = currentDesiredFlow;
                    osiSend(handler, "<,DF=" + savedDesiredFlow.value.ToString() + ",EOF,>");
                    firstRun = true;
                }

                //check if the OSI server value has changed for VfdState
                if (currentVfdState.value != savedVfdState.value)
                {
                    //send current value to Rasp PI
                    savedVfdState = currentVfdState;
                    osiSend(handler, "<,S=" + savedVfdState.value.ToString() + ",EOF,>");
                }


                //check if the OSI server value has changed for desired flow rate
                if (currentDesiredFlow.value != savedDesiredFlow.value)
                {
                    //send current value to Rasp PI
                    savedDesiredFlow = currentDesiredFlow;
                    osiSend(handler, "<,DF=" + savedDesiredFlow.value.ToString() + ",EOF,>");
                }


                Thread.Sleep(5000); //sleep 5 sec then update
            }
        }
Beispiel #6
0
        /// <summary>
        /// Monitor PI points for changes
        /// </summary>
        /// <param name="ar"></param>
        public static void osiReadCallback(IAsyncResult ar)
        {
            Console.WriteLine("In osiReadCallBack...");

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;
            /****Search current VFD State and Frequency PI values*****/

            //initailize OSI PI and server
            OSIPI myPI = new OSIPI();
            myPI.connect_Server("ESMARTSERVER-PC");

            //Create PI values and Lists for each
            PointVal savedVfdState = null;
            PointVal currentVfdState = null;
            PointVal savedFreq = null;
            PointVal currentFreq = null;
            PointVal savedLockState = null;
            PointVal currentLockState = null;
            PointVal savedDesiredFlow = null;
            PointVal currentDesiredFlow = null;
            List<PointVal> vfdStateList;
            List<PointVal> freqList;
            List<PointVal> lockStateList;
            List<PointVal> desiredFlowList;
            bool firstRun = false;

            if (myPI.check_connection())
            {

                //put VFD state, frequency, lock, and desired floe rate in a saved PI points
                vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedVfdState = currentVfdState = vfdStateList.ElementAt(0);

                freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedFreq = currentFreq = freqList.ElementAt(0);

                lockStateList = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedLockState = currentLockState = lockStateList.ElementAt(0);

                desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedDesiredFlow = currentDesiredFlow = desiredFlowList.ElementAt(0);

            }

            while (true)
            {

                //get current OSI PI value for VFD, frequency, lock, and desire flow rate
                if (myPI.check_connection())
                {
                    vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentVfdState = vfdStateList.ElementAt(0);

                    freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentFreq = freqList.ElementAt(0);

                    lockStateList = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentLockState = lockStateList.ElementAt(0);

                    desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentDesiredFlow = desiredFlowList.ElementAt(0);
                }

                //check for first run to send initail DesiredFlow value
                if (firstRun == false)
                {
                    //Send to Rasp PI to initiate function to change flow via frequency
                    savedDesiredFlow = currentDesiredFlow;
                    osiSend(handler, "<,DF=" + savedDesiredFlow.value.ToString() + ",EOF,>");
                    firstRun = true;
                }

                //check if the OSI server value has changed for VfdState
                if (currentVfdState.value != savedVfdState.value)
                {

                    //send current value to Rasp PI
                    savedVfdState = currentVfdState;
                    osiSend(handler, "<,S=" + savedVfdState.value.ToString() + ",EOF,>");
                }

                //check if the OSI server value has changed for frequency
                if (currentFreq.value != savedFreq.value)
                {
                    //send current value to Rasp PI
                    savedFreq = currentFreq;
                    osiSend(handler, "<,F=" + savedFreq.value.ToString() + ",EOF,>");

                }

                //check if the OSI server value has changed for Lock
                if (currentLockState.value != savedLockState.value)
                {

                    //send current value to Rasp PI
                    savedLockState = currentLockState;
                    osiSend(handler, "<,S=" + savedLockState.value.ToString() + ",EOF,>");

                }

                //check if the OSI server value has changed for desired flow rate
                if (currentDesiredFlow.value != savedDesiredFlow.value)
                {

                    //send current value to Rasp PI
                    savedDesiredFlow = currentDesiredFlow;
                    osiSend(handler, "<,DF=" + savedDesiredFlow.value.ToString() + ",EOF,>");

                }

                Thread.Sleep(5000); //sleep 5 sec then update

            }
        }
Beispiel #7
0
        /// <summary>
        /// Listen for changes in the OSI points and send changes to Rasp PI (VFD State, Flow, Lock, and Desired Flow Rate)
        /// </summary>
        public static void osiListen()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            IPAddress  ipAddress     = IPAddress.Parse("192.168.144.144");
            int        port          = 9700;
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                //listener.Connect(localEndPoint);
                listener.Bind(localEndPoint);
                listener.Listen(100);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();
                    if (!listener.Poll(300, SelectMode.SelectRead))
                    {
                        Console.WriteLine("Dong Dong Dong");
                    }


                    // Start an asynchronous socket to listen for connections.
                    Console.WriteLine("Waiting for Rasp PI connection...");
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);


                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                    Console.WriteLine("Rasp PI connected!!!!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }



            /****Search current VFD State and Frequency PI values*****/

            //initailize OSI PI and server
            OSIPI myPI = new OSIPI();

            myPI.connect_Server("ESMARTSERVER-PC");

            //Create PI values and Lists for each
            PointVal        savedVfdState      = null;
            PointVal        currentVfdState    = null;
            PointVal        savedFreq          = null;
            PointVal        currentFreq        = null;
            PointVal        savedLockState     = null;
            PointVal        currentLockState   = null;
            PointVal        savedDesiredFlow   = null;
            PointVal        currentDesiredFlow = null;
            List <PointVal> vfdStateList;
            List <PointVal> freqList;
            List <PointVal> lockStateList;
            List <PointVal> desiredFlowList;

            if (myPI.check_connection())
            {
                //put VFD state, frequency, lock, and desired floe rate in a saved PI points
                vfdStateList  = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedVfdState = currentVfdState = vfdStateList.ElementAt(0);

                freqList  = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedFreq = currentFreq = freqList.ElementAt(0);

                lockStateList  = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedLockState = currentLockState = lockStateList.ElementAt(0);

                desiredFlowList  = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedDesiredFlow = currentDesiredFlow = desiredFlowList.ElementAt(0);
            }


            while (true)
            {
                //get current OSI PI value for VFD, frequency, lock, and desire flow rate
                if (myPI.check_connection())
                {
                    vfdStateList    = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentVfdState = vfdStateList.ElementAt(0);

                    freqList    = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentFreq = freqList.ElementAt(0);

                    lockStateList    = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentLockState = lockStateList.ElementAt(0);

                    desiredFlowList    = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentDesiredFlow = desiredFlowList.ElementAt(0);
                }


                //check if the OSI server value has changed for VfdState
                if (currentVfdState.value != savedVfdState.value)
                {
                    //send current value to Rasp PI
                    savedVfdState = currentVfdState;
                    Send(listener, "<,S=" + savedVfdState.ToString() + ",EOF,>");
                    // PythonComm.sendTCP("192.168.144.126", 9750, "<,S=" + savedVfdState.ToString() + ",EOF,>");
                }

                //check if the OSI server value has changed for frequency
                if (currentFreq.value != savedFreq.value)
                {
                    //send current value to Rasp PI
                    savedFreq = currentFreq;
                    Send(listener, "<,F=" + savedFreq.ToString() + ",EOF,>");
                    // PythonComm.sendTCP("192.168.144.126", 9750, "<,F=" + savedFreq.ToString() + ",EOF,>");
                }

                //check if the OSI server value has changed for Lock
                if (currentLockState.value != savedLockState.value)
                {
                    //send current value to Rasp PI
                    savedLockState = currentLockState;
                    Send(listener, "<,S=" + savedLockState.ToString() + ",EOF,>");
                    // PythonComm.sendTCP("192.168.144.126", 9750, "<,S=" + savedLockState.ToString() + ",EOF,>");
                }

                //check if the OSI server value has changed for desired flow rate
                if (currentLockState.value != savedLockState.value)
                {
                    //Initiate function to change flow via frequency
                    savedDesiredFlow = currentDesiredFlow;
                    Thread changeFreq = new Thread(new ParameterizedThreadStart(freqConversion)); //thread to change frequency
                    changeFreq.Start(savedDesiredFlow.value);                                     //start thread
                }

                Thread.Sleep(5000); //sleep 5 sec then update
            }
        }
Beispiel #8
0
        /// <summary>
        /// Listen for changes in the OSI points and send changes to Rasp PI (VFD State, Flow, Lock, and Desired Flow Rate)
        /// </summary>
        public static void osiListen()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            IPAddress ipAddress = IPAddress.Parse("192.168.144.144");
            int port = 9700;
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                //listener.Connect(localEndPoint);
                listener.Bind(localEndPoint);
                listener.Listen(100);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();
                    if (!listener.Poll(300, SelectMode.SelectRead))
                    {
                        Console.WriteLine("Dong Dong Dong");
                    }

                    // Start an asynchronous socket to listen for connections.
                    Console.WriteLine("Waiting for Rasp PI connection...");
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                    Console.WriteLine("Rasp PI connected!!!!");
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            /****Search current VFD State and Frequency PI values*****/

            //initailize OSI PI and server
            OSIPI myPI = new OSIPI();
            myPI.connect_Server("ESMARTSERVER-PC");

            //Create PI values and Lists for each
            PointVal savedVfdState = null;
            PointVal currentVfdState = null;
            PointVal savedFreq = null;
            PointVal currentFreq = null;
            PointVal savedLockState = null;
            PointVal currentLockState = null;
            PointVal savedDesiredFlow = null;
            PointVal currentDesiredFlow = null;
            List<PointVal> vfdStateList;
            List<PointVal> freqList;
            List<PointVal> lockStateList;
            List<PointVal> desiredFlowList;

            if (myPI.check_connection())
            {

                //put VFD state, frequency, lock, and desired floe rate in a saved PI points
                vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedVfdState = currentVfdState = vfdStateList.ElementAt(0);

                freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedFreq = currentFreq = freqList.ElementAt(0);

                lockStateList = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedLockState = currentLockState = lockStateList.ElementAt(0);

                desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                savedDesiredFlow = currentDesiredFlow = desiredFlowList.ElementAt(0);
            }

            while(true)
            {

                //get current OSI PI value for VFD, frequency, lock, and desire flow rate
                if (myPI.check_connection())
                {
                    vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentVfdState = vfdStateList.ElementAt(0);

                    freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentFreq = freqList.ElementAt(0);

                    lockStateList = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentLockState = lockStateList.ElementAt(0);

                    desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentDesiredFlow = desiredFlowList.ElementAt(0);
                }

                //check if the OSI server value has changed for VfdState
                if(currentVfdState.value != savedVfdState.value)
                {

                    //send current value to Rasp PI
                    savedVfdState = currentVfdState;
                    Send(listener, "<,S=" + savedVfdState.ToString() + ",EOF,>");
                   // PythonComm.sendTCP("192.168.144.126", 9750, "<,S=" + savedVfdState.ToString() + ",EOF,>");

                }

                //check if the OSI server value has changed for frequency
                if (currentFreq.value != savedFreq.value)
                {
                    //send current value to Rasp PI
                    savedFreq = currentFreq;
                    Send(listener, "<,F=" + savedFreq.ToString() + ",EOF,>");
                   // PythonComm.sendTCP("192.168.144.126", 9750, "<,F=" + savedFreq.ToString() + ",EOF,>");

                }

                //check if the OSI server value has changed for Lock
                if (currentLockState.value != savedLockState.value)
                {

                    //send current value to Rasp PI
                    savedLockState = currentLockState;
                    Send(listener, "<,S=" + savedLockState.ToString() + ",EOF,>");
                    // PythonComm.sendTCP("192.168.144.126", 9750, "<,S=" + savedLockState.ToString() + ",EOF,>");

                }

                //check if the OSI server value has changed for desired flow rate
                if (currentLockState.value != savedLockState.value)
                {

                    //Initiate function to change flow via frequency
                    savedDesiredFlow = currentDesiredFlow;
                    Thread changeFreq = new Thread(new ParameterizedThreadStart(freqConversion)); //thread to change frequency
                    changeFreq.Start(savedDesiredFlow.value); //start thread

                }

                Thread.Sleep(5000); //sleep 5 sec then update

            }
        }
Beispiel #9
0
        public static void osiListen()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            IPAddress  ipAddress     = IPAddress.Parse("192.168.144.144");
            int        port          = 9700;
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                //listener.Connect(localEndPoint);
                listener.Bind(localEndPoint);
                listener.Listen(100);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();
                    if (!listener.Poll(300, SelectMode.SelectRead))
                    {
                        Console.WriteLine("Dong Dong Dong");
                    }


                    // Start an asynchronous socket to listen for connections.
                    Console.WriteLine("Waiting for Rasp PI connection...");
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);


                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            /****Search current VFD State PI value*****/
            OSIPI myPI = new OSIPI();

            myPI.connect_Server("ESMARTSERVER-PC");
            PointVal stateVal   = null;
            PointVal currentVal = null;

            if (myPI.check_connection())
            {
                //put VFD state in a saved PI point
                List <PointVal> vfdState = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                stateVal = vfdState.ElementAt(0);
            }


            while (true)
            {
                //get current OSI PI value for VFD
                if (myPI.check_connection())
                {
                    List <PointVal> vfdCurrentState = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentVal = vfdCurrentState.ElementAt(0);
                }

                //check if the OSI server value has changed
                if (currentVal != stateVal)
                {
                    //send current value to Rasp PI
                    stateVal = currentVal;
                    Send(listener, "<,S=" + stateVal.ToString() + ",EOF,>");
                }

                Thread.Sleep(5000);
            }
        }
Beispiel #10
0
        public static void osiListen()
        {
            // Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            IPAddress ipAddress = IPAddress.Parse("192.168.144.144");
            int port = 9700;
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                //listener.Connect(localEndPoint);
                listener.Bind(localEndPoint);
                listener.Listen(100);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();
                    if (!listener.Poll(300, SelectMode.SelectRead))
                    {
                        Console.WriteLine("Dong Dong Dong");
                    }

                    // Start an asynchronous socket to listen for connections.
                    Console.WriteLine("Waiting for Rasp PI connection...");
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            /****Search current VFD State PI value*****/
            OSIPI myPI = new OSIPI();
            myPI.connect_Server("ESMARTSERVER-PC");
            PointVal stateVal = null;
            PointVal currentVal = null;

            if (myPI.check_connection())
            {

                //put VFD state in a saved PI point
                List<PointVal> vfdState = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                stateVal = vfdState.ElementAt(0);

            }

            while(true)
            {

                //get current OSI PI value for VFD
                if (myPI.check_connection())
                {
                    List<PointVal> vfdCurrentState = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString());
                    currentVal = vfdCurrentState.ElementAt(0);
                }

                //check if the OSI server value has changed
                if(currentVal != stateVal)
                {
                    //send current value to Rasp PI
                    stateVal = currentVal;
                    Send(listener, "<,S=" + stateVal.ToString() + ",EOF,>");

                }

                Thread.Sleep(5000);

            }
        }