I am already send data from C# to Arduino, and it's work perfect; but when I try to receive data on C# from sensors by event handler, it must keep port open to read any received data; so I can't send data via serial port any more.
How can I send and receive data at the same time between C# and Arduino?
This is my code to receive data:
public static SerialPort port=
new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
protected void DataReceived(object sender, SerialDataReceivedEventArgs e) {
SerialPort sp=(SerialPort)sender;
indata=sp.ReadExisting();
Functions f=new Functions();
if(indata!="") {
if(indata.Equals("bed_light_on\r\n")) {
f.update_flag("bed_light", 1);
}
}
}
protected void Page_Load(object sender, EventArgs e) {
indata="";
port.Close();
port.DataReceived+=new SerialDataReceivedEventHandler(DataReceived);
if(!port.IsOpen)
port.Open();
}