Home > Net >  The order of the serial port receive events
The order of the serial port receive events

Time:09-25

I have a serial device to connect a serial port, as long as open the serial port, the device will continue to send real-time data to come over, if, in accordance with the following code, then I when I received the data value is 0 or 1, what is the program execution order, small white recently confuse the order of events, also please bosses to reassure

Private void Button3_Click_1 (object sender, EventArgs e)
{
Int I=0;
Port. The method DataReceived +=port_DataReceived;
Port. The open ();
i++;
}

Private void Aaport_DataReceived (object sender, SerialDataReceivedEventArgs e)
{
. Other code...
}

CodePudding user response:

Private void Button3_Click_1 (object sender, EventArgs e)
{
Int I=0;
Port. The method DataReceived +=port_DataReceived;
Port. The open ();
i++;
}
This code, you are opening a serial port and bind a serial port receive events, each serial data to perform port_DataReceived here the code
As you write code, you order a button3, after opening a serial port, at that time I=1, and I again is to define the button inside of you, is you no matter how many times I, I still=1
And serial events will be binding for many times, a serial port data, you can perform multiple port_DataReceived, see your button several times, port_DataReceived will be executed several times

CodePudding user response:

Can change to this
 

Int I=0;
Private void Button3_Click_1 (object sender, EventArgs e)
{
Port. The method DataReceived +=port_DataReceived;
Port. The open ();
}

Private void Aaport_DataReceived (object sender, SerialDataReceivedEventArgs e)
{
i++;
. Other code...
}

CodePudding user response:

,,, event names are different, change to the following so
 



Int I=0;
Private void Button3_Click_1 (object sender, EventArgs e)
{
Port. The method DataReceived +=port_DataReceived;
Port. The open ();
}

Private void port_DataReceived (object sender, SerialDataReceivedEventArgs e)
{
i++;
. Other code...
}


CodePudding user response:

refer to kill 1/f, matt, bull response:
private void Button3_Click_1 (object sender, EventArgs e)
{
Int I=0;
Port. The method DataReceived +=port_DataReceived;
Port. The open ();
i++;
}
This code, you are opening a serial port and bind a serial port receive events, each serial data to perform port_DataReceived here the code
As you write code, you order a button3, after opening a serial port, at that time I=1, and I again is to define the button inside of you, is you no matter how many times I, I still=1
And serial events will be binding for many times, a serial port data, you can perform multiple port_DataReceived, see your button several times, will be executed several times port_DataReceived

Brother, assuming that this is me to write a method, rather than a button click event, then when I perform this method to perform this step to a serial port open, this time I should have received the devices to send data, this time the content of the program is to continue my way, or perform other code receiving data inside

CodePudding user response:

reference 4 floor qq_39069474 response:
Quote: refer to 1st floor kill matt, bull response:
private void Button3_Click_1 (object sender, EventArgs e)
{
Int I=0;
Port. The method DataReceived +=port_DataReceived;
Port. The open ();
i++;
}
This code, you are opening a serial port and bind a serial port receive events, each serial data to perform port_DataReceived here the code
As you write code, you order a button3, after opening a serial port, at that time I=1, and I again is to define the button inside of you, is you no matter how many times I, I still=1
And serial events will be binding for many times, a serial port data, you can perform multiple port_DataReceived, see your button several times, will be executed several times port_DataReceived

Brother, assuming that this is a way to I write, rather than a button click event, then when I perform this method to perform to a serial port to open this step, this time I should have received the devices to send data, this time the content of the program is to continue my way, or the other code execution receive data inside the




Will perform the code in the binding event here, you are here to bind port_DataReceived, it would be to perform port_DataReceived this method in the code
 
Port. The method DataReceived +=port_DataReceived
  •  Tags:  
  • C#
  • Related