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):
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:
public ActionResult DisplayData(string text)
{
_serialPort = new SerialPort("COM1", 2400, Parity.Even, 7, StopBits.One);
_serialPort.Close();
_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.