public partial class ParseXmlFilesForm : Form { public static WirelessCharge celfras = new WirelessCharge(); public static StreamWriter logSW = null; public static FileStream logFS = null; #region 变量声明 private string xmlFilePath; private string rootName; DataTable dt = new DataTable(); private List<NodeMes> nodeList = new List<NodeMes>(); private Boolean bState; private string appendText; #endregion public ParseXmlFilesForm() { InitializeComponent(); } private void ParseXmlFilesForm_Load(object sender, EventArgs e) { dt.Columns.Add("InputName"); dt.Columns.Add("InputValue"); dt.Columns.Add("OutputName"); dt.Columns.Add("OutputValue"); dataGridView1.DataSource = dt; dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellValueChanged); btnSave.Enabled = false; btnOpenFile.Height = 25; btnOpenFile.Width = 60; btnSave.Height = 25; btnSave.Width = 60; btnRun.Height = 25; btnRun.Width = 60; btnClear.Height = 25; btnClear.Width = 60; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; } //析构函数 ~ParseXmlFilesForm() { } #region 将xml文件转换为TreeNode public void Xml2Tree() { //string xmlFilePath = "E:\test.xml"; XmlDocument xmlDoc = GetXmlDocByFilePath(xmlFilePath); //xmlDoc.Load(this.xmlFilePath); //获取根节点的名字 rootName = xmlDoc.DocumentElement.Name; //获取根节点 XmlNode root = xmlDoc.SelectSingleNode(rootName); //把根节点添加到TreeView TreeNode rootTreeNode = new TreeNode(); rootTreeNode.Text = rootName; treeView1.Nodes.Add(rootTreeNode); TransXml(root.ChildNodes, rootTreeNode, ""); //ShowNodeMes(); //MessageBox.Show(treeView1.Nodes[0].Nodes[4].Nodes[9].Nodes[2].Nodes.Count + ""); } /// <summary> /// 显示listNode中所有testNode的信息 /// </summary> private void ShowNodeMes() { byte I2CAddress = Convert.ToByte(nodeList[0].NodeList[0].ChildNodes[3].InnerText.ToString(), 16); richTextBox1.AppendText(I2CAddress.ToString()); try { foreach (NodeMes n in nodeList) { richTextBox1.AppendText(n.NodeName + "\t"); foreach (XmlAttribute attr in n.AttrColl) { richTextBox1.AppendText(attr.Name + " = " + attr.Value + "\t"); } richTextBox1.AppendText("\n**************" + n.NodeList[0].Name + "*****************\n"); foreach (XmlNode node in n.NodeList[0].ChildNodes) { richTextBox1.AppendText("\n" + node.Name + " = " + node.InnerText); } richTextBox1.AppendText("\n**************" + n.NodeList[1].Name + "*****************\n"); foreach (XmlNode node in n.NodeList[1].ChildNodes) { richTextBox1.AppendText("\n" + node.Name + " = " + node.InnerText); } richTextBox1.AppendText("\n----------------------------------------------------------\n"); } } catch (Exception ex) { } } /// <summary> /// 将xml中node的Attribute值添加到TreeView中 /// </summary> /// <param name="subXmlNode"></param> /// <param name="node"></param> private void AddAttr2Tree(XmlNode subXmlNode, TreeNode node) { TreeNode attrNode = new TreeNode(); foreach (XmlAttribute attr in subXmlNode.Attributes) { attrNode.Name = attr.Name; attrNode.Text = attrNode.Name + " = " + attr.Value; node.Nodes.Add(attrNode); node.Tag = "isAttribute"; } } private void TransXml(XmlNodeList childNodes, TreeNode node, string order) { for (int i = 0; i < childNodes.Count; i++) { XmlNode xmlNode = childNodes[i]; TreeNode subTreeNode = new TreeNode(); AddNode2List(xmlNode); #region 添加Attribute节点 try { if (xmlNode.Attributes.Count > 0 && xmlNode.Attributes != null) { AddAttr2Tree(xmlNode, subTreeNode); } } catch (Exception ex) { } #endregion #region 添加非Attribute 节点 if (xmlNode.HasChildNodes) { subTreeNode.Text = "[" + order + i.ToString("X") + "]" + xmlNode.Name; subTreeNode.Name = xmlNode.Name; subTreeNode.Tag = "isTreeNode"; TransXml(xmlNode.ChildNodes, subTreeNode, order + i.ToString("X")); } else { if (xmlNode.Value != null) { subTreeNode.Text = xmlNode.InnerText; subTreeNode.Name = xmlNode.Name; subTreeNode.Tag = "isTreeNode"; } else { subTreeNode.Text = xmlNode.Name; subTreeNode.Name = xmlNode.Name; subTreeNode.Tag = "NoValue"; } } node.Nodes.Add(subTreeNode); //node.Tag = "isTreeNode"; #endregion #region 判断节点是不是包含input and output 的testNode if (subTreeNode.Nodes.Count >= 3) { string[] strInput = subTreeNode.Nodes[1].Text.Split(‘]‘); string[] strOutput = subTreeNode.Nodes[2].Text.Split(‘]‘); if (strInput.Length == 2 && strOutput.Length == 2) { string inputName = strInput[1]; string outputName = strOutput[1]; if ("INPUT".Equals(inputName) && "OUTPUT".Equals(outputName)) { //richTextBox1.AppendText(input + "\t" + output + "\n"); subTreeNode.Tag = "testNode"; } } } #endregion } } /// <summary> /// 将testNode保存到nodeList中 /// </summary> /// <param name="xmlNode"></param> public void AddNode2List(XmlNode xmlNode) { NodeMes nodeMes = new NodeMes(xmlNode.Name, xmlNode.InnerText, xmlNode.ChildNodes, xmlNode.Attributes); if (nodeMes.NodeList.Count >= 2 && "INPUT".Equals(nodeMes.NodeList[0].Name) && "OUTPUT".Equals(nodeMes.NodeList[1].Name)) { nodeList.Add(nodeMes); } } public XmlDocument GetXmlDocByFilePath(string xmlFilePath) { if (string.IsNullOrEmpty(xmlFilePath) || !File.Exists(xmlFilePath)) { return null; } XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; try { XmlReader reader = XmlReader.Create(@xmlFilePath, settings); xmlDoc.Load(reader); reader.Close(); } catch (Exception ex) { MessageBox.Show("请检查Xml文件的格式是否正确,文件存放路径为:" + xmlFilePath + "\n错误信息提示:" + ex.ToString(), "提示"); } return xmlDoc; } #endregion #region click event private void btnOpenFile_Click(object sender, EventArgs e) { openFileDialog1.Filter = "XML文件|*.xml"; if (openFileDialog1.ShowDialog() == DialogResult.Cancel) { return; } else { xmlFilePath = System.IO.Path.GetFullPath(openFileDialog1.FileName); foreach (string str in comboBox1.Items) { if (xmlFilePath.CompareTo(str) == 0) { MessageBox.Show("你已经打开该xml文件!","提示"); return; } } comboBox1.Text = xmlFilePath; comboBox1.Items.Add(xmlFilePath); Xml2Tree(); btnSave.Enabled = true; } } private void btnClear_Click(object sender, EventArgs e) { int nodeCount = treeView1.Nodes.Count; for (int i = 0; i < nodeCount; i++) { treeView1.Nodes[0].Remove(); } } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { dt.Clear(); TreeNode node = treeView1.SelectedNode; if ("testNode".Equals(node.Tag)) { //将选择的node的信息显示到DataGridView中 AddNode2GridView(node); } } #endregion #region DataGridView的处理 private void AddNode2GridView(TreeNode node) { int inCount = node.Nodes[1].Nodes.Count; int outCount = node.Nodes[2].Nodes.Count; if (inCount > 0 && outCount > 0) { int max = inCount >= outCount ? inCount : outCount; if (max == inCount) { for (int i = 0; i < max; i++) { if (i > outCount - 1) { DataRow dr = dt.NewRow(); dr["OutputName"] = ""; dr["OutputValue"] = ""; dr["InputName"] = node.Nodes[1].Nodes[i].Text; dr["InputValue"] = node.Nodes[1].Nodes[i].Nodes[0].Text; dt.Rows.Add(dr); } else { DataRow dr = dt.NewRow(); dr["OutputName"] = node.Nodes[2].Nodes[i].Text; try { dr["OutputValue"] = node.Nodes[2].Nodes[i].Nodes[0].Text; } catch (Exception ex) { dr["OutputValue"] = ""; } dr["InputName"] = node.Nodes[1].Nodes[i].Text; dr["InputValue"] = node.Nodes[1].Nodes[i].Nodes[0].Text; dt.Rows.Add(dr); } } } else { for (int i = 0; i < outCount; i++) { if (i > inCount - 1) { DataRow dr = dt.NewRow(); dr["OutputName"] = node.Nodes[2].Nodes[i].Text; dr["OutputValue"] = node.Nodes[2].Nodes[i].Nodes[0].Text; dr["InputName"] = ""; dr["InputValue"] = ""; dt.Rows.Add(dr); } else { DataRow dr = dt.NewRow(); dr["OutputName"] = node.Nodes[2].Nodes[i].Text; dr["OutputValue"] = node.Nodes[2].Nodes[i].Nodes[0].Text; dr["InputName"] = node.Nodes[1].Nodes[i].Text; dr["InputValue"] = node.Nodes[1].Nodes[i].Nodes[0].Text; dt.Rows.Add(dr); } } } } } //dataGridView中单元格内容修改的事件处理 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex % 2 != 0) { string cellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); string cellName = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString(); CircleNode(cellName, cellValue, treeView1.Nodes); } } //循环遍历所有节点,找到对应DataGridView修改的节点 public void CircleNode(string nodeName, string nodeText, TreeNodeCollection nodeColl) { foreach (TreeNode node in nodeColl) { if (nodeName.Equals(node.Text)) { node.Nodes[0].Text = nodeText; return; } if (node.Nodes.Count > 0) { CircleNode(nodeName, nodeText, node.Nodes); } } } #endregion #region 保存处理 private void btnSave_Click(object sender, EventArgs e) { try { SaveTreeView2XML(); MessageBox.Show("保存成功!"); } catch (Exception ex) { MessageBox.Show("保存失败!"); } } private void SaveTreeView2XML() { XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes"); XDocument doc = new XDocument(dec); XElement root = new XElement(rootName); foreach (TreeNode node in treeView1.Nodes[0].Nodes) { XElement e = CreateElements(node, root); root.Add(e); } doc.Add(root); doc.Save("TreeView2XML.xml"); } private XElement CreateElements(TreeNode node, XElement elementRoot) { XElement root = CreateElement(node); if (node.Nodes.Count > 0) { foreach (TreeNode n in node.Nodes) { XElement e = CreateElements(n, root); if (n.Tag != null) { if ("isAttribute".Equals(n.Tag.ToString())) { string[] str = n.Text.ToString().Replace(" ", "").Split(‘=‘); root.SetAttributeValue(str[0], str[1]); } else if (n.Nodes.Count > 0) root.Add(e); else if (n.Tag.Equals("NoValue")) { root.Add(e); //root.Add("<" + n.Name + ">" + "</" + n.Name + ">"); } else root.Add(n.Text.ToString()); } } } return root; } private XElement CreateElement(TreeNode node) { XElement element = null; try { if (node.Nodes.Count > 0) { element = new XElement(node.Name); } } catch (Exception ex) { richTextBox1.AppendText(ex.Message); } return element; } #endregion public bool CFS_CSV_Init(ref string sLogName) { try { //code string logName = System.Environment.CurrentDirectory + "\\celfras" + DateTime.Now.ToString("-MM-dd-HH-mm-ss") + ".csv"; sLogName = logName; logFS = new FileStream(logName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write); logSW = new StreamWriter(logFS); logSW.WriteLine("test item,test value,results"); logSW.WriteLine("BGR test,0x21,22.34"); logSW.WriteLine("OTP test,0x11,21.34"); logSW.Flush(); return true; } catch (Exception ex) { logSW.WriteLine("[ERR]" + ex.Message); logSW.Flush(); return false; } } static void TestDUTCWQ1100() { string sLogName = null; celfras.CFS_LOG_Init(ref sLogName); celfras.CFS_CWQ1100_I2CInit(0x24, 0.15); celfras.CFS_CWQ1000_SetGPIO2Low(); //int iData = 0; //celfras.CFS_DUT_ReadRegister(0x5c,ref iData); celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x41); // celfras.CFS_DUT_ReadRegister(0x5c, ref iData); celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x42); // celfras.CFS_DUT_ReadRegister(0x5c, ref iData); celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x43); celfras.CFS_Term(); } static void TestPWR() { celfras.CFS_PWR_Init("GPIB0::5::INSTR"); // connect error celfras.CFS_PWR_OnOff(true); celfras.CFS_PWR_SetVoltage(7.0); celfras.CFS_PWR_SetCurrent(1.0); double dVoltage = 0.0; celfras.CFS_PWR_GetVoltage(ref dVoltage); double dCurrent = 0.0; celfras.CFS_PWR_GetCurrent(ref dCurrent); } private void btnRun_Click(object sender, EventArgs e) { appendText = "初始化DUT, OSC, PWD, 关闭电子负载,VRECT = 5.5V VPP = 3V \n"; richTextBox1.Text = appendText; string logName = null; celfras.CFS_LOG_Init(ref logName); InvokeDll(); } public void InvokeDll() { try { foreach (NodeMes node in nodeList) { //XmlNodeList list = node.NodeList; switch (node.NodeName) { case "Init_DUT": RunInitDut(node); break; case "Init_Tester_PWR": RunInitTesterPWR(node); break; case "Init_CFS_OSC": Run_Init_CFS_OSC(node); break; case "BGRVoltageCalibration": Run_BGR_Voltage_Calibration(node); break; case "OSCFrequentyCalibration": Run_OSC_Frequenty_Calibration(node); break; case "VOUT_Calibration": Run_VOUT_Calibration(node); break; case "TRIM_ADC_IOUT_Calibration": Run_TRIM_ADC_IOUT_Calibration(node); break; case "OTP_Write": Run_OTP_Write(node); break; case "BGR_Voltage_Test": Run_BGR_Voltage_Test(); break; case "OSC_Frequency_Test": Run_OSC_Frequency_Test(); break; case "LDO_Voltage_Test": break; case "ADC_Test": break; case "Current_Sensor_Test": break; case "ENB_Test": break; case "OVP_Test": break; case "OCL_Test": break; case "Test_Port_Test": break; default: break; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示!"); } } #region device operation public void RunInitDut(NodeMes node) { ShowRichTextBox("Init Dut",true); byte I2CAddress = Convert.ToByte(node.NodeList[0].ChildNodes[3].InnerText, 16); double I2CClock = double.Parse(node.NodeList[0].ChildNodes[4].InnerText); celfras.CFS_CWQ1100_I2CInit(I2CAddress, I2CClock); celfras.CFS_CWQ1000_SetGPIO2Low(); celfras.CFS_CWQ1100_I2CInit(); celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x41); celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x42); celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x43); MessageBox.Show("Init_DUT success!", "提示!"); } private void RunInitTesterPWR(NodeMes node) { ShowRichTextBox("Init Tester PWR",true); celfras.CFS_PWR_Init(node.NodeList[0].ChildNodes[1].InnerText); // connect error celfras.CFS_PWR_SetVoltage(5.5); celfras.CFS_PWR_SetCurrent(2.0); celfras.CFS_PWR_OnOff(true); richTextBox1.Text += "\n Init Tester PWR success!"; } private void Run_Init_CFS_OSC(NodeMes node) { ShowRichTextBox("Init CFS OSC",true); celfras.CFS_OSC_Init(node.NodeList[0].ChildNodes[0].InnerText); } #region CWQ1100 TRIM_CFG OTP Write Test private void Run_BGR_Voltage_Calibration(NodeMes node) { ShowRichTextBox("BGR Voltage Calibration",true); if ("True".Equals(node.AttrColl[0].Value.ToString())) { int iReg = 0; bState = celfras.CFS_TM_BGRVoltageCalibration(1, ref iReg); if (bState == true) { richTextBox1.Text += "\n iReg = " + iReg; } else richTextBox1.Text += "\n Cannot find Value"; } } private void Run_OSC_Frequenty_Calibration(NodeMes node) { ShowRichTextBox("OSC Frequenty Calibration",true); if ("True".Equals(node.AttrColl[0].Value.ToString())) { int iReg2 = 0; bState = celfras.CFS_OSC_FreqCal_SendCmd(); if (bState == false) throw new Exception("CFS_OSC_FreqCal_SendCmd()"); bState = celfras.CFS_TM_OSCFrequencyCalibration(2, ref iReg2); if (bState == true) { richTextBox1.Text += "\n iReg = " + iReg2 ; } else richTextBox1.Text += "\n 测试失败error"; } } private void Run_VOUT_Calibration(NodeMes node) { ShowRichTextBox("VOUT Calibration",true); richTextBox1.Text += "\n 打开电子负载!"; if ("True".Equals(node.AttrColl[0].Value.ToString())) { //需要接上电子负载,设置1A int iReg29 = int.Parse(node.NodeList[0].ChildNodes[0].InnerText); int iReg2A = int.Parse(node.NodeList[0].ChildNodes[1].InnerText); //celfras.CFS_OSC_Init("USB0::0x2A8D::0x1766::MY57251817::0::INSTR"); //CFS_TM_VOUTVoltageCalibration celfras.CFS_OSC_VoutCal_SendCmd(); bState = celfras.CFS_TM_VOUTVoltageCalibration(3, ref iReg29, ref iReg2A); if (bState == true) { richTextBox1.AppendText("iReg29 = " + iReg29 + "\t" + "iReg2A = " + iReg2A + "\n"); } else richTextBox1.AppendText("测试失败error \n"); } } private void Run_TRIM_ADC_IOUT_Calibration(NodeMes node) { ShowRichTextBox(" TRIM ADC IOUT Calibration ",true); if ("True".Equals(node.AttrColl[0].Value.ToString())) { richTextBox1.AppendText("先调整电源为5.45V,使得示波器第4个通道测试值为5.4V\n"); celfras.CFS_TM_ChangeVRECT(4); int iReg28 = 0; //celfras.CFS_OSC_Init("USB0::0x2A8D::0x1766::MY57251817::0::INSTR"); celfras.CFS_OSC_ADCIOUTCal_SendCmd(); bState = celfras.CFS_TM_ADCIOUTCalibration(1, ref iReg28); if (bState == true) { richTextBox1.AppendText("iReg28 = " + iReg28 + "\n"); } } } private void Run_OTP_Write(NodeMes node) { ShowRichTextBox(" OTP Write ",true); if ("True".Equals(node.AttrColl[0].Value.ToString())) { richTextBox1.AppendText("先调整VPP电源为8V,进行写OTP操作\n"); celfras.CFS_TM_OTPWrite(); richTextBox1.AppendText("测试结束写log\n"); celfras.CFS_TM_RUNSummary(); celfras.CFS_Term(); } } #endregion #region function test private void Run_BGR_Voltage_Test() { ShowRichTextBox("BGR Voltage Test",true); celfras.CFS_BGR_VoltageTest(true); celfras.CFS_OSC_SendCmd(); double dVoltage = 0.0; celfras.CFS_OSC_GetVoltage(1, ref dVoltage); richTextBox1.AppendText("ANA_TEST = " + dVoltage + "\n"); celfras.CFS_BGR_VoltageTest(false); } private void Run_OSC_Frequency_Test() { ShowRichTextBox("OSC Frequency Test",true); celfras.CFS_OSC_FrequencyTest(true); double dFrequency = 0.0; celfras.CFS_OSC_GetFrequency(2, ref dFrequency); richTextBox1.AppendText("DIG_TEST = " + dFrequency + "\n"); celfras.CFS_OSC_FrequencyTest(false); } #endregion public void ShowRichTextBox(string str,Boolean IsMethodName) { if (IsMethodName) { appendText += "================== " + str + " ==================\n"; } else { appendText += str + "\n"; } richTextBox1.Text = appendText; } #endregion public class NodeMes { public string NodeName { get; set; } public string NodeText { get; set; } public XmlNodeList NodeList { get; set; } public XmlAttributeCollection AttrColl { get; set; } public NodeMes(string name, string text, XmlNodeList node, XmlAttributeCollection attrColl) { NodeName = name; NodeText = text; NodeList = node; AttrColl = attrColl; } } }
时间: 2024-10-12 11:48:59