Jump to content
JDRenk

Comport Serial Port help

Recommended Posts

You can do the conversion using a record for example:

 

type
  RfloatBytes=packed record
    case boolean of
        true:(floatVal:single);
        false:(byteVals:array[0..3] of byte);
    end;

 

var

  x:  RfloatBytes;

 

begin

  x.byteVals[0]:=%00000000;

  x.bytevals[1]:=%00000000;

  x.bytevals[2]:=%01001000;

  x.bytevals[3]:=%01000001;

  ShowMessage(x.floatVal.ToString);


 

Or using an absolute keyword to share the memory (this is a Delphi 11 example for older versions you have to convert binary values into decimals or hex values):

 

var
 x: array[0..3] of byte;
 s: single absolute x;
begin
  x[0]:=%00000000;
  x[1]:=%00000000;
  x[2]:=%01001000;
  x[3]:=%01000001;
  ShowMessage(s.ToString);
end;
 

 

Share this post


Link to post

Thanks, but I don't quite understand how to apply that.  I'm new to this.

This is my procedure:

 

procedure TForm2.ComPort3RxChar(Sender: TObject);
var
  Buffer3 : array[0..14] of Byte;
  Result3: array[0..3] of byte;
    begin
      ComPort3.Read(@Buffer3,15);
      Move(Buffer3[7],Result3,4);
    end;

 

Result3 is the float32_t buffer

I am using Delphi 10.4 Community Edition

Share this post


Link to post
procedure TForm2.ComPort3RxChar(Sender: TObject);
var
  Buffer3 : array[0..14] of Byte;
  Result3: array[0..3] of byte;
  s: single absolute result3;
  lstr: string;
begin
  ComPort3.Read(@Buffer3,15);
  Move(Buffer3[7],Result3,4);
  lstr:=s.ToString;
end;

 

Share this post


Link to post
17 hours ago, JDRenk said:

Thanks, I now have that Com Port and sensor working OK.  Now I have another sensor and Com Port that are totally different.  This one returns a 4 byte float32_t value.  I have the 4 bytes in an array of bytes.  How would I convert that to a decimal string?

That would be the equivalent of a Delphi Single. Use the good old Move procedure to copy the 4 bytes to a Single variable and pass that to FormatFloat to get a string representation.

Share this post


Link to post
1 hour ago, PeterBelow said:

That would be the equivalent of a Delphi Single. Use the good old Move procedure to copy the 4 bytes to a Single variable and pass that to FormatFloat to get a string representation.

There is no need to copy the bytes using the absolute keyword the array and single can share the memory (like I posted).

Share this post


Link to post

Thanks again.  That code works in converting to a string, although I am getting what seem to be random memory errors.

Share this post


Link to post

All sensors are working and I'm logging data!  I would like to display live data trends and see TChart should do that, but I can't find it.  Is it not available in the Community Edition 10.4?

Share this post


Link to post
Quote

TChart should do that, but I can't find it. 

You should open a new message thread for that independent question.

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

×