/// <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 } }
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); } }