Pregunta

My requirement is as below:

My asp.net application has to connect to a serial port to get weights occasionally. But since its not possible to do this directly from ASP.net, I am using a WCF service.

How do I do this using WCF service? How do I get value back to the asp.net function? In serial port communication, the data is received in a separate thread.

using System.IO;
using System;
using System.IO.Ports;

namespace SerialComm
{
    public class FileReadService : IReadSerialComm
    {

        public void ReadSerialComm(string[] _params)
        {
            SerialPort spComm = new SerialPort(_params[0], Convert.ToInt32(_params[1]), Parity.None, Convert.ToInt32(_params[3]), StopBits.One);
            spComm.RtsEnable = false;
            spComm.DtrEnable = false;
            spComm.DataReceived += new SerialDataReceivedEventHandler(spComm_DataReceived);
            spComm.Open();
            spComm.Write(_params[5]);
        }

        void spComm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // data received event handler
        }     
    }  
}
¿Fue útil?

Solución

I'd approach this by having javascript in the browser calling the service on the local machine. You'd probably want to have some preliminary calls to ensure the service is there and ready, with messages to the user if there's a problem.

jQuery makes ajax very easy. You'll want to do some reading jquery.com. Grab the js library, and check out the ajax documentation.

Your code will look something like this (and this is completely untested)...

<script src="../Script/jquery-1.9.0.js" type="text/javascript"></script>
...
<script type="text/javascript">
function readSerialBuffer() {
    $.ajax({
       url: "http://localhost:12345/readSerialBuffer",
       success: function(data) {
          $(".serialdata").val(data);
       }
    });
}

</script>

<asp:Textbox runat="server" id="txtSerialData" class="serialdata" />
<input type="button" onclick="readserialbuffer();" />

Something like that. The idea is that the button click runs the ajax function to request the weight, and when that call succeeds, it loads the value returned into the textbox. I haven't tried running this... it's just off the top of my head.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top