使用DataReceived事件处理程序读取SerialPort缓冲区

时间:2018-01-11 14:15:34

标签: c# delphi serial-port

我来自Delphi,代码如下:

// 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;

我想在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 ???
}

我不知道如何从SerialDataReceivedEventArgs获取数据并将其复制到我的字节数组byteReadData。

这是我写给串口的内容:https://i.imgur.com/x8oK5PI.png 这是我应该从串口读取的内容,但它失败了:https://i.imgur.com/WQNhmqa.png

0 个答案:

没有答案