• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 

nrComm SendString and Delphi XE2

Started by gmcouto76, July 30, 2012, 02:40:31 pm

Previous topic - Next topic

gmcouto76

I used nrCommLib version 9.03 and Delphi 2009 to develop a serial communication with a chemichal machine using the code below.

I bought a upgrade to 9.30 version and build the same code with Delphi XE2 and the application is not working to send messages anymore.

The machine said there is a "protocol error" and the nrlogFile like that:

        nrComm: Try to send string \01h\0Ah52 FIT              09\0Ah\02h\0Ah\03h\0Ah\04h\0Ah ...

Has anything change on method SENDSTRING?

Is AnsiString function not enought to convert the data to the right bytes to send?

Here is the code:

const
  SOH = #1;
  STX = #2;
  ETX = #3;
  EOT = #4;
  LF = #10;
  CR = #13;
  SPACE = #32;



function MontarCabecalhoBloco(TipoBloco: integer): string;
var
  CodigoAparelho, IdentificacaoAparelho: String;
begin
  CodigoAparelho:= '52';
  if (Length(IdHost) <= 16) then IdentificacaoAparelho:= Padr(IdHost,16,' ')
    else IdHost:= Copy(IdHost,0,16);

  result:= CodigoAparelho + SPACE + IdentificacaoAparelho + SPACE +
    Zeros(IntToStr(TipoBloco),2);
end;

function MontarBloco(CabecalhoBloco, Dados: String): String;
var
  Bloco: String;
begin
  // Start of Heading
  Bloco:= SOH + LF;

  // Block Heading
  Bloco:= Bloco + CabecalhoBloco + LF;

  // Start of Text
  Bloco:= Bloco + STX + LF;

  // Data
  if (Trim(Dados) <> '')
    then Bloco:= Bloco + Dados + LF;

  // End of Text
  Bloco:= Bloco + ETX + LF;

  // End of Transmission
  Bloco:= Bloco + EOT + LF;

  result:= Bloco;
end;

begin
  nrComm1.SendString(AnsiString(MontarBloco(MontarCabecalhoBloco(9), '')));
end;

Best Regards.

Guilherme Couto

Roman Novgorodov

Hello

XE2 uses UNICODE string and chars by default.
Please try to change String types to AnsiString types and Char on AnsiChar in your routines MontarCabecalhoBloco()  MontarBloco()

Roman Novgorodov
DeepSoftware llc
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.