in Winforms, this is easily done:
using System.IO.Ports;
SerialPort myComPort = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
if (myComPort.IsOpen == false)
{
myComPort.Open();
}
Can I do the same in WebForms in ASP.NET?
yesterday when I tested it, it seems to be OK, but today, it gives me problem:
Access to the port 'COM1' is denied.
What are the problems here? What are the alternative solutions?

1 answers
As far as I'm aware, the SerialPort class works with any type of .NET application.
However, a problem all ASP.NET applications have is persisting objects between postbacks. An object will be destroyed when a page is unloaded unless you save it to Application or Session state so that the next page can recover it.
What may be happening here is that you're creating a SerialPort object and opening a connecton to it in a button click handler. Following the postback, the SerialPort object has been destroyed but the connection has not yet been closed by the OS (there's generally a delay in doing this) and so you're getting the 'access denied' exception when you press the button again.
I'd therefore try creating and opening the SerialPort object just the once (in Application_Start say) and persisting it to Application state. Every time you access it subsequently from a different eventhandler, recover the object from Application state and close the connection just before the application ends.
answered one year ago by:
17279
494
thanks.