⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Bluetooth Simulation in C# with Serial Ports By Badea Florin
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace CSSP
{
    public partial class Form1 : Form
    {
        SerialPort serialIn;
        SerialPort serialOut;

        Thread rcvThread;

        private delegate void updateText(string s);
        private delegate void closeDel();
        private delegate void disconnectDel();
        private string inboundPort = "";
        private string outboundPort = "";

        bool closeRequested = false;
        bool disconnectRequested = false;
        int numThreads = 0;
        public Form1()
        {
            InitializeComponent();
            lblStatus.Text = "Ports not set.";            
        }
        private void ReceiveData()
        {
            while (!closeRequested && !disconnectRequested)
            {
                try
                {
                    string line = serialIn.ReadLine();
                    if (line.CompareTo("quit$$$") == 0)
                    {
                        disconnectRequested = true;
                        continue;
                    }
                    txtLog.Invoke(new updateText(UpdateText), line);
                }
                catch 
                {
                    
                }
            }
            if (closeRequested)
                closeMe();
            if (disconnectRequested)
                this.Invoke(new disconnectDel(onDisconnect));               
        }
        private void onDisconnect()
        {
            serialIn.Close();
            serialOut.Close();
            mnuDisconnect.Enabled = false;
            mnuConnect.Enabled = false;
            mnuSettings.Enabled = true;
            lblStatus.Text = "Disconected.\r\nPorts not set.";
            numThreads--;
        }

        private void UpdateText(string s)
        {
            txtLog.Text += "thing:" + s + "\r\n";
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                serialOut.WriteLine(txtMess.Text);
                txtLog.Text += "you:" + txtMess.Text + "\r\n";
                txtMess.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void closeMe()
        {
            numThreads--;           
            this.Invoke(new closeDel(this.Close));
        }
        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            if (this.numThreads > 0)
            {
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
                if (serialIn != null && serialOut != null)
                {

                    serialIn.Close();
                    serialOut.Close();
                }
            }
            this.closeRequested = true;
        }
        private void mnuSettings_Click(object sender, EventArgs e)
        {
            SettingsForm form = new SettingsForm(inboundPort, outboundPort);
            if (form.ShowDialog() == DialogResult.OK)
            {
                if (form.GetInboundPort().CompareTo("NO PORT SELECTED") == 0 ||
                    form.GetOutboundPort().CompareTo("NO PORT SELECTED") == 0)
                {
                    MessageBox.Show("The ports were not set properly.");
                    lblStatus.Text = "Ports not set.";
                    mnuConnect.Enabled = false;
                }
                else
                {
                    inboundPort = form.GetInboundPort();
                    outboundPort = form.GetOutboundPort();
                    mnuConnect.Enabled = true;
                    lblStatus.Text = "Ports set.\r\nin:" + inboundPort + "\r\nout:" + outboundPort+"\r\nWaiting to press Connect...";
                }
            }
        }

        private void mnuConnect_Click(object sender, EventArgs e)
        {
            serialIn = new SerialPort(inboundPort);
            serialOut = new SerialPort(outboundPort);
            serialIn.ReadTimeout = 1000;
            serialOut.ReadTimeout = 1000;
            disconnectRequested = false;
            try
            {
                if (!serialIn.IsOpen)
                {
                    lblStatus.Text = "Input port closed. Opening input port...";
                    serialIn.Open();
                }
                if (!serialOut.IsOpen)
                {
                    lblStatus.Text = "Output port closed. Opening output port...";
                    serialOut.Open();
                }
                lblStatus.Text = "Ports opened. Starting the listener thread...";
                rcvThread = new Thread(new ThreadStart(ReceiveData));
                numThreads++;
                rcvThread.Start();
                lblStatus.Text = "Listener thread started.";
                btnSend.Enabled = true;
                lblStatus.Text="Connected.\r\nInbound:" + inboundPort + "\r\nOutbound:" + outboundPort;
                MessageBox.Show("Connected.");
                mnuConnect.Enabled = false;
                mnuSettings.Enabled = false;
                mnuDisconnect.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                lblStatus.Text = "error.\r\nPorts not set.";
                mnuConnect.Enabled = false;
            }
        }
        
        private void mnuDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                disconnectRequested = true;
                serialOut.WriteLine("quit$$$");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void mnuClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -