TCustomComPort.WriteStrAsync mistake!?

2011-12-26
2013-04-30
  • shusaku suda

    shusaku suda - 2011-12-26

    Function TCustomComPort.WriteStrAsync mistake!?

    Are not properly convert a byte array.

    example code.

    var
      S: AnsiString;
      B: TBytes;
      ...
    begin
      ...
      SetLength(B, 10);
      B[0] := $00;
      B[1] := $00;
      B[2] := $FF;
      B[3] := $03;
      B[4] := $FD;
      B[5] := $00;
      B[6] := $FF;
      B[7] := $FF;
      B[8] := $02;
      B[9] := $00;
      S := StringOf(B);
      ComPort.WriteStr(StringOf(B));
    

    Correct results.

    00 00 FF 03 FD 00 FF FF 02 00
    

    Results incorrect.

    00 00 F3 03 F1 00 F3 F3 02 00
    

    That returns the correct result of the following codes.

    function TCustomComPort.WriteStrAsync(var Str: string; var AsyncPtr: PAsync): Integer;
    var
      sa : Ansistring;
      i: integer;
      B: TBytes;
    begin
      if Length(Str) > 0 then
      begin
        setlength(sa,length(str));
        {$IFDEF Unicode}
        if length(sa)>0 then
        begin
          {$IFDEF VER220}
          SetLength(B, length(str));
          B := BytesOf(str);
          sa := StringOf(B);
          {$ELSE}
          for i := 1 to length(str) do
            sa[i] := ansichar(byte(str[i]));
          {$ENDIF}
          move(sa[1],str[1],length(sa));
        end;
        {$ENDIF}
        Result := WriteAsync(Str[1], Length(Str), AsyncPtr)
      end
      else
        Result := 0;
    end;
    
     
  • Brian Gochnauer

    Brian Gochnauer - 2011-12-26

    Your into typical string conversion issues
    Use  Write instead

    example code.

    var
     S: AnsiString;
     B: TBytes;
     ...
    begin
     ...
     SetLength(B, 10);
     B[0] := $00;
     B[1] := $00;
     B[2] := $FF;
     B[3] := $03;
     B[4] := $FD;
     B[5] := $00;
     B[6] := $FF;
     B[7] := $FF;
     B[8] := $02;
     B[9] := $00;
     ComPort.Write(B);
    
     
  • shusaku suda

    shusaku suda - 2011-12-26

    Windows7(x86) / Delphi XE(SP1) / ComPort Library(4.11f)

      SetLength(B, 10);
      B[0] := $00;
      B[1] := $00;
      B[2] := $FF;
      B[3] := $03;
      B[4] := $FD;
      B[5] := $00;
      B[6] := $FF;
      B[7] := $FF;
      B[8] := $02;
      B[9] := $00;
      ComPort.Write(B, 10);
    

    Portmon.exe
    Execution result.

    0.00003150  Project1.exe    IRP_MJ_WRITE    Serial0 SUCCESS Length 10: 78 52 53 01 10 69 58 01 00 00    
    0.00000000  Project1.exe    IOCTL_SERIAL_WAIT_ON_MASK   Serial0
    
     
  • Brian Gochnauer

    Brian Gochnauer - 2011-12-26

    try

       ComPort.Write(B[0], 10);
    
     
  • shusaku suda

    shusaku suda - 2011-12-26

    Has been resolved.
    Thank you.

     

Log in to post a comment.