📄 form1.cs
字号:
//Here are some useful articles when creating new PC applications for COM ports:
//(links valid as of June 3, 2008)
//
//"SerialPort Class"
//http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
//
//"SerialPort Members"
//http://msdn.microsoft.com/en-us/library/system.io.ports.serialport_members.aspx
//
//"SerialPort.DataReceived Event"
//http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx
//
//"How to: Make Thread-Safe Calls to Windows Forms Controls"
//http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx
//
//"The C# Language"
//http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx
//
//"C# Tutorials"
//http://msdn.microsoft.com/en-us/library/aa288436.aspxusing 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;
namespace Csharp_Simple_CDC_Demo
{
public partial class Form1 : Form
{
//Create a delegate function for this thread that will take
// in a string and will write it to the txtDataReceived textbox
delegate void SetTextCallback(string text);
/****************************************************************************
Function:
public Form1()
Summary:
The main contructor for the Form1 class.
Description:
The main contructor for the Form1 class. This function creates and
initializes all of the form objects.
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
public Form1()
{
InitializeComponent();
//Initialize the COM ports list
UpdateCOMPortList();
}
/****************************************************************************
Function:
private void UpdateCOMPortList()
Summary:
This function updates the COM ports listbox.
Description:
This function updates the COM ports listbox. This function is launched
periodically based on its Interval attribute (set in the form editor under
the properties window).
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
private void UpdateCOMPortList()
{
int i;
bool foundDifference;
i = 0;
foundDifference = false;
//If the number of COM ports is different than the last time we
// checked, then we know that the COM ports have changed and we
// don't need to verify each entry.
if (lstCOMPorts.Items.Count == SerialPort.GetPortNames().Length)
{
//Search the entire SerialPort object. Look at COM port name
// returned and see if it already exists in the list.
foreach (string s in SerialPort.GetPortNames())
{
//If any of the names have changed then we need to update
// the list
if (lstCOMPorts.Items[i++].Equals(s) == false)
{
foundDifference = true;
}
}
}
else
{
foundDifference = true;
}
//If nothing has changed, exit the function.
if (foundDifference == false)
{
return;
}
//If something has changed, then clear the list
lstCOMPorts.Items.Clear();
//Add all of the current COM ports to the list
foreach (string s in SerialPort.GetPortNames())
{
lstCOMPorts.Items.Add(s);
}
//Set the listbox to point to the first entry in the list
lstCOMPorts.SelectedIndex = 0;
}
/****************************************************************************
Function:
private void timer1_Tick(object sender, EventArgs e)
Summary:
This function updates the COM ports listbox.
Description:
This function updates the COM ports listbox. This function is launched
periodically based on its Interval attribute (set in the form editor under
the properties window).
Precondition:
None
Parameters:
object sender - Sender of the event (this form)
EventArgs e - The event arguments
Return Values:
None
Remarks:
None
***************************************************************************/
private void timer1_Tick(object sender, System.EventArgs e)
{
//Update the COM ports list so that we can detect
// new COM ports that have been added.
UpdateCOMPortList();
}
/****************************************************************************
Function:
private void btnConnect_Click(object sender, EventArgs e)
Summary:
This function opens the COM port.
Description:
This function opens the COM port. This function is launched when the
btnConnect button is clicked. In addition to opening the COM port, this
function will also change the Enable attribute of several of the form
objects to disable the user from opening a new COM port.
Precondition:
None
Parameters:
object sender - Sender of the event (this form)
EventArgs e - The event arguments
Return Values:
None
Remarks:
None
***************************************************************************/
private void btnConnect_Click(object sender, System.EventArgs e)
{
//This section of code will try to open the COM port.
// Please note that it is important to use a try/catch
// statement when opening the COM port. If a USB virtual
// COM port is removed and the PC software tries to open
// the COM port before it detects its removal then
// an exeception is thrown. If the execption is not in a
// try/catch statement this could result in the application
// crashing.
try
{
//Get the port name from the application list box.
// the PortName attribute is a string name of the COM
// port (e.g. - "COM1").
serialPort1.PortName = lstCOMPorts.Items[lstCOMPorts.SelectedIndex].ToString();
//Open the COM port.
serialPort1.Open();
//Change the state of the application objects
btnConnect.Enabled = false;
lstCOMPorts.Enabled = false;
btnClose.Enabled = true;
//Clear the textbox and print that we are connected.
txtDataReceived.Clear();
txtDataReceived.AppendText("Connected.\r\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -