码迷,mamicode.com
首页 > 其他好文 > 详细

添加电子称程序

时间:2015-04-24 16:05:41      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

定义一个委托:

delegate void ElectronicScales(string str);
ElectronicScales strc;


在初始化电子称:

       #region 电子称设置
        public void Electronic()
        {
            //初始化窗口程序接受电子秤信息
            try
            {
                serialPort1.PortName = "COM3";
                switch (Share.GetCompanyId())
                {
                    case 1:
                        serialPort1.BaudRate = 1200;
                        break;
                    default:
                        serialPort1.BaudRate = 2400;
                        break;
                }

                strc = new ElectronicScales(UpdateTextBox);
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Close();

                serialPort1.Open();
            }
            catch
            {
            }
        }

        private void UpdateTextBox(string weight)
        {
            TrueWeight = int.Parse(weight);   //TrueWeight界面文本框
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                var strweight = "";
                Thread.Sleep(200);

                string com = serialPort1.ReadExisting();

                char[] ch = com.Substring(com.IndexOf("=") + 1, 6).ToCharArray();
                Array.Reverse(ch);
                strweight = Convert.ToInt32(new string(ch)).ToString();

                this.Invoke(strc, new string[] { strweight });
            }
            catch
            {

            }
        }

        #endregion

COM3是端口号,需要把带你呢的端口号设置成COM3

 

添加电子称程序

标签:

原文地址:http://www.cnblogs.com/xiaoqi742709106/p/4453599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!