写入PLC期间数据丢失

时间:2019-07-16 12:58:23

标签: c# wpf plc opc-ua

在我的程序中,我需要读取xml文件并将该xml文件中的特定节点发送到PLC。但是在将节点数据写入PLC的过程中,其中一些数据仍然丢失。

我尝试了很多事情,最初使用OPC更改为OPC UA。然后改变了编码方式。但是这些都不起作用,数据只会丢失。 下面是我读取xml文件,收集节点(二进制代码)并写入PLC的代码

private void readFromXml()
    {
        Task.Run(() =>
        {
            try
            {
                string inputPath = string.Empty;
                string backupPath = string.Empty;
                string pathConfig = iOPC.ReadTag<string>(UTAC_Tags.Path_Config);
                bool lotRetest = iOPC.ReadTag<bool>(UTAC_Tags.Lot_Retest);
                switch (pathConfig)
                {
                    case "Server":
                        inputPath = lotRetest == false ? SettingFileModel.ServerFolderPath_Input : SettingFileModel.ServerFolderPath_Output;
                        backupPath = lotRetest == false ? SettingFileModel.ServerFolderPath_Backup : FolderPath.RetestXml;
                        break;
                    case "Local":
                        inputPath = lotRetest == false ? SettingFileModel.LocalFolderPath_Input : SettingFileModel.LocalFolderPath_Output;
                        backupPath = lotRetest == false ? SettingFileModel.LocalFolderPath_Backup : FolderPath.RetestXml;
                        break;
                    case "SecsGem":
                        inputPath = lotRetest == false ? SettingFileModel.SecsGemFolderPath : string.Empty;
                        break;
                    default:
                        break;
                }
                logger.Debug("---------- Start Read Xml ----------");
                //ReadXmlStripModel.StripBarcode = iOPC.ReadTag<string>(ReadXmlStripModel.StripTagsModel.StripBarcodeTag);
                ReadXmlStripModel.StripBarcode = siemensOPCUA.Read<string>(ReadXmlStripModel.StripTagsModel.StripBarcodeTag);
                logger.Debug($"Strip Barcode: {ReadXmlStripModel.StripBarcode}");
                if (!string.IsNullOrEmpty(inputPath) && (!string.IsNullOrEmpty(backupPath) || pathConfig == "SecsGem"))
                {
                    string inputFilePath = $"{inputPath}{ReadXmlStripModel.StripBarcode}.xml";
                    string backupFilePath = $"{backupPath}{ReadXmlStripModel.StripBarcode}.xml";
                    if (File.Exists(inputFilePath))
                    {
                        XmlFileModel xmlFileModel = XmlTools.Deserialize<XmlFileModel>(inputFilePath);
                        int column = 1;
                        int row = 1;
                        ReadXmlStripModel.UpstreamPass = xmlFileModel.SubstrateMaps.SubstrateMap.Overlay.BinCodeMap.BinDefinition.Find(
                            x => x.BinDescription.Contains("Good island") && x.BinQuality.Contains("GoodDevice") && x.Pick == true).BinCode;
                        siemensOPCUA.Write(ReadXmlStripModel.StripTagsModel.UpstreamPassTag, ReadXmlStripModel.UpstreamPass);
                        //iOPC.WriteTag(ReadXmlStripModel.StripTagsModel.UpstreamPassTag, ReadXmlStripModel.UpstreamPass);
                        xmlFileModel.SubstrateMaps.SubstrateMap.Overlay.BinCodeMap.BinCode.ForEach(x =>
                        {
                            string binCode = x;
                            while (binCode.Length > 0)
                            {
                                bool stringContain = false;
                                int startPosition = binCode.Length;
                                int stringLength = binCode.Length;
                                xmlFileModel.SubstrateMaps.SubstrateMap.Overlay.BinCodeMap.BinDefinition.ForEach(y =>
                                {
                                    stringContain = true;
                                    if (binCode.Contains(y.BinCode))
                                    {
                                        if (binCode.IndexOf(y.BinCode, 0) < startPosition)
                                        {
                                            startPosition = binCode.IndexOf(y.BinCode, 0);
                                            stringLength = y.BinCode.Length;
                                        }
                                    }
                                });
                                if (stringContain)
                                {
                                    if (ReadXmlStripModel.StripUnitModels.ToList().Exists(y => y.Row == row && y.Column == column))
                                    {
                                        Thread.Sleep(50);
                                        ReadXmlStripModel.StripUnitModels.ToList().Find(
                                            y => y.Row == row && y.Column == column).UpstreamResult = binCode.Substring(startPosition, stringLength);
                                        siemensOPCUA.Write(ReadXmlStripModel.StripUnitModels.ToList().Find(
                                            y => y.Row == row && y.Column == column).StripUnitTagsModel.UpstreamResultTag,
                                            ReadXmlStripModel.StripUnitModels.ToList().Find(
                                                y => y.Row == row && y.Column == column).UpstreamResult);
                                        //using (StreamWriter writer = new StreamWriter(FilePath.fileContent))
                                        //{
                                        //    foreach (var item in ReadXmlStripModel.StripUnitModels)
                                        //    {
                                        //        writer.WriteLine(String.Format("Tag:{0}", item.StripUnitTagsModel.UpstreamResultTag));
                                        //        writer.WriteLine(String.Format("Row/Column:[{0},{1}]", item.Row, item.Column));
                                        //        writer.WriteLine(String.Format("UpstreamResult:{0}", item.UpstreamResult));
                                        //    }
                                        //}
                                            logger.Debug($"[Row: {row}, Column: {column}]:" +
                                                $" [Tag: {ReadXmlStripModel.StripUnitModels.ToList().Find(y => y.Row == row && y.Column == column).StripUnitTagsModel.UpstreamResultTag}," +
                                                $" Value: {ReadXmlStripModel.StripUnitModels.ToList().Find(y => y.Row == row && y.Column == column).UpstreamResult}]");
                                    }
                                    row += 1;
                                }
                                binCode = binCode.Remove(startPosition, stringLength);
                            }
                            column += 1;
                            row = 1;
                        });
                        iOPC.WriteTag(UTAC_Tags.ReadXml_Done, true);
                        if (pathConfig != "SecsGem")
                        {
                            File.Copy(inputFilePath, backupFilePath, true);
                            File.Delete(inputFilePath);
                        }
                        logger.Debug("Read Xml Done");
                    }
                }
            }
            catch (Exception ex) { logger.Error(ex); }
        });
    }

这是错误日志的结果之一。我感谢任何帮助。我已经在这个问题上停留了很长时间了。

“ Strip_Data_Z1_DB”。“ Strip”。“ Unit” [6,13]“。” UpstreamResult“,0001:写入新值失败:BadNodeIdUnknown

1 个答案:

答案 0 :(得分:0)

服务器告诉您您要写入的NodeId不存在或未知。

您确定构造正确吗?