/// <summary> /// Disconnette un PLC /// </summary> /// <param name="PLC">Dati del PLC da disconnettere</param> /// <returns>true set tutto ok, altrimenti false</returns> private bool DisconnectPLC(PLCData PLC) { bool RetValue = true; if (!_PLCs.ContainsKey(PLC.PLCName)) { // log Logger.WarnFormat("PLC [{0}] not connected", PLC.PLCName); RetValue = false; } else { try { var plc = _PLCs[PLC.PLCName]; // Disconnetto plc.Disconnect(); // elimino dalla lista _PLCs.Remove(PLC.PLCName); } catch (Exception exc) { // log Logger.WarnFormat("PLC [{0}] error in disconnection : {1}", PLC.PLCName, exc.Message); RetValue = false; } } return(RetValue); }
public void LoadPLCData(string fileName) { FileInfo file = new FileInfo(fileName); if (file.Exists == false) { throw new FileNotFoundException(); } PLCData pLCData = null; using (Stream stream = file.OpenRead()) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(PLCData)); pLCData = xs.Deserialize(stream) as PLCData; } if (pLCData != null) { this.R.SetNumberDatas(pLCData.R); for (int i = 0; i < pLCData.Registers.Count; i++) { this.Registers.SetValue((ushort)pLCData.Registers[i].Num, pLCData.Registers[i].Value); } } }
public void LoadAdress() { ListMachines = new List <MachineItem>(); PLCData pLCData = new PLCData(); // ListMachines.Add(new MachineItem { IP = "172.16.1.145", Line = "L03", IsEnable = true }); ListMachines = pLCData.GetIpMachineRuning(); }
public void LoadAdress() { ListMachines = new List <MachineItem>(); PLCData pLCData = new PLCData(); ListMachines = pLCData.GetIpMachineRuning(); ListBarcode = VariablePLC.barcodeaddress(); tagsbarcode = new List <Tag>(); ListTagWrite = new List <Tag>(); Tag tag = new Tag(VariablePLC.AddingAvaiable, ""); ListTagWrite.Add(tag); tag = new Tag(VariablePLC.GapQty, ""); ListTagWrite.Add(tag); foreach (var item in ListBarcode) { tagsbarcode.Add(new Tag(item, "")); } ListError = VariablePLC.List38Errors(); tagsError = new List <Tag>(); foreach (var item in ListError) { tagsError.Add(new Tag(item, "")); } ListRework = VariablePLC.List38Reworks(); tagsRework = new List <Tag>(); foreach (var item in ListRework) { tagsRework.Add(new Tag(item, "")); } tags = new List <Tag>(); tag = new Tag(VariablePLC.Good_Products_Total, ""); tags.Add(tag); tag = new Tag(VariablePLC.NG_Products_Total, ""); tags.Add(tag); tag = new Tag(VariablePLC.RW_Products_Total, ""); tags.Add(tag); tag = new Tag(VariablePLC.OnOFF, ""); tags.Add(tag); }
/// <summary> /// 事件自动触发处理,生成PLC上报数据 /// </summary> /// <param name="sender"></param> /// <param name="Args"></param> private void AutoSendDataHandler(object sender, PlcEvent Args) { if (Args.ACTION_ID != null && Args.EQUIP_CODE != null && Args.ACTION_ID.Length > 0 && Args.EQUIP_CODE.Length > 0) { if (Args.Type == "RGV") { RGV_LOCATION node = new RGV_LOCATION(); node.EQUIP_CODE = Args.EQUIP_CODE; node.WC_CODE = LindeStr; node.RGV_LOCATION_Column = Args.ACTION_ID; node.STATE = sbyte.Parse(Args.Value.ToString()); node.RGV_SPEED = decimal.Parse(Args.ArrLabel); node.HAPPEN_TIME = DateTime.Now; RGVData SendData = new RGVData(node); lock (m_ShareData.m_lockrgvData) { m_ShareData.rgvData.Add(SendData); while (m_ShareData.rgvData.Count > m_ShareData.agvData_CountMax) { m_ShareData.rgvData.RemoveAt(0); } } } else { MONITOR_INFO node = new MONITOR_INFO(); node.ACTION_ID = Args.ACTION_ID; node.EQUIP_CODE = Args.EQUIP_CODE; node.HAPPEN_TIME = DateTime.Now; PLCData SendData = new PLCData(node); lock (m_ShareData.m_lockplcData) { m_ShareData.plcData.Add(SendData); while (m_ShareData.plcData.Count > m_ShareData.plcData_CountMax) { m_ShareData.plcData.RemoveAt(0); } } } } }
public void SavePLCData(string fileName) { PLCData pLCData = new PLCData(); pLCData.R = this.R.NumberDatas; pLCData.Registers = this.Registers.NumberDatas; FileInfo file = new FileInfo(fileName); if (file.Directory.Exists == false) { file.Directory.Create(); } using (Stream stream = file.Create()) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(pLCData.GetType()); xs.Serialize(stream, pLCData); } }
/// <summary> /// 作为服务器接收PLC数据 /// </summary> /// <returns></returns> public void ServerReceive() { while (null != this.socketServerWrapper.TempClientSocket) { //接收PLC客户端发送过来的消息并回复 byte[] receiveBuf = new byte[256]; if (this.socketServerWrapper.Read(receiveBuf)) { ModBusRequestHeader tempModBusReqHeader = new ModBusRequestHeader(receiveBuf, 0); ModBusResponseHeader respHeader = new ModBusResponseHeader(tempModBusReqHeader); //将接收到的数据追加到消息队列中 PLCData data = new PLCData(receiveBuf, ModBusRequestHeader.TotalLength); lock (BasicData.PLCQueue) { BasicData.PLCQueue.Enqueue(data); } byte[] resp = new byte[ModBusResponseHeader.TotalLength]; respHeader.ToBytes(resp, 0); //默认回复PLC ModBus protocol头 this.socketServerWrapper.Write(resp); } else //客户端终端连接 { Flag.PLCConnected = false; this.socketServerWrapper.TempClientSocket.Close(); this.socketServerWrapper.TempClientSocket = null; Log.Write("Server: PLC断开了连接."); } } //重新监听 Listen(); }
private bool RequestPLCStatus(string plcName) { bool RetValue = true; //Create a DotNetMQ Message to send var message = mdsClient.CreateMessage(); //Set destination application name message.DestinationApplicationName = PLCServerApplicationName; //Create a message var MsgData = new PLCData { MsgCode = MsgCodes.GetPLCStatus, PLCName = plcName }; //Set message data message.MessageData = GeneralHelper.SerializeObject(MsgData); // message.MessageData = Encoding.UTF8.GetBytes(messageText); message.TransmitRule = MessageTransmitRules.NonPersistent; try { //Send message message.Send(); } catch { // non sono riuscito a inviare il messaggio Logger.InfoFormat("Messaggio non inviato"); RetValue = false; } return(RetValue); }
/// <summary> /// 更新UI文本框邮件数据处理逻辑 /// </summary> private void UpdateUI() { while (true) { if (needUpdate) { if (BasicData.PLCQueue.Count > 0) { PLCData data = new PLCData(); lock (BasicData.PLCQueue) { data = BasicData.PLCQueue.Dequeue(); } if (this.InvokeRequired) { //更新UI数据 this.Invoke(UpdateUIFunction, new object[] { data.MailId, data.Volume, data.Weight }); //开启计时器 this.Invoke(UpdateTimerFunction); } else { UpdateUIAction(data.MailId, data.Volume, data.Weight); UpdateTimerAction(); } needUpdate = false; //查找对应的图像及条码信息并显示出来 bool findPic = false; while (!findPic && !needUpdate) { //查找条码 if (BasicData.OBRQueue.Count > 0) { //OBRData obrData = new OBRData(); OBRData obrData = BasicData.OBRQueue.Peek(); //匹配Mail ID // 当OBR中含有PLC未传出来的邮件ID while (obrData.MailId < data.MailId && BasicData.OBRQueue.Count > 0) { lock (BasicData.OBRQueue) { BasicData.OBRQueue.Dequeue(); } obrData = BasicData.OBRQueue.Peek(); } // 当ID等于PLC数据ID if (obrData.MailId == data.MailId) { lock (BasicData.OBRQueue) { BasicData.OBRQueue.Dequeue(); } if (this.InvokeRequired) { //更新UI数据 this.Invoke(UpdateBarcodeFunction, new object[] { obrData.BarCode }); } else { UpdateBarcodeAction(obrData.BarCode); } } } //查找图像 string fileName = data.MailId.ToString() + ".jpg"; string filePath = ConfigurationManager.AppSettings["PicDirectory"] + fileName; //d:\MailPic\1.jpg if (System.IO.File.Exists(filePath)) { if (this.InvokeRequired) { this.Invoke(UpdatePicFunction, new object[] { filePath }); } else { UpdatePicAction(filePath); } findPic = true; } } //add for test //Thread.Sleep(10); //wrapper.ClientSend(data.MailId.ToString(), OperationAction.Normal); //needUpdate = true; // } } } }
public PLCData DecodePLCData(byte[] rcvData) { if (rcvData[0] != (int)CONTROL_CODE.STX) return null; PLCData data = new PLCData(); int iOffset = 0; // 1. 控制代碼 data.m_controlCode = (CONTROL_CODE)Enum.Parse(typeof(CONTROL_CODE), rcvData[iOffset++].ToString()); // 2. 站號 byte[] stationNum = new byte[2]; Array.Copy( rcvData, iOffset, stationNum, 0, stationNum.Length); iOffset += 2; data.m_StationNum = Encoding.Default.GetString(stationNum); //3. PC 號 byte[] pcNum = new byte[2]; Array.Copy( rcvData, iOffset, pcNum, 0, pcNum.Length); iOffset += 2; data.m_PCNum = Encoding.Default.GetString(pcNum); //4. Data byte[] tmpdata = new byte[rcvData.Length - 8]; Array.Copy(rcvData, iOffset, tmpdata, 0, tmpdata.Length); data.m_RcvData = Encoding.Default.GetString(tmpdata); // check which type of int iIdx = iOffset; for( ;iIdx < rcvData.Length; ++iIdx) if( rcvData[iIdx] == (byte)CONTROL_CODE.ETX ) break; int dataLength = iIdx - iOffset + 1; switch(dataLength) { case 17: // This is Read D type from PLC : D1616 ~ D1619(一共 4*4 = 16 byte) data.m_DataType = DATA_TYPE.D_Type; break; case 8: // This is Read M type from PLC : M2120 ~ M2128(一共 9*1 = 9 byte) data.m_DataType = DATA_TYPE.M_Type; break; default: return null; // Unknown Type } return data; }