According to the article found on the Internet, the program added two bool variables, as a status mark, to ensure that the serial port is closed, the serial port event has been processed
Private volatile bool is_serial_listening = false;//serial port is listening for tags
Private volatile bool is_serial_closing = false;//serial port is closing tag
Program begins
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.IO.Ports; This namespace is needed to use SerialPort
Namespace CommTest
{
public partial class Form1:form
{
private string dispstring; Used to store the values read
Private volatile bool is_serial_listening = false;//serial port is listening for tags
Private volatile bool is_serial_closing = false;//serial port is closing tag
Public Form1 ()
{
InitializeComponent ();
}
private void Form1_Load (object sender, EventArgs e)
{
Port name can be identified by checking the ports
section on device Manager after connecting your device
Serialport1.portname = "COM3";
Provide the name of the port to which device is connected
Default values of Hardware[check with device specification document]
Serialport1.baudrate = 9600;
serialport1.parity = Parity.none;
Serialport1.stopbits = Stopbits.one;
Serialport1.handshake = Handshake.none;
Serialport1.open (); Opens the port
Serialport1.readtimeout = 200;
if (Serialport1.isopen)
{
dispstring = "";
Txtcardkeydeactivate.text = "";
}
serialport1.datareceived + = new Serialdatareceivedeventhandler (serialport1_datareceived);
}
private void Serialport1_datareceived (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (is_serial_closing)
{
Is_serial_listening = false; When you are ready to close the serial port, reset serial listener flag
Return
}
Try
{
if (Serialport1.isopen)
{
Is_serial_listening = true;
dispstring = Serialport1.readexisting ();
This. Invoke (New EventHandler (DisplayText));
byte[] bsendtemp = new Byte[1];
Bsendtemp[0] = 0x00;
Serialport1.write (bsendtemp, 0, 1);
}
}
Finally
{
is_serial_listening = false;//Serial call is complete, reset serial port listening flag
}
}
private void DisplayText (object sender, EventArgs e)
{
Textbox1.appendtext (dispstring);
TextBox2.Text = Substringcount (TextBox1.Text, "OK"). ToString ();//write your own test statement, count the number of "OK" string occurrences
}
private static int Substringcount (string str, string substring)
{
if (str. Contains (substring))
{
string strreplaced = str. Replace (substring, "");
Return (str. length-strreplaced.length)/substring. Length;
}
return 0;
}
private void Form1_formclosing (object sender, FormClosingEventArgs e)
{
Is_serial_closing = true;//The position is_serial_closing mark when the window is closed
while (is_serial_listening) application.doevents ();
Serialport1.close ();
}
}
}
C # SerialPort when executing the Close () method, the resolution of the program dies