SerialPort Test


hi,

i'm programming serial port communication class , have couple of problems.

the first function close. method disposes serial port. instead close communication , not release resourse, possible? usage correct serial port?

the second problem on integration test. when testing write , read work in debug because if run them receive on serial port portion of buffer. how can test these function reliably?

any help will be  welcome, thank in advance.

 

hi namale,

first let's @ official statement of serialport.close method:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.close.aspx.
"closes port connection, sets isopen property false, , disposes of internal stream object."
"calling method closes serialport object , clears both receive , transmit buffers. method calls component.dispose method, invokes protected serialport.dispose(boolean) method disposing parameter set true."

now go check msdn library serialport.dispose(boolean) method:
http://msdn.microsoft.com/en-us/library/cekaawx6.aspx.
"...this method flushes , closes stream object in basestream property."

here internal implementation of serialport.dispose(boolean) method:
  protected override void dispose(bool disposing) {     if (disposing && this.isopen)     {         this.internalserialstream.flush();         this.internalserialstream.close();         this.internalserialstream = null;     }     base.dispose(disposing); }  

basestream property internal operates private internalserialstream field.

let's @ serialport.open method do, msdn libraries won't supplied, here give internal implementation of method:
  public void open() {     if (this.isopen)     {         throw new invalidoperationexception(sr.getstring("port_already_open"));     }     new securitypermission(securitypermissionflag.unmanagedcode).demand();     this.internalserialstream = new serialstream(this.portname, this.baudrate, this.parity, this.databits, this.stopbits, this.readtimeout, this.writetimeout, this.handshake, this.dtrenable, this.rtsenable, this.discardnull, this.parityreplace);     this.internalserialstream.setbuffersizes(this.readbuffersize, this.writebuffersize);     this.internalserialstream.errorreceived += new serialerrorreceivedeventhandler(this.catcherrorevents);     this.internalserialstream.pinchanged += new serialpinchangedeventhandler(this.catchpinchangedevents);     this.internalserialstream.datareceived += new serialdatareceivedeventhandler(this.catchreceivedevents); }  

serialport instance there, while resources (like 1 instance of stream referenced internalserialstream field) cleared within close method.

have nice day,
leo liu [msft]
msdn community support | feedback us


Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'