Recent

Author Topic: TLazSerial: reading and displaying the byte value instead of the characters  (Read 237 times)

GeneCode

  • New Member
  • *
  • Posts: 19
  • What is the cost of lies?
I am trying to read some bytes from serial port.
Using TLazSerial readData, I managed to output the data into Memo like this:

Quote
"3DUfw
????
"3DUfw
????

On the other machine, I just sent "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee" bytes.
How do I format the data I get into same values?

My code:

Code: [Select]
Str :=  mySerial.ReadData;
« Last Edit: October 03, 2019, 01:43:17 am by GeneCode »
Windows 10
Lazarus 1.8.4

PaulRowntree

  • Jr. Member
  • **
  • Posts: 64
    • Paul Rowntree
Have you implemented the OnRcv events that the underlying code triggers when some bytes are received? 
Are you wanting to display the hex codes for the incoming character stream?
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

GeneCode

  • New Member
  • *
  • Posts: 19
  • What is the cost of lies?
Have you implemented the OnRcv events that the underlying code triggers when some bytes are received? 
Are you wanting to display the hex codes for the incoming character stream?
Sorry. Actually I have solved this just now. Here is my code in case others were looking for a solution.
Code: Pascal  [Select]
  1.  
  2. function TTestComForm.GetByteString(str:string): string;
  3. var
  4.  i: integer;
  5.  s: string;
  6. begin
  7.    for i:= 1 to Length(str) do begin
  8.        s:= s +' '+ IntToHex(Ord(str[i]),2);
  9.    end;
  10.    GetByteString := s;
  11. end;
  12.  
  13. procedure TTestComForm.mySerialRxData(Sender: TObject);
  14. var
  15.   Str: string;
  16. begin
  17.      if (mySerial.DataAvailable) then begin
  18.            Str :=  mySerial.ReadData;
  19.            if mySerial.SynSer.LastError = 0 then begin
  20.               LogMemo.Lines.Add(GetByteString(Str));  
  21.            end;
  22.      end;
  23. end;
Windows 10
Lazarus 1.8.4