private void button2_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(textBox1.Text)) { DataLogHelper logHelper = new DataLogHelper(); Business.DeviceBusiness deviceBusiness = new Business.DeviceBusiness(); string strText = textBox1.Text; int receiveNumber = strText.Length; if (!string.IsNullOrEmpty(strText)) { logHelper.SocketLog(strText, receiveNumber); #region 对接收到的传感器日志数据进行转换操作 try { SensorDataCollection datas = JsonConvert.DeserializeObject <SensorDataCollection>(strText); //将传感器读取的数据写入生产日志表 foreach (SensorData data in datas) { //判断值类型来提取传感器的状态信息 string strDeviceStatus = "P"; string strSensorId = data.SensorId; string strSensorValueType = data.ValueType; decimal deParValue = data.SensorValue; deviceBusiness.AddDeviceProduceLog(strDeviceStatus, strSensorId, strSensorValueType, deParValue); } } catch { } #endregion } } }
public void open() { #region 加载出对应的设备传感器集合 string strFactoryId = "G01"; AddressSets = DataLogHelper.GetPLCAddressSets(strFactoryId, _typeStr); #endregion //定义需要读取的寄存器数量 Wrapper.RegCount = Convert.ToInt16(AddressSets.Count()); Wrapper.Connect(); try { _thread = new Thread(Receive) { Name = _id.ToString() }; _thread.Start(); _status = "服务连接中"; _isRunning = true; ConnectSocketServer(); } catch (Exception ex) { string errorStr = "[" + _name + "]" + "服务连接失败!" + ex.Message; MessageBox.Show(errorStr, "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Bind() { try { if (!string.IsNullOrWhiteSpace(AssemblyLineId)) { //通过生产线获取对应的所有设备信息和参数 DataLogHelper logHelper = new DataLogHelper(); DeviceProduceLogCollection logs = logHelper.GetDeviceProduceLogCollection(AssemblyLineId); //加载进列表 foreach (DeviceProduceLog log in logs) { int index = this.dvLogList.Rows.Add(); this.dvLogList.Rows[index].Cells["AssemblyLineTitle"].Value = log.AssemblyLineTitle; this.dvLogList.Rows[index].Cells["ProcessTitle"].Value = log.ProcessTitle; this.dvLogList.Rows[index].Cells["SensorName"].Value = log.SensorName; this.dvLogList.Rows[index].Cells["DeviceName"].Value = log.DeviceName; this.dvLogList.Rows[index].Cells["Created"].Value = log.Created; this.dvLogList.Rows[index].Cells["DeviceStatus"].Value = SysHelper.Enums.EnumHelper.GetDeviceActionTypeString(log.DeviceStatus); this.dvLogList.Rows[index].Cells["SensorStatus"].Value = SysHelper.Enums.EnumHelper.GetSenserStatusTypeString(log.SensorStatus); this.dvLogList.Rows[index].Cells["ParType"].Value = SysHelper.Enums.EnumHelper.GetDeviceParameterTypeString(log.ParType); this.dvLogList.Rows[index].Cells["ParUnit"].Value = log.ParUnit; this.dvLogList.Rows[index].Cells["ParValue"].Value = log.ParValue; } } }catch (Exception ex) { } }
public void RequestPlcDataToDB(string strJsonText) { try { if (!string.IsNullOrEmpty(strJsonText)) { int receiveNumber = strJsonText.Length; DataLogHelper logHelper = new DataLogHelper(); logHelper.SocketLog(strJsonText, receiveNumber); #region 对接收到的传感器日志数据进行转换操作 try { HNFactoryAutoSystem.Business.DeviceBusiness deviceBusiness = new HNFactoryAutoSystem.Business.DeviceBusiness(); SensorDataCollection datas = JsonConvert.DeserializeObject <SensorDataCollection>(strJsonText); //将传感器读取的数据写入生产日志表 foreach (SensorData data in datas) { //判断值类型来提取传感器的状态信息 string strDeviceStatus = "P"; string strSensorId = data.SensorId; string strSensorValueType = data.ValueType; decimal deParValue = data.SensorValue; deviceBusiness.AddDeviceProduceLog(strDeviceStatus, strSensorId, strSensorValueType, deParValue); } } catch { } #endregion } } catch { } }
private void RequestToDB(IModbusMessage message) { WriteMultipleRegistersRequest typedResponse = (WriteMultipleRegistersRequest)message; ModbusMessageLog logitem = new ModbusMessageLog() { LogTime = DateTime.Now, LogMessage = message.ToString(), FunctionCode = message.FunctionCode.ToString(), MessageFrame = message.MessageFrame, ProtocolDataUnit = message.ProtocolDataUnit, SlaveAddress = message.SlaveAddress.ToString(), TransactionId = message.TransactionId.ToString() }; int iStartIndex = typedResponse.MinimumFrameSize; int iLen = message.MessageFrame.Length - iStartIndex; byte[] bdata = new byte[iLen]; Array.Copy(message.MessageFrame, iStartIndex, bdata, 0, iLen); string strFrame = System.Text.Encoding.UTF8.GetString(bdata).TrimEnd('\0'); string strUnit = Modbus.ConvertTools.BytesToHexString(message.ProtocolDataUnit); DataLogHelper.AddModbusMessageLog(logitem); //将数据传入后台处理 string strToText = logitem.ToJsonString(); SendToSocketServer(strToText); }
/// <summary> /// 将数据保存起来并发送给后台服务 /// </summary> /// <param name="data"></param> private void RequestToDB(byte[] data) { //地址起点 #region 对寄存器数据进行按需提取 int iIndex = 0; SensorDataCollection sensorDatas = new SensorDataCollection(); foreach (PLCAddressSet addressSet in AddressSets) { byte[] btInfo = new byte[addressSet.ValueLength]; if (data.Length > iIndex) { try { //按指定的寄存器地址来提取数据 Array.Copy(data, iIndex, btInfo, 0, addressSet.ValueLength); //针对提取出来的数据,进行解码操作 string strBxString = ConvertTools.BytesToHexString(btInfo); decimal deValue = decimal.Zero; deValue = Convert.ToInt32(strBxString, 16); //如果是过程值数据需要对参数进行缩放 if (addressSet.ValueType == "NumberValue") { deValue *= addressSet.Scale; } SensorData sensorData = new SensorData() { FactoryId = addressSet.FactoryId, PLCId = addressSet.PLC_Id, PLCAddress = addressSet.PLC_Address, SensorId = addressSet.SensorId, ValueType = addressSet.ValueType, SensorValue = Convert.ToDecimal(strBxString), ReceiveTime = DateTime.Now }; sensorDatas.Add(sensorData); } catch { } //更新下一个寄存器起点 iIndex += addressSet.ValueLength; } } #endregion #region 读取总记录写入数据库 //string strMessage = Encoding.ASCII.GetString(data); string strMessage = string.Format("[{0}]读取设备数据完成", _typeStr); ModbusMessageLog logitem = new ModbusMessageLog() { LogTime = DateTime.Now, LogMessage = strMessage, FunctionCode = FunctionCode.Read.ToString(), MessageFrame = data, ProtocolDataUnit = data, SlaveAddress = _startaddress.ToString(), TransactionId = Wrapper.CurrentDataIndex.ToString() }; DataLogHelper.AddModbusMessageLog(logitem); #endregion //将数据传入后台处理 string strToText = sensorDatas.ToJsonString(); SendToSocketServer(strToText); }
/// <summary> /// 生产线启动生产流程 /// </summary> /// <param name="strAssemblyLineId"></param> public void StartProcess(string strAssemblyLineId, string strCreateUser, DeviceAction deviceAction) { try { //读取生产线数据 DataHelper dataHelper = new DataHelper(); DataLogHelper logHelper = new DataLogHelper(); AssemblyLineInfo assembly = dataHelper.GetAssemblyLine(strAssemblyLineId); //线程休眠的基础单位 int iSleepUnit = 1000; if (assembly != null) { //获取生产线的所有设备信息 DeviceInfoCollection devices = dataHelper.GetAssemblyLineDevices(strAssemblyLineId); //获取相应的工艺流程信息 TechnologicalProcess process = dataHelper.GetTechnologicalProcess(assembly.ProcessId); DateTime dtMainStart = DateTime.Now; #region 主工艺流程日志记录 ProcessLog proLog = new ProcessLog() { AssemblyLineId = strAssemblyLineId, ProcessId = process.ProcessId, Created = DateTime.Now, CreateUser = strCreateUser, ProcessStatus = SysHelper.Enums.ProcessStatusType.Start, ProduceMaterialType = SysHelper.Enums.MaterialTypeEnum.Z, Production = decimal.Zero, TakeTime = 0 }; logHelper.AddMainProcessLog(proLog); #endregion //子工艺流程步骤数量 int iExProcessNum = 3; #region 工艺子流程循环 for (int iExIndex = 1; iExIndex <= iExProcessNum; iExIndex++) { //提取子工艺流程审批环节 TeExProcess exProcess = dataHelper.GetTeExProcess(process.ProcessId, iExIndex); if (exProcess != null) { //子工艺开始时间 DateTime dtExStart = DateTime.Now; #region 子工艺流程日志记录 ExProcessLog exLog = new ExProcessLog() { ProcessLogId = proLog.ProcessLogId, ExProcessId = exProcess.ExProcessId, Created = DateTime.Now, CreateUser = strCreateUser, ProcessStatus = SysHelper.Enums.ProcessStatusType.Start, ProduceMaterialType = SysHelper.Enums.MaterialTypeEnum.Z, Production = decimal.Zero, TakeTime = 0 }; //先记录数据 logHelper.AddExProcessLog(exLog); #endregion //是否达到启动条件 bool isStartExProcess = false; #region 判断启动的设备参数是否达到启动条件 DeviceProduceLog deviceLog = logHelper.GetDeviceProduceLog(exProcess.StartDeviceId, exProcess.ParType); if (deviceLog != null) { if (deviceLog.ParValue.Value >= exProcess.ParValue) { isStartExProcess = true; } } //循环等待达到启动条件 while (!isStartExProcess) { //每个循环先暂停10秒 int iStartSleep = iSleepUnit * 10; System.Threading.Thread.Sleep(iStartSleep); deviceLog = logHelper.GetDeviceProduceLog(exProcess.StartDeviceId, exProcess.ParType); if (deviceLog != null) { if (deviceLog.ParValue.Value >= exProcess.ParValue) { isStartExProcess = true; } } } #endregion if (isStartExProcess) { #region 循环启动设备 foreach (ExProcessStep step in exProcess.Steps) { //先找到需要操作的设备 foreach (DeviceInfo device in devices) { if (device.ProcessDeviceId == step.ProcessDeviceId) { string strDeviceId = device.DeviceId; //定义设备的操作类型 //提取步骤的参数控制数 foreach (ExProcessStepPars par in step.StepPars) { #region 记录操作的日志 DeviceActionLog actionLog = new DeviceActionLog() { DeviceId = strDeviceId, Created = DateTime.Now, CreateUser = strCreateUser, SensorId = par.SensorId, ActionType = par.ActionType, ParType = par.ParType, ParUnit = par.ParUnit, ParValue = par.ParValue, ToDeviceId = string.Empty, ToSensorId = string.Empty }; logHelper.AddDeviceActionLog(actionLog); #endregion //将操作放入委托里面进行执行(步骤执行完成才执行后续步骤) if (deviceAction(strDeviceId, par.SensorId, par.ActionType, par.ParType, par.ParValue)) { //判断是否需要等待 if (par.ParTime > 0) { int iSleepSet = par.ParTime * 60 * iSleepUnit; System.Threading.Thread.Sleep(iSleepSet); } //判断是否是完成参数 if (par.IsFinish) { } } } break; } } } #endregion } //子工艺完成时间 DateTime dtExEnd = DateTime.Now; TimeSpan tSpan = dtExEnd - dtExStart; //得到子工艺完成花费的分钟 exLog.TakeTime = Convert.ToInt32(tSpan.TotalMinutes); exLog.FinishTime = dtExEnd; exLog.ProcessStatus = SysHelper.Enums.ProcessStatusType.End; //exLog.ProduceMaterialType = SysHelper.Enums.MaterialTypeEnum. logHelper.UpdateExProcessLog(exLog); } } #endregion DateTime dtMainEnd = DateTime.Now; TimeSpan mSpan = dtMainEnd - dtMainStart; proLog.TakeTime = Convert.ToInt32(mSpan.TotalMinutes); proLog.FinishTime = dtMainEnd; proLog.ProcessStatus = SysHelper.Enums.ProcessStatusType.End; logHelper.UpdateMainProcessLog(proLog); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 接收消息 /// </summary> /// <param name="clientSocket"></param> private void ReceiveMessage(object clientSocket) { Socket myClientSocket = (Socket)clientSocket; while (true) { try { byte[] buffer = new byte[1024 * 1024 * 2]; //实际接受到的有效字节数 int receiveNumber = myClientSocket.Receive(buffer); if (receiveNumber == 0) { break; } //string strInfo = string.Format("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber)); string strInfo = Encoding.UTF8.GetString(buffer, 0, receiveNumber); string[] strTexts = strInfo.Split('|'); ////通过clientSocket接收数据 //int receiveNumber = myClientSocket.Receive(result); //string strInfo = string.Format("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber)); ////MessageBox.Show(strInfo); DataLogHelper logHelper = new DataLogHelper(); Business.DeviceBusiness deviceBusiness = new Business.DeviceBusiness(); foreach (string strText in strTexts) { if (!string.IsNullOrEmpty(strText)) { logHelper.SocketLog(strText, receiveNumber); #region 对接收到的传感器日志数据进行转换操作 try { SensorDataCollection datas = JsonConvert.DeserializeObject <SensorDataCollection>(strText); //将传感器读取的数据写入生产日志表 foreach (SensorData data in datas) { //判断值类型来提取传感器的状态信息 string strDeviceStatus = "P"; string strSensorId = data.SensorId; string strSensorValueType = data.ValueType; decimal deParValue = data.SensorValue; deviceBusiness.AddDeviceProduceLog(strDeviceStatus, strSensorId, strSensorValueType, deParValue); } } catch { } #endregion } } } catch (Exception ex) { //Console.WriteLine(ex.Message); //ServerMainFormtxt_Log.AppendText(ex.Message + "\r \n"); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); break; } } }