示例#1
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (serial == null)
                {
                    try
                    {
                        string portName = this.cmbPort.Text;

                        SerialPortBaudRates baud =
                            (SerialPortBaudRates)Enum.Parse(typeof(SerialPortBaudRates), cmbBaudRate.Text);
                        SerialPortDatabit databit =
                            (SerialPortDatabit)Enum.Parse(typeof(SerialPortDatabit), cmbDataBit.Text);
                        Parity   parity   = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
                        StopBits stopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBit.Text);

                        //使用枚举构造函数
                        //serial = new SerialPortUtil(portName, baud, parity, databit, stopBits);

                        //使用字符串构造函数
                        serial = new SerialPortUtil(portName, cmbBaudRate.Text, cmbParity.Text, cmbDataBit.Text, cmbStopBit.Text);
                        serial.DataReceived += serial_DataReceived;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        serial = null;
                        return;
                    }
                }

                if (!isOpen)
                {
                    //打开串口通讯
                    serial.OpenPort();
                    btnConnect.Text = "断开";

                    //m_receiveT = new Thread(Receive);
                    //m_receiveT.IsBackground = true;
                    //m_receiveT.Start();
                }
                else
                {
                    serial.ClosePort();
                    //m_receiveT.Abort();
                    btnConnect.Text = "连接";
                }

                isOpen = !isOpen;
                this.btnSend.Enabled = isOpen;
                this.labState.Text   = isOpen ? "已连接" : "未连接";
            }
            catch (Exception ex)
            {
                this.labState.Text = ex.Message;
                MessageBox.Show(ex.Message);
            }
        }
示例#2
0
        /// <summary>
        /// 参数构造函数(使用字符串参数构造)
        /// </summary>
        /// <param name="name">串口号</param>
        /// <param name="baud">波特率</param>
        /// <param name="parity">奇偶校验位</param>
        /// <param name="databit">数据位</param>
        /// <param name="stopbit">停止位</param>
        public SerialPortUtil(string name, string baud, string parity, string databit, string stopbit)
        {
            _portName = name;
            _baudRate = (SerialPortBaudRates)Enum.Parse(typeof(SerialPortBaudRates), baud);
            _parity   = (Parity)Enum.Parse(typeof(Parity), parity);
            _dataBits = (SerialPortDatabit)Enum.Parse(typeof(SerialPortDatabit), databit);
            _stopBits = (StopBits)Enum.Parse(typeof(StopBits), stopbit);

            _comPort.DataReceived  += new SerialDataReceivedEventHandler(comPort_DataReceived);
            _comPort.ErrorReceived += new SerialErrorReceivedEventHandler(comPort_ErrorReceived);
        }
示例#3
0
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SerialPortUtil()
        {
            _portName = "COM1";
            _baudRate = SerialPortBaudRates.BaudRate_9600;
            _parity   = Parity.None;
            _dataBits = SerialPortDatabit.EightBits;
            _stopBits = StopBits.One;

            _comPort.DataReceived  += new SerialDataReceivedEventHandler(comPort_DataReceived);
            _comPort.ErrorReceived += new SerialErrorReceivedEventHandler(comPort_ErrorReceived);
        }
示例#4
0
        /// <summary>
        /// 参数构造函数(使用枚举参数构造)
        /// </summary>
        /// <param name="name">串口号</param>
        /// <param name="baud">波特率</param>
        /// <param name="parity">奇偶校验位</param>
        /// <param name="databit">数据位</param>
        /// <param name="stopBits">停止位</param>
        public SerialPortUtil(string name, SerialPortBaudRates baud, Parity parity, SerialPortDatabit databit, StopBits stopBits)
        {
            _portName = name;
            _baudRate = baud;
            _parity   = parity;
            _dataBits = databit;
            _stopBits = stopBits;

            _comPort.DataReceived  += new SerialDataReceivedEventHandler(comPort_DataReceived);
            _comPort.ErrorReceived += new SerialErrorReceivedEventHandler(comPort_ErrorReceived);
        }