📄 form1.cs
字号:
catch
{
//If there was an exception, then close the handle to
// the device and assume that the device was removed
btnClose_Click(this, null);
}
}
/****************************************************************************
Function:
private void btnClose_Click(object sender, EventArgs e)
Summary:
This function closes the COM port.
Description:
This function closes the COM port. This function is launched when the
btnClose button is clicked. This function can also be called directly
from other functions. In addition to closing the COM port, this
function will also change the Enable attribute of several of the form
objects to enable the user to open 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 btnClose_Click(object sender, System.EventArgs e)
{
//Reset the state of the application objects
btnClose.Enabled = false;
btnConnect.Enabled = true;
lstCOMPorts.Enabled = true;
//This section of code will try to close the COM port.
// Please note that it is important to use a try/catch
// statement when closing the COM port. If a USB virtual
// COM port is removed and the PC software tries to close
// 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
{
//Dispose the In and Out buffers;
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
//Close the COM port
serialPort1.Close();
}
//If there was an exeception then there isn't much we can
// do. The port is no longer available.
catch { }
}
/****************************************************************************
Function:
private void serialPort1_DataReceived( object sender,
SerialDataReceivedEventArgs e)
Summary:
This function prints any data received on the COM port.
Description:
This function is called when the data is received on the COM port. This
function attempts to write that data to the txtDataReceived textbox. If
an exception occurs the btnClose_Click() function is called in order to
close the COM port that caused the exception.
Precondition:
None
Parameters:
object sender - Sender of the event (this form)
SerialDataReceivedEventArgs e - The event arguments
Return Values:
None
Remarks:
None
***************************************************************************/
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//The ReadExisting() function will read all of the data that
// is currently available in the COM port buffer. In this
// example we are sending all of the available COM port data
// to the SetText() function.
//
// NOTE: the <SerialPort>_DataReceived() function is launched
// in a seperate thread from the rest of the application. A
// delegate function is required in order to properly access
// any managed objects inside of the other thread. Since we
// will be writing to a textBox (a managed object) the delegate
// function is required. Please see the SetText() function for
// more information about delegate functions and how to use them.
try
{
SetText(serialPort1.ReadExisting());
}
catch
{
//If there was an exception, then close the handle to
// the device and assume that the device was removed
btnClose_Click(this, null);
}
}
/****************************************************************************
Function:
private void SetText(string text)
Summary:
This function prints the input text to the txtDataReceived textbox.
Description:
This function prints the input text to the txtDataReceived textbox. If
the calling thread is the same as the thread that owns the textbox, then
the AppendText() method is called directly. If a thread other than the
main thread calls this function, then an instance of the delegate function
is created so that the function runs again in the main thread.
Precondition:
None
Parameters:
string text - Text that needs to be printed to the textbox
Return Values:
None
Remarks:
None
***************************************************************************/
private void SetText(string text)
{
//InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true. We can
// use this attribute to determine if we can append text
// directly to the textbox or if we must launch an a delegate
// function instance to write to the textbox.
if (txtDataReceived.InvokeRequired)
{
//InvokeRequired returned TRUE meaning that this function
// was called from a thread different than the current
// thread. We must launch a deleage function.
//Create an instance of the SetTextCallback delegate and
// assign the delegate function to be this function. This
// effectively causes this same SetText() function to be
// called within the main thread instead of the second
// thread.
SetTextCallback d = new SetTextCallback(SetText);
//Invoke the new delegate sending the same text to the
// delegate that was passed into this function from the
// other thread.
Invoke(d, new object[] { text });
}
else
{
//If this function was called from the same thread that
// holds the required objects then just add the text.
txtDataReceived.AppendText(text);
}
}
/****************************************************************************
Function:
private void btnSendData_Click(object sender, EventArgs e)
Summary:
This function will attempt to send the contents of txtData over the COM port
Description:
This function is called when the btnSendData button is clicked. It will
attempt to send the contents of txtData over the COM port. If the attempt
is unsuccessful this function will call the btnClose_Click() in order to
close the COM port that just failed.
Precondition:
None
Parameters:
object sender - Sender of the event (this form)
EventArgs e - The event arguments
Return Values:
None
Remarks:
None
***************************************************************************/
private void btnSendData_Click(object sender, System.EventArgs e)
{
//This section of code will try to write to the COM port.
// Please note that it is important to use a try/catch
// statement when writing to the COM port. If a USB virtual
// COM port is removed and the PC software tries to write
// to 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
{
//Write the data in the text box to the open serial port
serialPort1.Write(txtData.Text);
}
catch
{
//If there was an exception, then close the handle to
// the device and assume that the device was removed
btnClose_Click(this, null);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -