串口调试程序,看界面布局

  

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