lu
2025-05-21 c1a18e87764130bd4f2b5d7ed0abff5d88ae0f6a
DevApp/Gs.DevApp/DevFrm/Work/Frm_Work09.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO.Ports;
using System.Timers;
using System.Windows.Forms;
@@ -8,12 +7,12 @@
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
namespace Gs.DevApp.DevFrm.Work
{
    public partial class Frm_Work09 : DevExpress.XtraEditors.XtraForm
    {
        string _webServiceName = "WorkWeight/";
        string value = string.Empty;
        /// <summary>
        /// 串口类
        /// </summary>
@@ -28,12 +27,20 @@
        /// </summary>
        private HandleInterfaceUpdataDelegate interfaceUpdataHandle;
        System.Timers.Timer timer = new System.Timers.Timer(10000); // 1000毫秒间隔
        public Frm_Work09()
        {
            InitializeComponent();
            this.FormClosing += (s, e) =>
            {
                if (Sp.IsOpen)
                    Sp.Close();
                if (timer != null)
                {
                    timer.Stop();
                    timer.Dispose();
                }
            };
            GetComList();
            this.comLine.getSuppler("");
            Gs.DevApp.ToolBox.UtilityHelper.SetGridViewParameterMx(gvMx1);
@@ -51,7 +58,6 @@
                cmbSerialPortNum.Properties.Items.Add(port);
            }
        }
        /// <summary>
        /// 启动事件
        /// </summary>
@@ -60,11 +66,11 @@
        private void btnStart_Click(object sender, EventArgs e)
        {
            string _comLine = comLine.GetId();
            string _cmbSerialPortNum = cmbSerialPortNum.SelectedText.Trim();
            string _cmbBaudRate = cmbBaudRate.SelectedText.Trim();
            string _comSjw = comSjw.SelectedText.Trim();
            string _comStopBits = comStopBits.SelectedText.Trim();
            string _comParity = comParity.SelectedText.Trim();
            string _cmbSerialPortNum = cmbSerialPortNum.Text.Trim();
            string _cmbBaudRate = cmbBaudRate.Text.Trim();
            string _comSjw = comSjw.Text.Trim();
            string _comStopBits = comStopBits.Text.Trim();
            string _comParity = comParity.Text.Trim();
            if (string.IsNullOrEmpty(_comLine))
            {
                Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择线体");
@@ -91,7 +97,7 @@
            }
            if (comStopBits.SelectedIndex < 0)
            {
                Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择数停止位");
                Gs.DevApp.ToolBox.MsgHelper.ShowError("请选择停止位");
                this.comStopBits.Focus();
                return;
            }
@@ -104,42 +110,35 @@
            try
            {
                interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox);//实例化委托对象 
                Sp.RtsEnable = true;
                Sp.DtrEnable = true;
                Sp.ReceivedBytesThreshold = 1;
                Sp.PortName = this.cmbSerialPortNum.Text.Trim();
                Sp.BaudRate = Convert.ToInt32(this.cmbBaudRate.Text.Trim());
                // Sp.Parity = Parity.Even;
                Sp.PortName = _cmbSerialPortNum;
                Sp.BaudRate = Convert.ToInt32(_cmbBaudRate);//设置波特率
                if (Enum.TryParse(_comParity, out Parity pt1))
                {
                    Sp.Parity = pt1;
                    Sp.Parity = pt1;// 设置校验位
                }
                else
                {
                    Gs.DevApp.ToolBox.MsgHelper.ShowError("无法识别的校验");
                }
                // Sp.StopBits = StopBits.One;
                if (Enum.TryParse(_comStopBits, out StopBits pt2))
                {
                    Sp.StopBits = pt2;
                    Sp.StopBits = pt2;//设置停止位
                }
                else
                {
                    Gs.DevApp.ToolBox.MsgHelper.ShowError("无法识别的停止位");
                }
                Sp.DataBits = int.Parse(_comSjw); // 设置数据位
                Sp.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived);
                Sp.ReceivedBytesThreshold = 1;
                LogHelper.Debug(this.ToString(), "打开串口通信");
                LogHelper.Debug(this.ToString(), "打开串口通信PortName-" + Sp.PortName.ToString() + ",BaudRate-" + Sp.BaudRate.ToString() + ",StopBits-" + Sp.StopBits.ToString() + ",Parity-" + Sp.Parity.ToString());
                if (!Sp.IsOpen)
                    Sp.Open();
                setEnable(false);
                //timer1.Enabled = false;
                // LogHelper.Debug(this.ToString(), "启动刷新数据定时器");
                // timer1.Start();
                timer.Elapsed += OnTimedEvent;
                timer.AutoReset = true; // 设置为true表示重复执行,false表示执行一次后停止
                timer.Enabled = true; // 开始计时
                Gs.DevApp.ToolBox.MsgHelper.ShowInformation("打开成功!");
            }
            catch (Exception exe)
@@ -150,36 +149,26 @@
        }
        /// <summary>
        /// 串收到数据
        /// 串口收到数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //string start = "\nwn";
            string abc = string.Empty;
            string strTemp = "";
            try
            {
                LogHelper.Debug(this.ToString(), $"1监听到数据开始: ");
                int i = Sp.BytesToRead;
                LogHelper.Debug(this.ToString(), $"2监听到字节数:{i}");
                if (i > 0)
                {
                    abc = Sp.ReadExisting();
                    LogHelper.Debug(this.ToString(), $"3监听到数据:{abc}");
                    //strTemp = abc.Substring(7, 7);
                    //LogHelper.Debug(this.ToString(), :截取数据:{strTemp}");
                    //value = Math.Round(float.Parse(strTemp), 2).ToString();
                    //LogHelper.Debug(this.ToString(), :转换数据:{value}");
                    //this.Invoke(interfaceUpdataHandle, value);
                    LogHelper.Debug(this.ToString(), $"监听到数据:{abc}");
                    this.Invoke(interfaceUpdataHandle, abc);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Debug(this.ToString(), ex.Message);
                Gs.DevApp.ToolBox.MsgHelper.ShowError("收到数据:" + ex.Message);
                LogHelper.Debug(this.ToString(), $"监听到数据error:{ex.Message}");
                return;
            }
        }
@@ -190,8 +179,26 @@
        /// <param name="text"></param>
        private void UpdateTextBox(string text)
        {
            txtNum.Text = text;
            addModel();
            //ST,GS,+  250.2kg
            string _tmpStr = text.Trim();
            if (_tmpStr.Length <= 0)
            {
                LogHelper.Debug(this.ToString(), "_tmpStr长度<=0,退出上报");
                return;
            }
            if (!_tmpStr.Contains("ST"))
            {
                LogHelper.Debug(this.ToString(), "_tmpStr重量状态不为ST,退出上报");
                return;
            }
            if (!_tmpStr.Contains("+"))
            {
                LogHelper.Debug(this.ToString(), "_tmpStr重量+,退出上报");
                return;
            }
            _tmpStr = _tmpStr.Substring(7);
            txtNum.Text = _tmpStr;
            addModel(_tmpStr);
        }
        /// <summary>
        /// 启用或禁用
@@ -210,10 +217,9 @@
        /// <summary>
        /// 上传称重数据
        /// </summary>
        private void addModel()
        private void addModel(string _realWeight)
        {
            string _lineId = comLine.GetId();
            string _realWeight = txtNum.Text.Trim();
            var _obj = new
            {
                lineId = _lineId,
@@ -235,7 +241,12 @@
            }
        }
        private  void OnTimedEvent(Object source, ElapsedEventArgs e)
        /// <summary>
        /// 定时读取列表
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            var _obj = new
            {
@@ -244,11 +255,11 @@
            try
            {
                var lst = new List<string>();
                var strReturn = UtilityHelper.HttpPost("", _webServiceName + "GetModelList", JsonConvert.SerializeObject(_obj),false);
                var strReturn = UtilityHelper.HttpPost("", _webServiceName + "GetModelList", JsonConvert.SerializeObject(_obj), false);
                var dd = UtilityHelper.ReturnToList(strReturn);
                var dt = dd.rtnData;
                BeginInvoke(new Action(() => {
                    // txtNum.Text = DateTime.Now.ToString();
                BeginInvoke(new Action(() =>
                {
                    gcMx1.BindingContext = new BindingContext();
                    gcMx1.DataSource = dt;
                    gcMx1.ForceInitialize();
@@ -257,9 +268,9 @@
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("gvMx1:" + ex.Message);
            }
        }
    }
}