溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#怎么實現(xiàn)連接電子秤串口自動稱重

發(fā)布時間:2022-04-27 14:36:09 來源:億速云 閱讀:249 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“C#怎么實現(xiàn)連接電子秤串口自動稱重”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C#怎么實現(xiàn)連接電子秤串口自動稱重”吧!

一、界面設計

C#怎么實現(xiàn)連接電子秤串口自動稱重

二、核心代碼

        private SerialPort com;
        private void FormCheck_Load(object sender, EventArgs e)
        {
            //初始化COMList
            initCOMList();     
        }

        #region
        private void initCOMList()
        {
            ManageCOM com = new ManageCOM();
            List<string> s = com.AutoGetCOM();

            string[] comNames = s.ToArray();
            comboxCOM.Items.Clear();
            comboxCOM.Items.Add("請選擇COM");
            comboxCOM.SelectedIndex = 0;
            foreach (string portName in comNames)
            {
                //添加COM口到界面下拉框
                comboxCOM.Items.Add(portName);
            }
        }
        #endregion

        private void btnStart_Click(object sender, EventArgs e)
        {

            getWeightfromCOM();
        }

        #region
        private void getWeightfromCOM()
        {
            int selectIndex= comboxCOM.SelectedIndex;

            if (selectIndex!=0)
            {
                string COM = comboxCOM.Items[selectIndex].ToString();

                com = new SerialPort(COM);       //實例化SerialPort并設置COM口
                com.BaudRate = 9600;//波特率
                com.Parity = Parity.None;//無奇偶校驗位
                com.StopBits = StopBits.Two;//兩個停止位
                com.Handshake = Handshake.RequestToSend;//控制協(xié)議
                com.ReceivedBytesThreshold = 13;//設置 DataReceived 事件發(fā)生前內(nèi)部輸入緩沖區(qū)中的字節(jié)數(shù),我這里是13字節(jié)為一組
                com.Open();                 //打開串口  
                com.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);           //接受數(shù)據(jù)線程
            }
            else
            {
                MessageBox.Show("請選擇COM");
            }
            
        }

        /// <summary>
        /// 監(jiān)聽串口數(shù)據(jù)線程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);//線程休眠500毫秒,方便接收串口的全部數(shù)據(jù)
            try
            {
                if (com.IsOpen)
                {
                    byte[] readBuffer = new byte[com.ReadBufferSize + 1];
                    try
                    {
                        int count = com.Read(readBuffer, 0, com.ReadBufferSize);        //讀取串口數(shù)據(jù)(監(jiān)聽)
                        String SerialIn = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count);//將字節(jié)數(shù)組解碼為字符串
                        if (count != 0)
                        {
                            //這里強調(diào)一下,線程里不可以直接對UI進行賦值,只能使用委托操作控件
                            this.BeginInvoke(new System.Threading.ThreadStart(delegate ()
                            {
                                rbResult.Text = SerialIn;
                            }));

                        }
                    }
                    catch (TimeoutException) { }
                }
                else
                {
                    TimeSpan waitTime = new TimeSpan(0, 0, 0, 0, 50);
                    Thread.Sleep(waitTime);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion

到此,相信大家對“C#怎么實現(xiàn)連接電子秤串口自動稱重”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI