bug
lu
9 天以前 5ba3024752708ecbc2a84bd73f1f79d4bd86b994
DevApp/Gs.DevApp/XtraForm1.cs
@@ -1,17 +1,10 @@
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NModbus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Gs.DevApp
{
@@ -20,67 +13,107 @@
        public XtraForm1()
        {
            InitializeComponent();
            tlMenu.ContextMenuStrip = contextMenuStrip1;
            getTree();
        }
        /// <summary>
        ///     读取列表
        /// 水检
        /// </summary>
        private void getTree()
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            var pgq = new PageQueryModel(1, 999999, "a.type_memo");
            var json = JsonConvert.SerializeObject(pgq);
            try
            string strCsl1 = "";
            string strSw1 = "";
            string strCsl2 = "";
            string strSw2 = "";
            string strCsl3 = "";
            string strSw3 = "";
            string strCsl4 = "";
            string strSw4 = "";
            string strCsl5 = "";
            string strSw5 = "";
            string SJJG = "";
            try//水检取数,后面补充
            {
                var strReturn =
                    UtilityHelper.HttpPost("", "MesDefectTypeManager/GetListPage", json);
                var dd = UtilityHelper.ReturnToTablePage(strReturn);
                var dt = dd.rtnData.list;
                tlMenu.DataSource = dt;
                tlMenu.KeyFieldName = "guid";
                tlMenu.ParentFieldName = "upGuid";
                tlMenu.Tag = "typeMemo";
                tlMenu.EndUpdate();
                this.tlMenu.CollapseAll();
                tlMenu.OptionsBehavior.Editable = true;
                tlMenu.OptionsBehavior.AllowRecursiveNodeChecking = false;
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                string hostname = "192.168.60.51";
                ModbusFactory modbusFactory = new ModbusFactory();
                IModbusMaster master = modbusFactory.CreateMaster(new TcpClient(hostname, 502));
                master.Transport.ReadTimeout = 10000;
                master.Transport.Retries = 10000;
                DataTable dt = new DataTable();
                //参数(分别为从站地址,起始地址,长度)
                byte slaveAddress = byte.Parse(1.ToString());
                ushort numberOfPoints = ushort.Parse("1");
                float fsw = 0;
                DataTable dt1 = new DataTable();
                ushort[] csl1 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(32.ToString()), numberOfPoints);
                strCsl1 = csl1[0].ToString();
                ushort[] sw1 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(31.ToString()), numberOfPoints);
                strSw1 = (float.Parse(sw1[0].ToString()) / 10).ToString();
                ushort[] csl2 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(34.ToString()), numberOfPoints);
                strCsl2 = csl2[0].ToString();
                ushort[] sw2 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(33.ToString()), numberOfPoints);
                strSw2 = (float.Parse(sw2[0].ToString()) / 10).ToString();
                ushort[] csl3 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(36.ToString()), numberOfPoints);
                strCsl3 = csl3[0].ToString();
                ushort[] sw3 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(35.ToString()), numberOfPoints);
                strSw3 = (float.Parse(sw3[0].ToString()) / 10).ToString();
                ushort[] csl4 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(38.ToString()), numberOfPoints);
                strCsl4 = csl4[0].ToString();
                ushort[] sw4 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(37.ToString()), numberOfPoints);
                strSw4 = (float.Parse(sw4[0].ToString()) / 10).ToString();
                ushort[] csl5 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(40.ToString()), numberOfPoints);
                strCsl5 = csl5[0].ToString();
                ushort[] sw5 = master.ReadHoldingRegisters(slaveAddress, ushort.Parse(39.ToString()), numberOfPoints);
                strSw5 = (float.Parse(sw5[0].ToString()) / 10).ToString();
                bool[] sjjg = master.ReadCoils(slaveAddress, ushort.Parse(77.ToString()), numberOfPoints);
                SJJG = sjjg[0].ToString();
                ///////////////////
                ///
                // 3. 读取从站设备的寄存器
                ushort startAddress = 0;   // 寄存器起始地址
                ushort numRegisters = 125;  // 读取 10 个寄存器
                try
                {
                    // 读取保持寄存器(从站地址为 1)
                    ushort[] registers = master.ReadHoldingRegisters(1, startAddress, numRegisters);
                    // 输出结果
                    Console.WriteLine("读取到的寄存器值:");
                    foreach (var register in registers)
                    {
                        Console.WriteLine(register);
                        stringBuilder.Append(register.ToString());
                    }
                    richTextBox1.Text = stringBuilder.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"读取错误: {ex.Message}");
                }
            }
            catch (Exception ex)
            {
                MsgHelper.Warning("提示:" + ex.Message);
                //VisiblePanel(txtDQBarCode.EditValue.ToString(), ex.Message, "NG", Color.Yellow);
                //txtGWM.EditValue = "";
                return;
            }
        }
        private void tlMenu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var hitInfo = tlMenu.CalcHitInfo(new Point(e.X, e.Y));
                if (hitInfo.HitInfoType == HitInfoType.Cell)
                {
                    tlMenu.FocusedNode = hitInfo.Node;
                   contextMenuStrip1.Show(tlMenu, e.Location);
                }
            }
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (tlMenu.FocusedNode != null)
            {
                // 删除节点的逻辑
                tlMenu.DeleteNode(tlMenu.FocusedNode);
            }
        }
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (tlMenu.FocusedNode != null)
            {
                // 修改节点的逻辑,例如更改节点值
                tlMenu.FocusedNode.SetValue("列名", "新值"); // 替换"列名"和"新值"为实际列名和值
            }
        }
    }
}