bug
lu
5 小时以前 6f0266b7737f7f6af97cb1ace5bb0521c9b0790c
DevApp/Gs.DevApp/DevFrm/Work/Frm_Work09.cs
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Timers;
using System.Windows.Forms;
using DevExpress.Utils;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
@@ -18,16 +22,9 @@
        /// </summary>
        private SerialPort Sp = new SerialPort();
        /// <summary>
        /// 更新UI委托,只定义,不实现
        /// 定时刷新
        /// </summary>
        /// <param name="text"></param>
        public delegate void HandleInterfaceUpdataDelegate(string text);
        /// <summary>
        /// 更新UI委托,实现
        /// </summary>
        private HandleInterfaceUpdataDelegate interfaceUpdataHandle;
        System.Timers.Timer timer = new System.Timers.Timer(10000); // 1000毫秒间隔
        System.Timers.Timer timer = new System.Timers.Timer(10000);
        public Frm_Work09()
        {
            InitializeComponent();
@@ -43,7 +40,31 @@
            };
            GetComList();
            this.comLine.getSuppler("");
            Gs.DevApp.ToolBox.UtilityHelper.SetGridViewParameterMx(gvMx1);
            gvMx1.OptionsFind.ShowSearchNavButtons = false;
            gvMx1.OptionsView.ShowAutoFilterRow = false;
            gvMx1.OptionsView.ShowGroupPanel = false;
            gvMx1.RowStyle += (s, e) =>
            {
                //默认选中行不变色
                gvMx1.OptionsSelection.EnableAppearanceFocusedRow = false;
                //默认选中单元格不变色
                gvMx1.OptionsSelection.EnableAppearanceFocusedCell = false;
                if (e.RowHandle >= 0)
                {
                    DataRow row = gvMx1.GetDataRow(e.RowHandle);
                    if (row == null)
                        return;
                    string colorString = row["bgColor"].ToString(); // 使用十六进制表示颜色的ARGB值
                    Color color = ColorTranslator.FromHtml(colorString);
                    e.Appearance.BackColor = color;
                }
            };
            gvMx1.IndicatorWidth = 60;
            gvMx1.CustomDrawRowIndicator += (s, e) =>
            {
                if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
            };
        }
        /// <summary> 
@@ -65,6 +86,7 @@
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            Sp.DataReceived -= new SerialDataReceivedEventHandler(Sp_DataReceived);
            string _comLine = comLine.GetId();
            string _cmbSerialPortNum = cmbSerialPortNum.Text.Trim();
            string _cmbBaudRate = cmbBaudRate.Text.Trim();
@@ -109,7 +131,6 @@
            }
            try
            {
                interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox);//实例化委托对象
                Sp.ReceivedBytesThreshold = 1;
                Sp.PortName = _cmbSerialPortNum;
                Sp.BaudRate = Convert.ToInt32(_cmbBaudRate);//设置波特率
@@ -130,6 +151,8 @@
                    Gs.DevApp.ToolBox.MsgHelper.ShowError("无法识别的停止位");
                }
                Sp.DataBits = int.Parse(_comSjw); // 设置数据位
                //Sp.ReadTimeout = 500; // 设置超时时间,例如500毫秒
                //Sp.ReadBufferSize = 4096; // 设置缓冲区大小
                Sp.DataReceived += new SerialDataReceivedEventHandler(Sp_DataReceived);
                Sp.ReceivedBytesThreshold = 1;
                LogHelper.Debug(this.ToString(), "打开串口通信PortName-" + Sp.PortName.ToString() + ",BaudRate-" + Sp.BaudRate.ToString() + ",StopBits-" + Sp.StopBits.ToString() + ",Parity-" + Sp.Parity.ToString());
@@ -155,15 +178,39 @@
        /// <param name="e"></param>
        public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string abc = string.Empty;
            string _tmpStr = string.Empty;
            try
            {
                int i = Sp.BytesToRead;
                if (i > 0)
                {
                    abc = Sp.ReadExisting();
                    LogHelper.Debug(this.ToString(), $"监听到数据:{abc}");
                    this.Invoke(interfaceUpdataHandle, abc);
                    _tmpStr = Sp.ReadExisting();
                   // Thread.Sleep(1000);
                    LogHelper.Debug(this.ToString(), $"监听到数据,字节长度:{i.ToString()},{_tmpStr}");
                    this.BeginInvoke(new Action(() =>
                    {
                        txtMsg.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff->字节数字" + i.ToString() + "->") + _tmpStr);
                        //ST,GS,+  250.2kg
                        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;
                        }
                        string[] _ary=_tmpStr.Split('+');
                        string abc = _ary.Last();//ST,GS,
                        txtNum.Text = (abc.Trim().Replace("k", "").Replace("g", "").Replace("S","").Replace("T","").Replace("G","").Replace(",",""));
                        addModel(txtNum.Text, _tmpStr);
                    }));
                }
            }
            catch (Exception ex)
@@ -173,33 +220,7 @@
            }
        }
        /// <summary>
        /// 接受到数据,开始处理数据
        /// </summary>
        /// <param name="text"></param>
        private void UpdateTextBox(string text)
        {
            //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>
        /// 启用或禁用
        /// </summary>
@@ -217,13 +238,16 @@
        /// <summary>
        /// 上传称重数据
        /// </summary>
        private void addModel(string _realWeight)
        /// <param name="_realWeight">截取后的数量</param>
        /// <param name="_realWeightTxt">原子串</param>
        private void addModel(string _realWeight, string _realWeightTxt)
        {
            string _lineId = comLine.GetId();
            var _obj = new
            {
                lineId = _lineId,
                realWeight = _realWeight,
                realWeight = _realWeight.Trim(),
                realWeightTxt = _realWeightTxt.Trim(),
            };
            try
            {
@@ -268,9 +292,9 @@
            }
            catch (Exception ex)
            {
                MessageBox.Show("gvMx1:" + ex.Message);
                Toast vm = new Toast(-1, "gvMx1:" + ex.Message);
                vm.Show();
            }
        }
    }
}