Click here to Skip to main content
15,887,929 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, im trying to add the result of COM port device in an HTML view, using ASP.NET framework, this code is for test:

I have a global variable called result.

I have a serial port data receive method(this method runs when the device display the data):
C#
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        int dataLength = _serialPort.BytesToRead;
        byte[] data = new byte[dataLength];
        int nbrDataRead = _serialPort.Read(data, 0, dataLength);
        if (nbrDataRead == 0)
            return;

        result = Encoding.ASCII.GetString(data);

        _serialPort.Close();

        DisplayData(result);
    }


and i have my action result method, this method returns a view:

C#
public ActionResult DisplayData(string text)
{
  _serialPort = new SerialPort("COM1", 2400, Parity.Even, 7, StopBits.One);
  _serialPort.Close();
  // Subscribe to event and open serial port for data
  _serialPort.DataReceived += new 
  SerialDataReceivedEventHandler(_serialPort_DataReceived);
  _serialPort.Open();
  ViewBag.Message = text;
  return View();
}

I want to display the text from the _serialPort_DataReceived method, but i dont know how to do that.

Im in the correct way?

Thanks in advance for the help

What I have tried:

I tried with this code but when _serialPort_DataReceived goest to DisplayData(), it goes with the current data, but it doesnt return the view.
Posted
Comments
F-ES Sitecore 11-Aug-17 11:37am    
Two things...first your .net code runs on the server, it has no access to client hardware so this only looks like it is working when developing on your local machine as the client and server are the same machine. When you publish this site it will stop working.

Second asp.net is a request\response technology...it sends html to the browser in response to a request from the browser. It can't push content onto the client though so you can't have code running from some event triggered server-side write data to the client.

Basically you're using the wrong technology this, you won't get it to work. Look at developing a Windows forms app instead.
jnsanchezg 11-Aug-17 11:48am    
Thanks for your answer, i have another project and im using an ApiController, i have a IHttpActionResult that returns data from a device who returns weight, i can catch this data and returns like a json format: {"result" : "12 kg"}, i can do this because the hardware is always returning a value,is constant, and i dont use the _serialPort_DataReceived function, but i have another device, this one returns data only when i push a button, for this second device i use _serialPort_DataReceived function and when i debug this app, when i push the device button, automatically it goes to the _serialPort_DataReceived method, it comes with the data, ok the problem is that i dont know how display this data in the json result data.
Karthik_Mahalingam 16-Aug-17 3:37am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
j snooze 11-Aug-17 17:15pm    
So the issue is not that the data is not getting pushed to your page, its that you want our result in a json format? why not instead of an actionresult return a JsonResult instead?...I'm not sure if you need the newtonsoft nuget package to do that or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900