博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
串口调试程序,看界面布局
阅读量:6604 次
发布时间:2019-06-24

本文共 5177 字,大约阅读时间需要 17 分钟。

 

  

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO.Ports;using System.Threading;using System.IO;namespace SP{    public partial class mainForm : Form    {        static bool _isOpen=false;//判断串口是否打开        static bool _isPostBack = false;        static SerialPort _serialPort;        Thread readThread;        FileStream gpsFile;        StreamWriter sw;        public mainForm()        {            InitializeComponent();        }        private void mainForm_Load(object sender, EventArgs e)        {            string[] Ports=SerialPort.GetPortNames();            Array.Sort(Ports);            this.comboBoxSP_num.Items.AddRange(Ports);            this.comboBoxSP_num.SelectedIndex=this.comboBoxSP_num.Items.Count>0?0:-1;            this.comboBoxSP_baudrate.SelectedIndex=this.comboBoxSP_baudrate.Items.IndexOf("9600");            this.comboBoxSP_parity.SelectedIndex=this.comboBoxSP_parity.Items.IndexOf("None");            this.comboBoxSP_databit.SelectedIndex=this.comboBoxSP_databit.Items.IndexOf("8");            this.comboBoxSP_stopbit.SelectedIndex=this.comboBoxSP_stopbit.Items.IndexOf("1");                    _serialPort = new SerialPort();            readThread = new Thread(Read);            gpsFile=new FileStream(@"E:\position.txt",FileMode.Append);            sw = new StreamWriter(gpsFile);        }        //打开或关闭串口        private void buttonOpen_Click(object sender, EventArgs e)        {            try            {                if(_serialPort.IsOpen==false)                {                    setSP();                                        _serialPort.Open();                    _isOpen = true;                    if (readThread.ThreadState == ThreadState.Suspended)                                            readThread.Resume();                                        else                             readThread.Start();                    this.comboBoxSP_num.Enabled = false;                    this.comboBoxSP_baudrate.Enabled = false;                    this.comboBoxSP_databit.Enabled = false;                    this.comboBoxSP_parity.Enabled = false;                    this.comboBoxSP_stopbit.Enabled = false;                    buttonOpen.Text = "关闭串口";                    labelStatus.Text = "串口正处于打开状态! " + label1.Text + ":" + comboBoxSP_num.Text + ", " + label2.Text + ":" + comboBoxSP_baudrate.Text + ", " + label3.Text + ":" + comboBoxSP_parity.Text + ", " + label4.Text + ":" + comboBoxSP_databit.Text + ", " + label5.Text + ":" + comboBoxSP_stopbit.Text;                }                else if(_serialPort.IsOpen==true)                {                    _isOpen = false;                    //_isPostBack = true;                    readThread.Suspend();                    _serialPort.Close();                    this.comboBoxSP_num.Enabled = true;                    this.comboBoxSP_baudrate.Enabled = true;                    this.comboBoxSP_databit.Enabled = true;                    this.comboBoxSP_parity.Enabled = true;                    this.comboBoxSP_stopbit.Enabled = true;                    buttonOpen.Text = "打开串口";                    labelStatus.Text = "串口正处于关闭状态!";                }            }            catch(Exception EX)            {                MessageBox.Show(EX.Message);            }        }        //接收串口数据,并保存在一个".txt"文档中        public void Read()        {            string receiveMsg;            while (_isOpen)            {                try                {                    receiveMsg = _serialPort.ReadLine();                    sw.WriteLine(receiveMsg);                    textBoxReceive.AppendText(receiveMsg);                }                catch (TimeoutException e)                { }                catch (InvalidOperationException e)                { }                catch (System.IO.IOException e)                { }            }        }        private void setSP()        {            _serialPort.PortName = (string)this.comboBoxSP_num.SelectedItem;            _serialPort.BaudRate = int.Parse(this.comboBoxSP_baudrate.SelectedItem.ToString());            //用此方法将string对象转换为属性集合元素(对象)时,要求与原属性元素(Parity的集合元素)大小写一致            _serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), this.comboBoxSP_parity.SelectedItem.ToString());            _serialPort.DataBits = int.Parse(this.comboBoxSP_databit.SelectedItem.ToString());            _serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), this.comboBoxSP_stopbit.SelectedItem.ToString());        }        //窗口关闭时回收资源        private void mainForm_FormClosing(object sender, FormClosingEventArgs e)        {            if (readThread.ThreadState == ThreadState.Suspended)            {                readThread.Resume();                readThread.Abort();                                _serialPort.Close();                GC.Collect();            }            else            {                readThread.Abort();                                _serialPort.Close();                GC.Collect();            }        }             }}

 

转载于:https://www.cnblogs.com/rechen/p/5528781.html

你可能感兴趣的文章
9.02-Spring IOC 容器中Bean的生命周期
查看>>
6.6 tar打包
查看>>
微信自动抢红包的实现(Demo已增加查看TopActivity功能)
查看>>
Spring MVC核心技术
查看>>
TCP协议如何保证传输的可靠性
查看>>
Spring Cloud云架构 - SSO单点登录之OAuth2.0 登出流程(3)
查看>>
建站心得之discuz门户程序相比ZBLOG具有哪些优势[图]
查看>>
编程之美 测试赛 石头剪刀布
查看>>
签名问题
查看>>
软件开发各阶段交付物列表
查看>>
2018-05-24 Linux学习
查看>>
ntp服务器的搭建
查看>>
我的友情链接
查看>>
sysstat 安装
查看>>
《你必须知道的.NET》 - 书摘精要
查看>>
六、nginx搭建织梦DedeCms网站
查看>>
Tair学习小记
查看>>
网卡绑定(服务器&&交换机),缓存服务器Squid架构配置
查看>>
web网站加速之CDN(Content Delivery Network)技术原理
查看>>
打算写一款框架来提高自己 写个结构吧
查看>>