开发者

Add serial port to web page

Is it possible to add a serial port control to a web application. I've tried creating one programmatically, but i have issues with the port staying open. I'm not sure how to fix that except by adding a serial port control somehow to the web page. Any ideas on how I can accomplish this task? Thanks in advance.

the following is the code I currently have:

public partial class LoadCellTest : System.Web.UI.Page
{ 
SerialPort serialPort1 = new SerialPort("COM3",9600,Parity.None,8,StopBits.One);

 protected void Page_Load(object sender, EventArgs e)
{

    serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
    serialPort1.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort1_ErrorReceived);

}

delegate void SerialDataReceivedDelegate(object sender, SerialDataReceivedEventArgs e);
delegate void SerialErr开发者_如何学GoorReceivedDelegate(object sender, SerialErrorReceivedEventArgs e);


protected void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   TextBox1.Text = (serialPort1.ReadExisting());
   if (serialPort1.ReadExisting().Length == 0)
   {
       ListBox1.Items.Insert(0, TextBox1.Text);
       TextBox1.Text = "";
   }
}
protected void button1_Click(object sender, EventArgs e)
{
    try
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
            button1.Text = "Start";
        }
        else
        {
            serialPort1.Open();
            button1.Text = "Stop";
        }
    }
    catch (IOException ex)
    {
        MessageBox.Show(ex.Message);
    }
}


Turning my comments into an answer...

The serial port code is server-side code. You can't do it on the client with ASP.NET.

Creating an ActiveX or other fat client control is a load of work, and just not a good idea.

My recommendation would be to continue on with your WinForms app for the code where you need the scanner, and add a menu to it to enable you to launch a separate ASP.NET web app for the reports/data access.

If you want to make it more "seamless" for the users, you can add a form with a WebBrowser control that loads your report/data access site. To them it will just be a "part of the application".

THAT SAID, depending on the device you have connected to the port, there may be an even simpler option.

One of our barcode scanners comes with software that just takes barcode data as it's scanned and pastes it into whatever open document has focus. If you're working in Notepad, the scanned data is pasted into Notepad. If you have a web app open, and the cursor in a text box, the data simply pastes in there.

It's a simpler option to implement, BUT it's harder on the users, because if they're not technical, they're going to call you wondering why the barcode beeps but doesn't populate the text box. (The answer will be "Because your cursor isn't in the text box or the form doesn't have focus")

So I go back to recommendation #1.


Your code just accesses the serial port of the server since it is running on the server...

IF you really need to access the serial port on the client from a web app then you will need to use some technology that runs directly on the client... this could be an ActiveX control embedded into your web page...

I am really not sure perhaps a Silverlight application embedded into your web page could achieve this too...

Beware that doing so in a web page is a possible security problem !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜