/// <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); } }
public void formPointUpdate() { while (true) { label4.Text = myPI.searchPiPoints("SP14VICE_Flow", DateTime.Now.ToString(), DateTime.Now.ToString()).ToString(); label5.Text = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString()).ToString(); label6.Text = myPI.searchPiPoints("SP14VICE_RPM", DateTime.Now.ToString(), DateTime.Now.ToString()).ToString(); } }
private void buttonSearch_Click(object sender, EventArgs e) { //Check for empty tag if (textBoxDisplayTag.Text.Count() == 0) // if nothing is in textbox { MessageBox.Show("Invalid tag"); // show error messege return; } if (dateTimePickerStart.Value > DateTime.Now) { MessageBox.Show("Invalid start time"); return; } if (dateTimePickerEnd.Value > DateTime.Now) { MessageBox.Show("Invalid end time"); return; } listView1.Items.Clear();// clear the list view box after new search listView1.Visible = true; myPI.connect_Server("ESMARTSERVER-PC"); if (myPI.check_connection()) { // create list with the points and the start/end times List <PointVal> list = myPI.searchPiPoints(textBoxDisplayTag.Text, dateTimePickerStart.Value.ToString(), dateTimePickerEnd.Value.ToString()); foreach (PointVal item in list) { //adds date and value in list view ListViewItem listItem = listView1.Items.Add(item.date); listItem.SubItems.Add(item.value); } } }
/// <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 } }
/// <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); } }