Home Read SerialPort buffer using DataReceived event handler
Reply: 0

Read SerialPort buffer using DataReceived event handler

user1037
1#
user1037 Published in April 22, 2018, 5:00 am

I come from Delphi with the following code:

// COM port received data processing
procedure TMainForm.CommPortReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
  redata: array[0..290] of byte;
begin
  move(Buffer^, pchar(@redata)^, BufferLength); // copy data from COM buffer
  end;
end;

And I want to do the same thing in C#:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    byte[] byteReadData = new byte[290];
    serialPort1 = (SerialPort)sender;
    string indata = serialPort1.ReadExisting();
    Console.WriteLine("Data Received:");
    Console.Write(indata);
    // copy data from COM buffer ???
}

I don't know how to take the data from SerialDataReceivedEventArgs and copy it to my byte array byteReadData.

This is what I write to serial port: https://i.imgur.com/x8oK5PI.png This is what I should be reading from serial port but it fails: https://i.imgur.com/WQNhmqa.png

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305623 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO