0

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.stack.imgur.com/EfLI1.png This is what I should be reading from serial port but it fails: https://i.stack.imgur.com/PllQi.png

ThorPowerx
  • 3
  • 1
  • 5
  • You get the best responses on SO showing you have made some effort yourself. A starting point may be to look at the [SerialPort class](https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx) documentation - specifically the Read methods - you will find examples there. Or look for a tutorial like this top of list with googling ".net serialport tutorial" : http://www.instructables.com/id/Serial-Port-Programming-With-NET/ – PaulF Jan 11 '18 at 14:20
  • I usually look on MSDN website for documentation and some examples, but couldn't find any example for Class SerialDataReceivedEventArgs. – ThorPowerx Jan 11 '18 at 14:26
  • If the data to be received is only text, it is OK with ReadExisting(). However, if you receive binary data, you can read the size of the data in the receive buffer with the BytesToRead property, specify it as the data size, specify the byte array as the receive area, and call the Read() method. – kunif Jan 11 '18 at 14:46
  • @kunif I'm actually reading binary data. My problem is that I don't understand where are the equivalent "Buffer: Pointer" and "BufferLength: Word" in my serialPort1_DataReceived event parameters. Could you please write me your answer refered to my code? Currently it writes nothing, so there must be something wrong in it (my indata gets printed empty). – ThorPowerx Jan 11 '18 at 15:06
  • 2
    Possible duplicate of [How do I use dataReceived event of the SerialPort Port Object in C#?](https://stackoverflow.com/questions/466474/how-do-i-use-datareceived-event-of-the-serialport-port-object-in-c) – whosrdaddy Jan 11 '18 at 15:39
  • "Buffer: Pointer" and "BufferLength: Word" are the address and size of the buffer prepared by the caller of Delphi's CommPortReceiveData() and are not directly related to the contents of C#. "Buffer: Pointer" is an address to store the read data, which is a combination of the first parameter and the second parameter of the C# Read() method. In the C# code of the question, the byteReadData variable and the starting index in it(usually 0). "BufferLength: Word" is the size of the byteReadData variable(290), it is not used in the Read() method. – kunif Jan 11 '18 at 15:48
  • @whosrdaddy I copied the code from that link: https://i.imgur.com/fWbSxnu.png my expected output byte sequence is different tho: https://i.imgur.com/WQNhmqa.png – ThorPowerx Jan 11 '18 at 15:51
  • No it is the same, the IDE shows you decimal values while your monitoring tool is showing hex values.... – whosrdaddy Jan 11 '18 at 15:53
  • Oh yeah, I didn't notice. What if I want to display them in hexa format tho? – ThorPowerx Jan 11 '18 at 15:57
  • Please learn to search on this site ;) https://stackoverflow.com/a/3354508/800214 – whosrdaddy Jan 11 '18 at 16:09
  • I found it: BitConverter.ToString, sure your link didn't help. – ThorPowerx Jan 11 '18 at 16:50

0 Answers0