Home > Net >  The.net dual serial port receiving data at the same time
The.net dual serial port receiving data at the same time

Time:10-13

I want to receive pass two serial data at the same time, now defines two SerialPort, wrote two receiving function, two managed to receive function auxiliary thread SerialDataReceivedEventArgs,
SerialPort1. Method DataReceived +=new SerialDataReceivedEventHandler (serialPort1_DataReceived);
SerialPort2. Method DataReceived +=new SerialDataReceivedEventHandler (serialPort2_DataReceived);
If I just open a serial, data reception there is no problem, but at the same time open two serial ports, receiving process become very slowly, and sometimes will stop receiving,
I try to put two SerialDataReceivedEventArgs in thread, but the effect is not good, is there anyone what solution?
Now posted two receiving function
Private void serialPort1_DataReceived (object sender, System. IO, Ports, SerialDataReceivedEventArgs e)
{

Try
{
String res=null;
Thread.sleep (100);
Int length=4;
Num1 +=length;
Byte [] buf=new byte (length),
SerialPort1. Read (buf, 0, length);

This. Invoke ((EventHandler) (delegate
{
For the. Text=num1. ToString ();
TextBox1. Text +=res;
}));

}
The catch (Exception)
{
MessageBox. Show (" is closing... ");
}

}


Private void serialPort2_DataReceived (object sender, System. IO, Ports, SerialDataReceivedEventArgs e)
{
Try
{
String s=null;
S=serialPort2. ReadLine ();
Num2 + +;
SerialPort2. DiscardInBuffer ();

This. Invoke ((EventHandler) (delegate
{
TextBox2. Text +=s + "";
Button3. Text=num2. ToString ();

}));

} the catch (Exception ee)
{
MessageBox. Show (ee. The ToString ());
}
  •  Tags:  
  • C#
  • Related