CleverON
CleverON

Reputation: 11

TCommPortDriver error OnReceiveData Delphi 11

I use TCommPortDriver component in Delphi 7, its very cool for serial communication.

But in Delphi 11 the receivedata is "strange", for example:

Baud Rate 19200, Parity 1, databits 8, hwflow none

in Delphi 7 "22.02.22 08:25:38 02 11 45991883904 * 0.00"

in Delphi 11 "計ࢊ 葂𒀈절쐩䫾⩉䡊䭋ࢇࡈ䈨ࢄࠈ䩊萋ࠈ⠈葂蠈ࠈࠈࢄࠈ䈨䦤赊䫊樈จ䠊ࠈࠈࠈࠈ䈡ࢄ࢈計ࠌࢊ℈葂ࠈ절쐈þ"

I download the last version, then I instaled the old version in Delphi Alexandria, I make a lot of tests and the result is always the same.

The procedure is same in two versions, a lithe example:

procedure TForm1.SerialTarifadorReceiveData(Sender: TObject; DataPtr: Pointer;
  DataSize: Cardinal);
var
    s: string;
begin
  s := StringOfChar( ' ', DataSize );
  move( DataPtr^, system.pchar(s)^, DataSize );
  ShowMessage(s);
end;

I belive the problem is in DataPtr.

Please Help Me, Thanks,

Upvotes: 1

Views: 864

Answers (2)

Uwe Raabe
Uwe Raabe

Reputation: 47819

Another approach with a bit more flexibility about the encoding is:

var
  reader: TStreamReader;
  s: string;
begin
  reader := TStreamReader.Create(TPointerStream.Create(DataPtr, DataSize, True), TEncoding.ASCII);
  try
    reader.OwnStream;
    s := reader.ReadToEnd;
    ShowMessage(s);
  finally
    reader.Free;
  end;
end;

Upvotes: 1

Rob Lambden
Rob Lambden

Reputation: 2293

The event you receive is passing you a pointer to a buffer containing the data (bytes) you have received.

You then copy this data, byte by byte, into a string. In Delphi 11 the string type is for two byte characters so each character is now two bytes of your input.

Use a RawByteString to have single byte characters (or UTF8String if you know that the data is a UTF8 stream).

Upvotes: 1

Related Questions