獲取串口數據,設置串口

Posted wangshaod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了獲取串口數據,設置串口相关的知识,希望对你有一定的参考价值。

        /// <summary>
        /// 提交结果
        /// </summary>
        /// <returns></returns>
        private void SubmitData()
        {
            string Port = this.txtCardReadPort.SkinTxt.Text.Trim();
            string Torque = this.txtNiuli.SkinTxt.Text.Trim();//扭力
            string path = Application.StartupPath + "setup.ini";//保存地址

            FileInfo file = new FileInfo(path);
            if (file.Exists == false)//如果文件不存在,就創建文件
            {
                File.AppendAllText(path, "[IDCard]
", Encoding.Default);
                File.AppendAllText(path, "CARD_COMPORT=1
");
             
                File.AppendAllText(path, "[IDTorque]
", Encoding.Default);
                File.AppendAllText(path, "CARD_COMTorque=1
");
            }
            try
            {
                this._iniManager.Write("IDCard", "CARD_COMPORT", Port);
                this._iniManager.Write("IDTorque", "CARD_COMTorque", Torque);
                MessageBox.Show(LanguageHelper.rm.GetString("Common_SuccessfulOperation"));
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

  

技术图片

         //全局串口事件
       
private SerialPort comm = new SerialPort();

//獲取串口設置的端口號,並且打開串口 private bool CardReaderEvent() { try { //獲取串口號 string cardComport = iniManager.ReadValue("IDCard", "CARD_COMPORT");//COM4 //串口號轉為數字 int port = int.Parse(cardComport); //打開串口
                    comm.PortName = "COM" + com;
                    comm.BaudRate = 19200;
                   comm.DataBits = 8;
                    comm.StopBits = StopBits.One;
                    comm.Parity = Parity.None;
                  comm.WriteTimeout = SerialPort.InfiniteTimeout;
                    comm.ReadTimeout = SerialPort.InfiniteTimeout;
                    comm.Handshake = Handshake.None;
                    comm.RtsEnable = true;
                   comm.DtrEnable = true;
              if (comm.IsOpen)
                        {
                            comm.Close();//關閉串口
                        }
                   comm.Open();
                    comm.DataReceived += new SerialDataReceivedEventHandler(comm_SerialPort_ReceiveData); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return false; } } //實時獲取打卡數據 private void comm_SerialPort_ReceiveData(object sender, UUTSystem_Global.Common.ReceiveDataEventArgs e) { if (e != null) { //e.CommDatab表示獲取的串口返回數據 string cardId = e.CommData; if (!string.IsNullOrEmpty(cardId)) { if (cardId != "") { //去除串口返回數據的空格和換行符 cardId = cardId.Replace(" ", ""); cardId = cardId.Replace(" ", ""); //由于是打卡所以只截取十位卡号 string value = cardId.Substring(cardId.Length - 10); //使用委托才能修改页面UI this.SetTxtIdCardNum(value); } } } } //關閉串口 private void CommClose() {           if(comm.IsOpen) {
                            comm.Close();//關閉串口
                        } }

  

以上是关于獲取串口數據,設置串口的主要内容,如果未能解决你的问题,请参考以下文章

獲取DataGrid數據

Flutter dio獲取數據顯示到listview中

MVC+Ninject+三层架构+代码生成 -- 总结(數據層)

獲取12306所需剩餘的票

linux串口编程(termios)相关的使用问题

Python readlines Api从串口访问时需要很长时间