Jump to content
Miro Hlozka

TcustomComPort.Write(void type, integer) first parameter

Recommended Posts

I'm switching to delphi from c++ builder. I am rewriting part of the application from C++ to Delphi.

I use the CPortLib component for serial communication there the write and read function worked for me in C++ without problems.

The pointer to the char array pointed to the data. for delphi the function is defined as:

TcustomComPort.Write(void type, integer)

function Write(Const Buffer, Count integer)

TcustomComPort.Reade(void type@, integer)

function Read(var Buffer; Count: Integer): Integer;

The write / read functions give incorrect data.

Can you advise how to enter the first parameter for the read / write function in Delphi?

PS: if I use WriteLn, ReadLn and enter the data in a string, it works fine. Thank you for your response 🙂

Share this post


Link to post
var
//  MyVar:string;
//  or
 MyVar:array of byte;
begin
SetLength( MyVar, 100) ; // if byte
//
READ( myVar[0], 100); // first var pos
// READ( myVar, 100); // pointer pos

 

try this gihub

https://github.com/CWBudde/ComPort-Library

https://github.com/CWBudde/ComPort-Library/blob/master/Source/CPort.pas  = TCustomComPort

 

SAMPLES: https://github.com/CWBudde/ComPort-Library/tree/master/Examples/ComExample

Edited by programmerdelphi2k

Share this post


Link to post
22 hours ago, Miro Hlozka said:

TcustomComPort.Write(void type, integer)

TcustomComPort.Reade(void type@, integer)

Those are not valid Delphi declarations.

Quote

function Write(Const Buffer, Count integer)
function Read(var Buffer; Count: Integer): Integer;

Those are valid Delphi declarations (well, almost - Write() needs a semicolon instead of a comma between Buffer and Count).

Quote

The write / read functions give incorrect data.

They work the same as they do in C++.  Most likely, you are simply passing in the wrong values, so you end up reading/writing data from the wrong memory addresses.  Hard to say since you did not show the code you are having trouble with.

Quote

Can you advise how to enter the first parameter for the read / write function in Delphi?

In Delphi, an untyped 'const'/`var' parameter is just a reference to a memory address, so you can pass in whatever variable you want and the compiler will pass in the memory address of that variable.  In C++, the closest equivalent to that is a '(const) void*' pointer, using the '&' operator to get the address of a variable.

 

So, in Delphi, to read/write data from/to an array, for instance, you would pass in the 1st element of the array, and the compiler will pass in the memory address of that element.  On the other hand, if you have a pointer to memory, you would have to dereference the pointer so the parameter can receive the memory address of the thing being pointed at.

 

Here is a few examples using different types of memory:

var
  Value: Integer;

ComPort.Read(Value, SizeOf(Value));
ComPort.Write(Value, SizeOf(Value));
var
  Buffer: array[0..255] of Byte;

ComPort.Read(Buffer[0], SizeOf(Buffer));
// or: ComPort.Read(Buffer, SizeOf(Buffer));
ComPort.Write(Buffer[0], SizeOf(Buffer));
// or: ComPort.Write(Buffer, SizeOf(Buffer));
var
  Buffer: array of Byte;

SetLength(Buffer, ...);
ComPort.Read(Buffer[0], Length(Buffer));
// or: ComPort.Read(PByte(Buffer)^, Length(Buffer));
ComPort.Write(Buffer[0], Length(Buffer));
// or: ComPort.Write(PByte(Buffer)^, Length(Buffer));
var
  Str: AnsiString;

SetLength(Str, ...);
ComPort.Read(Str[0], Length(Str));
// or: ComPort.Read(PAnsiChar(Str)^, Length(Str));
ComPort.Write(Str[0], Length(Str));
// or: ComPort.Write(PAnsiChar(Str)^, Length(Str));
var
  UStr: UnicodeString;

SetLength(UStr, ...);
ComPort.Read(UStr[0], Length(UStr) * SizeOf(WideChar));
// or: ComPort.Read(PWideChar(Str)^, Length(UStr) * SizeOf(WideChar));
ComPort.Write(UStr[0], Length(UStr) * SizeOf(WideChar));
// or: ComPort.Write(PWideChar(Str)^, Length(UStr) * SizeOf(WideChar));
type
  PMyRec = ^TMyRec;
  TMyRec = record
    Value: Integer;
    ...
  end;

var
  Ptr: PMyRec;
  NumBytes: Integer;

NumBytes := SizeOf(TMyRec) * ...;
GetMem(Ptr, NumBytes);
ComPort.Read(Ptr^, NumBytes);
ComPort.Write(Ptr^, NumBytes);
FreeMem(Ptr);
Quote

PS: if I use WriteLn, ReadLn and enter the data in a string, it works fine.

Those are compiler intrinsic functions, they have special handling for parameters that you don't normally get with user-defined functions.

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×