C#读取电子称串口数据,显示在TextBox控件 1C

已经能显示出来数据,但是我应该如何提取数据到textbox中,与电子秤上的重量同步显示图片说明

3个回答

 using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace weight
{
    /// <summary>
    /// 电子秤接口信息类,封装COM口数据
    /// </summary>
    public class WeightInformation
    {
        string _wdata;
        string _wunit;
        string _qdata;
        string _qunit;
        string _percentage;
        /// <summary>
        /// 获取或设置重量
        /// </summary>
        public string WData { get { return this._wdata; } set { this._wdata = value; } }
        /// <summary>
        /// 获取或设置重量单位
        /// </summary>
        public string WUnit { get { return this._wunit; } set { this._wunit = value; } }
        /// <summary>
        /// 获取或设置数量
        /// </summary>
        public string QData { get { return this._qdata; } set { this._qdata = value; } }
        /// <summary>
        /// 获取或设置数量单位
        /// </summary>
        public string QUnit { get { return this._qunit; } set { this._qunit = value; } }
        /// <summary>
        /// 获取或设置百分数
        /// </summary>
        public string Percentage { get { return this._percentage; } set { this._percentage = value; } }
    }
    /// <summary>
    /// 电子称数据读取类
    /// </summary>
    public class WeightReader : IDisposable
    {
        #region 字段、属性与构造函数
        SerialPort sp;
        int _speed = 300;
        /// <summary>
        /// 获取或设置电脑取COM数据缓冲时间,单位毫秒
        /// </summary>
        public int Speed
        {
            get
            {
                return this._speed;
            }
            set
            {
                if (value < 300)
                    throw new Exception("串口读取缓冲时间不能小于300毫秒!");
                this._speed = value;
            }
        }
        public bool InitCom(string PortName)
        {
            return this.InitCom(PortName, 4800, 300);
        }
        /// <summary>
        /// 初始化串口
        /// </summary>
        /// <param name="PortName">数据传输端口</param>
        /// <param name="BaudRate">波特率</param>
        /// <param name="Speed">串口读数缓冲时间</param>
        /// <returns></returns>
        public bool InitCom(string PortName, int BaudRate,int Speed) 
        {
            try
            {
                sp = new SerialPort(PortName, BaudRate, Parity.None, 8);
                sp.ReceivedBytesThreshold = 10;
                sp.Handshake = Handshake.RequestToSend;
                sp.Parity = Parity.None;
                sp.ReadTimeout = 600;
                sp.WriteTimeout = 600;
                this.Speed = Speed;
                if (!sp.IsOpen)
                {
                    sp.Open();
                }
                return true;
            }
            catch
            {
                throw new Exception(string.Format("无法初始化串口{0}!",PortName));
            }
        }
        #endregion
        #region 串口数据读取方法
        public WeightInformation ReadInfo()
        {
            string src = this.ReadCom();
            WeightInformation info = new WeightInformation();
            info.WData = this.DecodeWeightData(src);
            info.WUnit = this.DecodeWeightUnit(src);
            info.Percentage = this.DecodePercentage(src);
            info.QData = this.DecodeQualityData(src);
            info.QUnit = this.DecodeQualityUnit(src);
            return info;
        }
        /// <summary>
        /// 将COM口缓存数据全部读取
        /// </summary>
        /// <returns></returns>
        private string ReadCom()//返回信息 
        {
            if (this.sp.IsOpen)
            {
                Thread.Sleep(this._speed);
                string res = "";
                //for (int i = 0; i < 5; i++)
                //{
                byte[] buffer = new byte[sp.BytesToRead];
                sp.Read(buffer, 0, buffer.Length);
                res = System.Text.Encoding.ASCII.GetString(buffer);
                //if (res != "")
                //    break;
                //}
                if (res == "")
                {
                    throw new Exception("串口读取数据为空,参数设置是否正确!");
                }
                return res;
            }
            return "";
        }
        #endregion
        #region  基本取数方法
        /// <summary>
        /// 从字符串中取值
        /// </summary>
        /// <param name="head">起始字符串</param>
        /// <param name="intervalLen">间隔位长度</param>
        /// <param name="valueLen">值长度</param>
        /// <param name="src">源字符串</param>
        /// <returns></returns>
        private string DecodeValue(string head, int intervalLen, int valueLen,string src)
        {
            int index = src.IndexOf(head);
            return src.Substring(index + intervalLen, valueLen);
        }
        /// <summary>
        /// 取重量
        /// </summary>
        /// <param name="srcString">源字符串</param>
        /// <returns></returns>
        private string DecodeWeightData(string srcString)
        {
            return this.DecodeValue("GS,", 3, 8,srcString);
        }
        /// <summary>
        /// 取重量单位
        /// </summary>
        /// <param name="srcString">源字符串</param>
        /// <returns></returns>
        private string DecodeWeightUnit(string srcString)
        {
            return this.DecodeValue("GS,", 12, 2, srcString);
        }
        /// <summary>
        /// 取百分数
        /// </summary>
        /// <param name="srcString">源字符串</param>
        /// <returns></returns>
        private string DecodePercentage(string srcString)
        {
            return this.DecodeValue("U.W.", 4, 14, srcString);
        }
        /// <summary>
        /// 取数量
        /// </summary>
        /// <param name="srcString">源字符串</param>
        /// <returns></returns>
        private string DecodeQualityData(string srcString)
        {
            return this.DecodeValue("PCS", 3, 9, srcString);            
        }

        /// <summary>
        /// 取数量单位
        /// </summary>
        /// <param name="srcString">源字符串</param>
        /// <returns></returns>
        private string DecodeQualityUnit(string srcString)
        {
            return this.DecodeValue("PCS", 12, 3, srcString);            
        }
        #endregion
        #region 释放所有资源
        public void Dispose()
        {
            if (sp != null && sp.IsOpen)
            {
                sp.Close();
            }
        }
        #endregion
    }
}

调用ReadInfo,用TextBox1.Text = weight.ReadInfo().WData.ToString();

你右边的就是文本框。只是看你的代码,你每次更新都把数字追加进去了,你要像电子秤,就不要追加,而是直接设置text属性

楼主目前的情况大致是:

TextBox1.Text += DataStr;

改为:

TextBox1.Text = DataStr;

理论上就对了。
如果本回答对您有所帮助,敬请采纳,谢谢题主!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐