立即注冊 登錄
返回首頁

uid:208491的個人空間

日志

PC控制程序

已有 507 次閱讀2017-6-6 20:35

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//以上幾句是自動生成的

using System.IO.Ports;//我加的,包含串口相關(guān)的
using System.Text.RegularExpressions;//我加的


namespace WindowsFormsApplication1
{
    public partial class PCZigbeeLed : Form
    {
        private SerialPort comm = new SerialPort();//我加的,新建一個串口變量
        private bool closing = false;//是否正在關(guān)閉串口,執(zhí)行Application.DoEvents,并阻止再次invoke
        private bool Listening = false;//是否沒有執(zhí)行完invoke相關(guān)操作
        public PCZigbeeLed()//自動生成的函數(shù)
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comboPortName.Items.AddRange(ports);
            comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;

            //初始化SerialPort對象  
            comm.NewLine = "/r/n";
            comm.RtsEnable = true;//根據(jù)實際情況吧。 
            //comm.DataReceived += comm_DataReceived;
        }

    private void comboPortName_SelectedIndexChanged(object sender, EventArgs e)
    {

     }

        private void buttonOpenClosecom_Click(object sender, EventArgs e)
        {
            //根據(jù)當(dāng)前串口對象,來判斷操作  
            if (comm.IsOpen)
            {
                closing = true;
                while (Listening) Application.DoEvents();
                //打開時點擊,則關(guān)閉串口  
                comm.Close();
                //this.timer1.Stop();
                closing = false;
            }
            else
            {
                //關(guān)閉時點擊,則設(shè)置好端口,波特率后打開  
                comm.PortName = comboPortName.Text;
                comm.BaudRate = 9600;
                try
                {
                    comm.Open();
                }
                catch (Exception ex)
                {
                    //捕獲到異常信息,創(chuàng)建一個新的comm對象,之前的不能用了。  
                    comm = new SerialPort();
                    //現(xiàn)實異常信息給客戶。  
                    MessageBox.Show(ex.Message); 
                }
            }
            //設(shè)置按鈕的狀態(tài)  
            buttonOpenClosecom.Text = comm.IsOpen ? "關(guān)閉串口" : "打開串口";
        }

        private void buttonOpenLight_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[2];
            SendBuf[0] = 0x31;
            SendBuf[1] = 0x31;
            comm.Write(SendBuf, 0, 2);
            System.Threading.Thread.Sleep(100);
        }

        private void buttonCloseLight_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[2];
            SendBuf[0] = 0x31;
            SendBuf[1] = 0x30;
            comm.Write(SendBuf, 0, 2);
            System.Threading.Thread.Sleep(100);
        }
    }
}


路過

雞蛋

鮮花

握手

雷人

評論 (0 個評論)

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

返回頂部