Reputation: 559
I am trying to read/write some data using serial port,
byte[] txTestData = new byte[] { 0x03, 0x0, 0x01, 0x0};
Serial.Write(txTestData, 0, txTestData.Length);
But I want to read the data only when i receive data from serial port. ,i.e
if(Data received from serial port)
{
byte[] rxTestResponse = new byte[2];
Serial.Read(rxTestResponse, 0, 2);
}
Is there any API that tells me the data received event ??
Thanks, Dattatarya
Upvotes: 1
Views: 7319
Reputation: 23117
Yes, there is an API which can tell you when data is received, it's called events. Read more about events here.
Attach event:
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Create handler
private static void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
{
byte[] rxTestResponse = new byte[2];
Serial.Read(rxTestResponse, 0, 2);
}
Every time SerialPort receives data it will call DataReceived
event and your event handler will execute
Upvotes: 4