c# - Multiple objects containing SerialPort with same SerialDataReceivedEventHandler -


i have 2 objects (of same class) each contain serialport object. class has method handles serialport.datareceived event , used both serialport objects.

when instantiate each object in separate application, each port handles datareceived event individually expected.

when instantiate 2 instances of com_front_end class in same application , send data 1 serial port other, both port's datareceived event handlers fire. short, i'll call "cross-talk".

my class structure looks this:

public class com_front_end {     private serialport_custom port;     private lockobject;      public com_front_end(string portname, string baudrate)     {         // other code          port = new serialport_custom(portname, baudrate, new serialdatareceivedeventhandler(serialdatareceived));          port.open();     }      private void serialdatareceived(object sender, serialdatareceivedeventargs e)     {         //lock (lockobject)  // lock not needed here. 1 serialdatareceived event can fire @ time         //{             serialport port;              try             {                 port = sender serialport;                  if (port != null)                 {                     byte[] buffer = new byte[port.bytestoread];                     int bytesread = port.read(buffer, 0, buffer.length);                      foreach (byte inbyte in buffer)                     {                         // byte processing code                     }                 }             }             catch (exception ex)             {                 // exception handling code             }         //}     } } 

the class containing actual serialport class looks like:

public class serialport_custom : serialport {     public serialport_custom(string portname, int baudrate, serialdatareceivedeventhandler datareceivedhandler)     {         this.portname = portname;         this.baudrate = baudrate;         this.parity = system.io.ports.parity.none;         this.databits = 8;         this.stopbits = system.io.ports.stopbits.one;         this.handshake = system.io.ports.handshake.none;         this.rtsenable = true;         this.dtrenable = true;         this.discardnull = false;         this.encoding = encoding.ascii;          this.datareceived += datareceivedhandler;     }      // other methods } 

i have 2 instances of com_front_end class in same application. whenever 1 instance receives data, both objects' serialdatareceived methods fire.

why datareceived event handler fire both serial ports when instantiated in same application? furthermore, how can ensure multiple instantiation of class not cause "cross-talk"?

i've found root cause of problem:

the project in com_front_end resides has 2 static classes. 1 of these classes receive buffer , other transmit buffer. changing these classes not static solved problem. within each com_front_end object task polls receive buffer. since both use same static class, both pulling buffer explains why

a. serialdatareceived both objects fired.

b. data received each mangled/partial.

tl;dr: non-static objects containing static objects yield shared resources whether intended or not.

please correct me wherever explanation faulty or incomplete.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -