我来自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