wrong Delphi implementation of GetParam for CAN_PARAM_NETCLIENTS

Professional CAN and CAN FD Development Package for Windows®
Post Reply
dummzeuch
Posts: 4
Joined: Fri 5. Mar 2021, 15:54

wrong Delphi implementation of GetParam for CAN_PARAM_NETCLIENTS

Post by dummzeuch » Tue 6. Apr 2021, 18:36

The Delphi implementation for GetParam reading a can_param_string255_t looks like this:

Code: Select all

class function TCanApi4.GetParam(const device: can_device_t; const paramType: Word;
  const objClass: can_objclass_t; const objHandle: Longword; out value: AnsiString)
  : can_status_t;
var
  param: can_any_param_t;
begin
  param.base.size := SizeOf(can_param_string255_t);
  param.base.&type := paramType;
  param.base.objclass := Ord(objClass);
  param.base.objhandle := objHandle;
  Result := GetParam(device, param);
  if (Result = CAN_ERR_OK) then
    value := AnsiString(param.string255.value);
end;
This is fine for those GetParam calls that atually return a string value, but CAN_PARAM_NETCLIENTS returns an array of of 0 or 1 bytes:
Represents an array of values in range of [0, 1], that give information about the
clients connected to a net. The array index equals the handle of a client and the
value is a flag indicating connectivity (1) or not (0):
If clients[ i ] not equal to 0: Client 'i' is connected to the net.

Note: The first value of the array is always 0. For this reason, the attempt of
converting this array to a string ends in an empty string.
And as the Note says: Converting the value to a string will result in an empty string. So in this case we need a different implementation. I suggest the follwowing, which returns a TBytes (dynamic array of bytes, length set to 256 in the function).

Code: Select all

class function TCanApi4.GetParam(const device: can_device_t; const paramType: Word;
  const objclass: can_objclass_t; const objhandle: HCANOBJECT;
  out Value: TBytes): can_status_t;
var
  param: can_any_param_t;
  i: Integer;
begin
  param.base.size := SizeOf(can_param_string255_t);
  param.base.&type := paramType;
  param.base.objclass := Ord(objclass);
  param.base.objhandle := objhandle.Value;
  Result := GetParam(device, param);
  if (Result = CAN_ERR_OK) then begin
    // the next 3 lines are different
    SetLength(Value, CAN_PARAM_CONST_MAX_STRINGLEN);
    for i := 0 to CAN_PARAM_CONST_MAX_STRINGLEN - 1 do
      Value[i] := Byte(param.string255.Value[i]);
  end;
end;
This works for me.

K.Wolf
Software Development
Software Development
Posts: 124
Joined: Wed 22. Sep 2010, 15:37

Re: wrong Delphi implementation of GetParam for CAN_PARAM_NETCLIENTS

Post by K.Wolf » Wed 7. Apr 2021, 11:43

Thank you for this suggestion, we will add the appropriate overload to CanApi4.pas.

Post Reply