public void ReadGS() { if (GSerror) { OnChangedState?.Invoke("GS", 0, "ПЛК не отвечает"); } GSerror = true; bufferInput = new byte[0]; try { inputGS.SerialPort.Write(read12, 0, read12.Length); } catch (Exception ex) { OnChangedState?.Invoke("GS", 0, ex.Message); } }
private void SendToServerStart(WorkInfo wi) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://pkrs.awardto.me/v1/jobs"); request.Method = "POST"; request.Headers.Add("Authorization", "Basic Y29udHJvbGxlcjphc2VsYXU2YWVsb294MFhhaDhFY2g3Y2FpczZhZXI="); request.ContentType = "application/json"; string data = "{" + "\"controllerId\": \"10524efa-debb-4509-9f42-be168a862e55\"," + "\"wellname\":\"" + wi.Well + "\"," + "\"fieldname\":\"" + wi.Field + "\"," + "\"contrCompany\": \"" + wi.Builder + "\"," + "\"responsible\": \"" + wi.FIO + "\"," + "\"process\": \"ОПЗ\"," + "\"serial\": \"" + wi.NumPKRS + "\"," + "\"configuration\": {" + "\"values\": [" + "{\"name\": \"P1\"}," + "{\"name\": \"P2\"}," + "{\"name\": \"P3\"}," + "{\"name\": \"T1\"}," + "{\"name\": \"R1\"}," + "{\"name\": \"Q1\"}," + "{\"name\": \"V1\"}," + "{\"name\": \"Q2\"}," + "{\"name\": \"V2\"}" + "]}}"; try { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(data); } WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); jobID = Convert.ToInt32(result.Split(':')[2].Split('}')[0]); } } response.Close(); OnChangedState?.Invoke("Web", 1, ""); } catch (Exception e) { //MessageBox.Show("Не удалось создать закачку в удаленном сервер\n" + e.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); OnChangedState?.Invoke("Web", 0, e.Message); } }
private void SendToServerData() { if (jobID < 0) { return; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://pkrs.awardto.me/v1/jobs/" + jobID.ToString()); request.ContentType = "application/json"; request.Method = "POST"; request.Headers["Authorization"] = "Basic Y29udHJvbGxlcjphc2VsYXU2YWVsb294MFhhaDhFY2g3Y2FpczZhZXI="; string data = "[" + "{" + "\"P1\": " + Setting.GetParamByTitleSmall("P1").LastValue.Value.ToString().Replace(',', '.') + "," + "\"P2\": " + Setting.GetParamByTitleSmall("P2").LastValue.Value.ToString().Replace(',', '.') + "," + "\"P3\": " + Setting.GetParamByTitleSmall("P3").LastValue.Value.ToString().Replace(',', '.') + "," + "\"T1\": " + Setting.GetParamByTitleSmall("T").LastValue.Value.ToString().Replace(',', '.') + "," + "\"R1\": " + Setting.GetParamByTitleSmall("po").LastValue.Value.ToString().Replace(',', '.') + "," + "\"V1\": " + Setting.GetParamByTitleSmall("V1").LastValue.Value.ToString().Replace(',', '.') + "," + "\"Q1\": " + Setting.GetParamByTitleSmall("Q1").LastValue.Value.ToString().Replace(',', '.') + "," + "\"V2\": " + Setting.GetParamByTitleSmall("V1").LastValue.Value.ToString().Replace(',', '.') + "," + "\"Q2\": " + Setting.GetParamByTitleSmall("Q1").LastValue.Value.ToString().Replace(',', '.') + "," + "\"t\": " + (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds + "000" + "}" + "]"; try { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(data); } WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); } } response.Close(); OnChangedState?.Invoke("Web", 1, ""); } catch (Exception e) { OnChangedState?.Invoke("Web", 0, e.Message); } }
public void Start(bool isCUDR, bool isGS, int intervalReadGS, int intervalReadCUDR, AnyParam firstFlow) { this.isCUDR = isCUDR; this.isGS = isGS; this.intervalReadGS = intervalReadGS; this.intervalReadCUDR = intervalReadCUDR; this.firstFlow = firstFlow; if (isCUDR) { inputCUDR.Master = new ModbusFactory().CreateRtuMaster(new SerialPortAdapter(inputCUDR.SerialPort)); inputCUDR.Master.Transport.ReadTimeout = 500; inputCUDR.Master.Transport.Retries = 1; inputCUDR.Master.Transport.RetryOnOldResponseThreshold = 1; inputCUDR.Master.Transport.SlaveBusyUsesRetryCount = false; try { inputCUDR.SerialPort.Open(); } catch (Exception ex) { OnChangedState?.Invoke("CUDR", 0, ex.Message); } } if (isGS) { inputGS.SerialPort.BaudRate = 57600; try { inputGS.SerialPort.Open(); inputGS.SerialPort.DataReceived -= SerialPort_DataReceived; inputGS.SerialPort.DataReceived += SerialPort_DataReceived; } catch (Exception ex) { OnChangedState?.Invoke("GS", 0, ex.Message); } } timerReadCUDR = 0; timerReadGS = 0; TimerSecond.Start(); }
private void NewStateSwitch(Type StateNext) { CurrentState = OpenStates[StateNext]; OnChangedState.Invoke(CurrentState); }
private void CheckBuffer(byte[] buffer) { int tempLength = bufferInput.Length + buffer.Length; byte[] concat = new byte[tempLength]; for (int i = 0; i < tempLength; i++) { if (i < bufferInput.Length) { concat[i] = bufferInput[i]; } else { concat[i] = buffer[i - bufferInput.Length]; } } bufferInput = concat; int paramIndex = 0; if (readStage) { if (bufferInput.Length >= 76) { readStage = false; if (bufferInput.Length > 90) { } else { string result = Encoding.UTF8.GetString(bufferInput, 10, 64); string stageTitle = result.Split(new string[] { "\0" }, StringSplitOptions.None)[0]; if (ListStages.Count == 0 || ListStages[ListStages.Count - 1].Text != stageTitle) { Application.Current.Dispatcher.Invoke(new System.Threading.ThreadStart(delegate { ListStages.Add(new Stage { ID = r.Next(int.MaxValue), Text = stageTitle, DateTime = DateTime.Now, IsSecond = false, }); })); } OnChangedState?.Invoke("GS", 1, ""); GSerror = false; } } } else { if (bufferInput.Length >= 628) { bool addLog = false; if (CheckCRC(bufferInput)) { for (int i = 8; i < bufferInput.Length - 10; i += 71) { // for (int i = 6; i < bufferInput.Length - 10; i += 69) { byte[] paramValue = new byte[] { bufferInput[i], bufferInput[i + 1], bufferInput[i + 2], bufferInput[i + 3] }; float floatData = BitConverter.ToSingle(paramValue, 0); if (floatData > 1000000 || floatData < -1000000 || (floatData > 0 && floatData < 0.00001) || (floatData < 0 && floatData > -0.00001)) { floatData = -1; addLog = true; } try { inputGS.InputParams[paramIndex].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = inputGS.InputParams[paramIndex].Param.CalcValue(floatData) }); paramIndex++; } catch { } } } else { string path = Constants.ENVPATH + "logfileCRC.txt"; string result = BitConverter.ToString(bufferInput); System.IO.File.AppendAllText(path, DateTime.Now + ";" + result + "\r\n", System.Text.Encoding.UTF8); } if (addLog) { string path = Constants.ENVPATH + "logfile.txt"; string result = BitConverter.ToString(bufferInput); System.IO.File.AppendAllText(path, DateTime.Now + ";" + result + "\r\n", System.Text.Encoding.UTF8); } bufferInput = new byte[0]; readStage = true; inputGS.SerialPort.Write(read13, 0, read13.Length); } } }
public void ReadCUDR() { //inputCUDR.InputParams[0].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 1 }); for (int i = 1; i < inputCUDR.InputParams.Count - 3;) { //inputCUDR.InputParams[i].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = (float)(testRandom.Next(1000) / 10.0) }); //i++; //continue; byte slaveId = Convert.ToByte(inputCUDR.InputParams[i].SlaveID); string[] addressSplit = (inputCUDR.InputParams[i].Address.ToString()).Replace('.', ',').Split(','); ushort startAddress = (ushort)(Convert.ToInt16(addressSplit[0])); if (startAddress == 0) { i++; continue; } ushort numRegisters = 0; int countRegister = 1; switch (inputCUDR.InputParams[i].TypeParam) { case TypeParam.BIT: case TypeParam.WORD: numRegisters = 1; break; case TypeParam.DWORD: case TypeParam.FLOAT: numRegisters = 2; break; } #region определение кол-во читаемых регистров for (int j = i + 1; j < inputCUDR.InputParams.Count && j > -1; j++) { if (inputCUDR.InputParams[j].SlaveID == inputCUDR.InputParams[i].SlaveID && inputCUDR.InputParams[j].Command == inputCUDR.InputParams[i].Command && inputCUDR.InputParams[j].TypeParam == inputCUDR.InputParams[i].TypeParam) { switch (inputCUDR.InputParams[i].TypeParam) { case TypeParam.BIT: case TypeParam.WORD: if (inputCUDR.InputParams[j].Address - (inputCUDR.InputParams[i].Address + (countRegister - 1) * 1) == 1) { countRegister++; } else { j = int.MaxValue; } break; case TypeParam.DWORD: case TypeParam.FLOAT: if (inputCUDR.InputParams[j].Address - (inputCUDR.InputParams[i].Address + (countRegister - 1) * 2) == 2) { countRegister++; } else { j = int.MaxValue; } break; } } else { break; } } //i += countRegister - 1; numRegisters = (ushort)(numRegisters * countRegister); #endregion #region чтение данных ushort[] registers = null; bool[] coils = null; if (inputCUDR.InputParams[i].Command == Command.ReadCoils) { try { coils = inputCUDR.Master.ReadCoils(slaveId, startAddress, numRegisters); } catch { } } if (inputCUDR.InputParams[i].Command == Command.ReadHoldingRegisters) { try { registers = inputCUDR.Master.ReadHoldingRegisters(slaveId, startAddress, numRegisters); } catch { OnChangedState?.Invoke("CUDR", 0, "Устройство не отвечает"); return; i++; } } if ((registers == null) && (coils == null)) { continue; } #endregion #region преобразование прочитанных регистров switch (inputCUDR.InputParams[i].TypeParam) { case TypeParam.WORD: for (int r = 0; r < registers.Length; r++) { Int16 int16value = BitConverter.ToInt16(new byte[] { BitConverter.GetBytes(registers[r])[0], BitConverter.GetBytes(registers[r])[1] }, 0); inputCUDR.InputParams[i++].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = int16value }); } break; case TypeParam.DWORD: for (int r = 0; r < registers.Length; r += 2) { int intValue = BitConverter.ToInt32(new byte[] { BitConverter.GetBytes(registers[r])[0], BitConverter.GetBytes(registers[r])[1], BitConverter.GetBytes(registers[r + 1])[0], BitConverter.GetBytes(registers[r + 1])[1] }, 0); inputCUDR.InputParams[i++].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = intValue }); } break; case TypeParam.FLOAT: for (int r = 0; r < registers.Length; r += 2) { float floatData = BitConverter.ToSingle(new byte[] { BitConverter.GetBytes(registers[r])[0], BitConverter.GetBytes(registers[r])[1], BitConverter.GetBytes(registers[r + 1])[0], BitConverter.GetBytes(registers[r + 1])[1] }, 0); inputCUDR.InputParams[i].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = inputCUDR.InputParams[i].Param.CalcValue(floatData, true) }); i++; } break; } #endregion } if (inputCUDR.InputParams[1].Param.Points.Count <= 1) // если 0 или 1 точка { inputCUDR.InputParams[6].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); // расход НД1 inputCUDR.InputParams[0].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); // концентрация НД1 inputCUDR.InputParams[7].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); // расход НД2 inputCUDR.InputParams[2].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); // концентрация НД2 inputCUDR.InputParams[8].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); //расход ДШ inputCUDR.InputParams[4].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); // концентрация ДШ } else { inputCUDR.InputParams[6].Param.Points.Add(new Service.DataParamPoint { // Расход НД1 DateTime = DateTime.Now, Value = (inputCUDR.InputParams[1].Param.Points[inputCUDR.InputParams[1].Param.Points.Count - 1].Value - inputCUDR.InputParams[1].Param.Points[inputCUDR.InputParams[1].Param.Points.Count - 2].Value) * (3600 / intervalReadCUDR) }); inputCUDR.InputParams[7].Param.Points.Add(new Service.DataParamPoint { // расход НД2 DateTime = DateTime.Now, Value = (inputCUDR.InputParams[3].Param.Points[inputCUDR.InputParams[3].Param.Points.Count - 1].Value - inputCUDR.InputParams[3].Param.Points[inputCUDR.InputParams[3].Param.Points.Count - 2].Value) * (3600 / intervalReadCUDR) }); inputCUDR.InputParams[8].Param.Points.Add(new Service.DataParamPoint { //расход ДШ DateTime = DateTime.Now, Value = (inputCUDR.InputParams[5].Param.Points[inputCUDR.InputParams[5].Param.Points.Count - 1].Value - inputCUDR.InputParams[5].Param.Points[inputCUDR.InputParams[5].Param.Points.Count - 2].Value) * (3600 / intervalReadCUDR) }); //если есть показания расхода и этот расход больше нуля //if (inputGS.InputParams[0].Param.Points.Count > 0 && inputGS.InputParams[0].Param.LastValue.Value > 0.01 ) { if (firstFlow.Value == "1") { inputCUDR.InputParams[0].Param.Points.Add(new Service.DataParamPoint { // концентрация НД1 DateTime = DateTime.Now, Value = (inputCUDR.InputParams[6].Param.Points[inputCUDR.InputParams[6].Param.Points.Count - 1].Value / inputGS.InputParams[0].Param.GetFlow() * (float)100) }); inputCUDR.InputParams[2].Param.Points.Add(new Service.DataParamPoint { // концентрация НД2 DateTime = DateTime.Now, Value = (inputCUDR.InputParams[7].Param.Points[inputCUDR.InputParams[7].Param.Points.Count - 1].Value / inputGS.InputParams[0].Param.GetFlow() * (float)100) }); inputCUDR.InputParams[4].Param.Points.Add(new Service.DataParamPoint { // концентрация ДШ DateTime = DateTime.Now, Value = (inputCUDR.InputParams[8].Param.Points[inputCUDR.InputParams[8].Param.Points.Count - 1].Value / inputGS.InputParams[0].Param.GetFlow() * (float)100) }); } else { if (inputGS.InputParams[2].Param.Points.Count > 0 && inputGS.InputParams[2].Param.LastValue.Value > 0.01) { inputCUDR.InputParams[0].Param.Points.Add(new Service.DataParamPoint { // концентрация НД1 DateTime = DateTime.Now, Value = (inputCUDR.InputParams[6].Param.Points[inputCUDR.InputParams[6].Param.Points.Count - 1].Value / inputGS.InputParams[2].Param.GetFlow() * (float)100) }); inputCUDR.InputParams[2].Param.Points.Add(new Service.DataParamPoint { // концентрация НД2 DateTime = DateTime.Now, Value = (inputCUDR.InputParams[7].Param.Points[inputCUDR.InputParams[7].Param.Points.Count - 1].Value / inputGS.InputParams[2].Param.GetFlow() * (float)100) }); inputCUDR.InputParams[4].Param.Points.Add(new Service.DataParamPoint { // концентрация ДШ DateTime = DateTime.Now, Value = (inputCUDR.InputParams[8].Param.Points[inputCUDR.InputParams[8].Param.Points.Count - 1].Value / inputGS.InputParams[2].Param.GetFlow() * (float)100) }); } else { inputCUDR.InputParams[0].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); inputCUDR.InputParams[2].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); inputCUDR.InputParams[4].Param.Points.Add(new Service.DataParamPoint { DateTime = DateTime.Now, Value = 0 }); } } } OnChangedState?.Invoke("CUDR", 1, ""); }
private void InputWork_OnChangedState(string source, int state, string error) { OnChangedState?.Invoke(source, state, error); }
public static void ChangeState(string source, int state) { OnChangedState?.Invoke(source, state); }