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

📄 form1.cs

📁 Example to access serial port.
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using sgUnitManagementLibrary;


namespace demoGSM
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
        private System.Windows.Forms.TextBox textBoxResponse;
        private System.Windows.Forms.Button btnSend;
		private System.Windows.Forms.Button btnEnable;
		private System.Windows.Forms.Button btnDisable;
		private System.Windows.Forms.Label labelEnabled;

        private System.IO.Ports.SerialPort serialPort_GSM;
        private System.ComponentModel.IContainer components;
        private ComboBox comboBoxCommand;
        private System.Windows.Forms.Timer timerEnable;

        private SGC_UnitManagement myUnitMan = new SGC_UnitManagement();

        private static Form1 thisForm;


		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

            thisForm = this;
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.textBoxResponse = new System.Windows.Forms.TextBox();
            this.btnSend = new System.Windows.Forms.Button();
            this.btnEnable = new System.Windows.Forms.Button();
            this.btnDisable = new System.Windows.Forms.Button();
            this.labelEnabled = new System.Windows.Forms.Label();
            this.serialPort_GSM = new System.IO.Ports.SerialPort(this.components);
            this.comboBoxCommand = new System.Windows.Forms.ComboBox();
            this.timerEnable = new System.Windows.Forms.Timer();
            this.SuspendLayout();
            // 
            // textBoxResponse
            // 
            this.textBoxResponse.Location = new System.Drawing.Point(35, 77);
            this.textBoxResponse.Multiline = true;
            this.textBoxResponse.Name = "textBoxResponse";
            this.textBoxResponse.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBoxResponse.Size = new System.Drawing.Size(216, 104);
            this.textBoxResponse.TabIndex = 7;
            // 
            // btnSend
            // 
            this.btnSend.Enabled = false;
            this.btnSend.Location = new System.Drawing.Point(113, 22);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(72, 20);
            this.btnSend.TabIndex = 5;
            this.btnSend.Text = "Send";
            this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
            // 
            // btnEnable
            // 
            this.btnEnable.Location = new System.Drawing.Point(35, 22);
            this.btnEnable.Name = "btnEnable";
            this.btnEnable.Size = new System.Drawing.Size(72, 20);
            this.btnEnable.TabIndex = 2;
            this.btnEnable.Text = "Enable";
            this.btnEnable.Click += new System.EventHandler(this.btnEnable_Click);
            // 
            // btnDisable
            // 
            this.btnDisable.Enabled = false;
            this.btnDisable.Location = new System.Drawing.Point(187, 22);
            this.btnDisable.Name = "btnDisable";
            this.btnDisable.Size = new System.Drawing.Size(72, 20);
            this.btnDisable.TabIndex = 1;
            this.btnDisable.Text = "Disable";
            this.btnDisable.Click += new System.EventHandler(this.btnDisable_Click);
            // 
            // labelEnabled
            // 
            this.labelEnabled.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            this.labelEnabled.ForeColor = System.Drawing.Color.Red;
            this.labelEnabled.Location = new System.Drawing.Point(51, -1);
            this.labelEnabled.Name = "labelEnabled";
            this.labelEnabled.Size = new System.Drawing.Size(208, 20);
            this.labelEnabled.Text = "GSM Disabled";
            this.labelEnabled.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // serialPort_GSM
            // 
            this.serialPort_GSM.BaudRate = 115200;
            this.serialPort_GSM.DiscardNull = true;
            this.serialPort_GSM.DtrEnable = true;
            this.serialPort_GSM.PortName = "COM9:";
            this.serialPort_GSM.ReadBufferSize = 2048;
            this.serialPort_GSM.ReadTimeout = 1000;
            this.serialPort_GSM.ReceivedBytesThreshold = 2048;
            this.serialPort_GSM.RtsEnable = true;
            // 
            // comboBoxCommand
            // 
            this.comboBoxCommand.Items.Add("AT");
            this.comboBoxCommand.Items.Add("ATE0");
            this.comboBoxCommand.Items.Add("ATE1");
            this.comboBoxCommand.Items.Add("AT+CGMI?");
            this.comboBoxCommand.Items.Add("AT+CSQ");
            this.comboBoxCommand.Items.Add("AT+CGDCONT?");
            this.comboBoxCommand.Items.Add("AT+CGDCONT=1,IP,internet,0.0.0.0,0,0");
            this.comboBoxCommand.Items.Add("AT+CMGR");
            this.comboBoxCommand.Location = new System.Drawing.Point(35, 48);
            this.comboBoxCommand.Name = "comboBoxCommand";
            this.comboBoxCommand.Size = new System.Drawing.Size(136, 23);
            this.comboBoxCommand.TabIndex = 8;
            this.comboBoxCommand.SelectedIndexChanged += new System.EventHandler(this.comboBoxCommand_SelectedIndexChanged);
            // 
            // timerEnable
            // 
            this.timerEnable.Interval = 5;
            this.timerEnable.Tick += new System.EventHandler(this.timerEnable_Tick);
            // 
            // Form1
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(638, 455);
            this.Controls.Add(this.comboBoxCommand);
            this.Controls.Add(this.labelEnabled);
            this.Controls.Add(this.btnDisable);
            this.Controls.Add(this.btnEnable);
            this.Controls.Add(this.btnSend);
            this.Controls.Add(this.textBoxResponse);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = ".NET GSM Demo";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void btnEnable_Click(object sender, System.EventArgs e)
		{
            labelEnabled.Text = "Waiting for GSM..";
            btnEnable.Enabled = false;

            timerEnable.Enabled = true;
        }

        private void timerEnable_Tick(object sender, EventArgs e)
        {
            timerEnable.Enabled = false;
            EnableGSM();
        }

        private void EnableGSM()
        {
            if (myUnitMan.EnableDevice(SGC_UnitManagement.Device.GSM) == SGC_UnitManagement.ERROR_NONE)
            {
                Thread.Sleep(10000);

                try
                {
                    serialPort_GSM.Open();
                }
                catch { }

                if (serialPort_GSM.IsOpen)
                {
                    labelEnabled.ForeColor = Color.Green;
                    labelEnabled.Text = "GSM Enabled";

                    btnEnable.Enabled = false;
                    btnDisable.Enabled = true;
                    btnSend.Enabled = true;
                }
                else
                {
                    myUnitMan.DisableDevice(SGC_UnitManagement.Device.GSM);
                    MessageBox.Show("Cannot Open Port");
                }
            }
            else
            {
                labelEnabled.Text = "GSM Disabled";
                btnEnable.Enabled = true;
            }
        }

		private void btnDisable_Click(object sender, System.EventArgs e)
		{
            try
            {
                serialPort_GSM.Close();
            }
            catch { }

            myUnitMan.DisableDevice(SGC_UnitManagement.Device.GSM);

			labelEnabled.ForeColor = Color.Red;
			labelEnabled.Text = "GSM Disabled"; 

			btnEnable.Enabled  = true;
			btnDisable.Enabled = false;
			btnSend.Enabled    = false;
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			textBoxResponse.Text = "";
		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
            if (serialPort_GSM.IsOpen)
			{
                btnSend.Enabled = false;

                try
                {
                    textBoxResponse.Text = "";

                    char[] resultBuffer = new char[64];

                    serialPort_GSM.DiscardInBuffer();
                    serialPort_GSM.WriteLine(comboBoxCommand.Text + "\r\n");

                    int i = 0;
                    while ((serialPort_GSM.Read(resultBuffer, 0, 1) > 0) && (i < 1024))
                    {
                        textBoxResponse.Text += new string(resultBuffer);
                        i++;
                    }
                }
                catch
                {
                    // Code here to handle thread exception
                }
                btnSend.Enabled = true;
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
           

            int status = myUnitMan.GetDeviceState(SGC_UnitManagement.Device.GSM);

            if (status >= SGC_UnitManagement.ERROR_NONE)
			{
				switch (status)
				{
                    case SGC_UnitManagement.SGUM_DEVICE_STATE_DISABLED:
                        // Do nothing
						break;
                    case SGC_UnitManagement.SGUM_DEVICE_STATE_ENABLED:
						btnEnable_Click(sender, e);
						break;
					default:
						labelEnabled.ForeColor = Color.Red;
						labelEnabled.Text = "ERROR - GSM NOT FOUND";
						
						btnEnable.Enabled  = false;
						break;
				}
			}
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
            try
            {
                serialPort_GSM.Close();
            }
            catch { }

            myUnitMan.DisableDevice(SGC_UnitManagement.Device.GSM);
		}

		private void textBoxCommand_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (e.KeyChar == 0x0D)
			{
				btnSend_Click(sender, null);
			}
		}

        private void comboBoxCommand_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
	}
}

⌨️ 快捷键说明

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