/// <summary> /// Creates a PI point on the PI server with a given value /// </summary> /// <param name="value"></param> private void createPIPoint(float value) { try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { string usertag = textBox_tag.Text;//set new value to a certain point //Creating a point myPI.createPiPoint(usertag, 2); // Setting the value of the point // Also have the power to only update the value on an existing // tag instead of creating a new tag. myPI.setPiPointValue(usertag, myTruncate(value, 6)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { } }
/// <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); } }
/// <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); } }
/// <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 } }
/// <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 } }
public void parseVal(string letter, string value) { string lockVal = "SP14VICE_Lock"; string connection = "SP14VICE_Connection"; string frequency = "SP14VICE_Freq"; string vfdState = "SP14VICE_VfdState"; string temp = "SP14VICE_Temp"; string flow = "SP14VICE_Flow"; string desiredFlow = "SP14VICE_DesiredFlow"; string pressure = "SP14VICE_Pressure"; string voltage = "F13APA_Voltage"; string currenttop = "F13APA_CurrentTOP"; string currentbot = "F13APA_CurrentBOTTOM"; string powertop = "F13APA_Power_Top"; string powerbot = "F13APA_Power_Bot"; switch (letter) //checks first value in string for command { case "C": //if connection try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(connection, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "L": //if Lock try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(lockVal, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "FR": //if frequency try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(frequency, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "VS": //if VFD State try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(vfdState, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "PR": //if Pressure try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(pressure, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "T": //if Temperature try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(temp, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "F": //if Flow try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(flow, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "DF": //if DesiredFlow try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(desiredFlow, (float.Parse(value))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "V": //if voltage string vtemp = voltage + outlet.ToString(); try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(vtemp, ((((float.Parse(value)) * 120) / Math.Pow(2, 23)) / .6)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "IT": //if current TOP outlet string currenttoptemp = currenttop + outlet.ToString(); try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(currenttoptemp, ((((float.Parse(value)) * 15) / (Math.Pow(2, 24) - 1)) / .6)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "IB": //if current BOT outlet string currentbottemp = currentbot + outlet.ToString(); try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(currentbottemp, ((((float.Parse(value)) * 15) / (Math.Pow(2, 24) - 1)) / .6)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "PT": //POWER TOP string powertoptemp = powertop + outlet.ToString(); try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(powertoptemp, 1 - ((float.Parse(value)) / (Math.Pow(2, 23) - 1))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "PB": //POWER BOT string powerbottemp = powerbot + outlet.ToString(); try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue(powerbottemp, 1 - ((float.Parse(value)) / (Math.Pow(2, 23) - 1))); } } catch (Exception ex) { Console.WriteLine(ex.Message); } break; case "S": //set outlet Status outlet = int.Parse(value); break; case "STM": //set outlet Status stream = int.Parse(value); break; case "OO": //command from Web Service try { myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { //Setting the value of the point myPI.setPiPointValue("F13APA_STATUS", int.Parse(value)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } switch (value) //case for outlet switching from web service { case "00": { if (outlet == 1) { PythonComm.sendTCP(EWO1, 9750, "<,RT=0,RB=0,TM=" + stream.ToString() + ",>"); } else if (outlet == 2) { PythonComm.sendTCP(EWO2, 9750, "<,RT=0,RB=0,TM=" + stream.ToString() + ",>"); } } break; case "11": { if (outlet == 1) { PythonComm.sendTCP(EWO1, 9750, "<,RT=1,RB=1,TM=" + stream.ToString() + ",>"); } else if (outlet == 2) { PythonComm.sendTCP(EWO2, 9750, "<,RT=1,RB=1,TM=" + stream.ToString() + ",>"); } } break; case "10": { if (outlet == 1) { PythonComm.sendTCP(EWO1, 9750, "<,RT=1,RB=0,TM=" + stream.ToString() + ",>"); } else if (outlet == 2) { PythonComm.sendTCP(EWO2, 9750, "<,RT=1,RB=0,TM=" + stream.ToString() + ",>"); } } break; case "01": { if (outlet == 1) { PythonComm.sendTCP(EWO1, 9750, "<,RT=0,RB=1,TM=" + stream.ToString() + ",>"); } else if (outlet == 2) { PythonComm.sendTCP(EWO2, 9750, "<,RT=0,RB=1,TM=" + stream.ToString() + ",>"); } } break; } break; default: break; } }
/// <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 } }
/// <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); } }
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); } }