Ir ao conteúdo
  • Cadastre-se

Alan santos Da silva

Membro Júnior
  • Posts

    1
  • Cadastrado em

  • Última visita

posts postados por Alan santos Da silva

  1. procedure TFrmGerenciador.SERVERExecute(AContext: TIdContext);
    var
    byteBuf : TIdBytes;
    Buffer : array [1..1024] of byte;
    BufferSec : array [1..1024] of byte;
    hexComado : string;
    i : integer;
    rx, tx : TRxTx;
    tamStr : integer;
    sComandoSec : string;
    begin
       try
            FillChar(Buffer, SizeOf(Buffer), 0);
            AContext.Connection.IOHandler.ReadBytes(byteBuf, 12, false);
            idGlobal.BytesToRaw(byteBuf, Buffer, 12);
            hexComado := '';
            for i := 1 to 12 do
              begin
                hexComado := hexComado + IntToHex(Ord(Buffer[i]), 2);
              end;
     
            rx := strToRxTx(hexComado);
     
            if(rx.tamanho > 0) then
             begin
              FillChar(BufferSec, SizeOf(BufferSec), 0);
              AContext.Connection.IOHandler.ReadBytes(byteBuf, rx.tamanho + 1, false);
              idGlobal.BytesToRaw(byteBuf, BufferSec,  rx.tamanho + 1);
     
               sComandoSec := '';
               for i := 1 to rx.tamanho + 1 do
                begin
                  sComandoSec := sComandoSec + IntToHex(Ord(BufferSec[i]), 2);
                end;
                rx.dados := RetZero(sComandoSec, 1024, false);
             end;
            tx := trataComandoPlaca(rx); // AQUI TRATA O COMANDO RECEBIDO DA CONTROLADORA
     
            hexComado := RxTxtoStr(tx);
            tamStr := length(hexComado) div 2;
            FillChar(Buffer, SizeOf(Buffer), 0);
            for i := 1 to tamStr do
              begin
                Buffer [i + 1] := HexToDec(hexComado[(i*2)+1] + hexComado[(i*2)+2]);
              end;
            byteBuf := Idglobal.RawToBytes(Buffer, tamStr);
            AContext.Connection.IOHandler.Write(byteBuf);
     
          except on e:Exception do
           begin
                   AContext.Connection.DisconnectNotifyPeer;
           end;
       end;
    end;
    function TFrmGerenciador.enviarComando(TX: TRxTx; resposta: Boolean): TRxTx;
    var
    hexComado, sComandoSec : WideString;
    buf, rxBuf : array [1..1024] of byte;
    bufSec : array [1..1024] of byte;
    tamStr, i,M : integer;
    rx : TRxTx;
    byteBuf  : TIdBytes;
     begin
             for m := 1 to 2  do
           begin
            sockets[m] :=TidTCPClient.Create;
            sockets[M].Disconnect;
            sockets[M].DisconnectNotifyPeer;
            sockets[M].Host := ListaDeIps.Strings[m];
            sockets[M].Port := 9760;
            sockets[M].ConnectTimeout := -1;
            sockets[M].IPVersion := Id_IPv4;
            sockets[M].ReadTimeout := -1;
            sockets[M].ReuseSocket := rsFalse;
            sockets[M].Connect;
     
     
         if(not sockets[m].Connected)  then
       //if(not tcp.Connected)  then
       begin
         try
           sockets[m].connect();
          //tcp.Connect();
         except
          exit;
         end;
       end;
     
     
         if(not sockets[m].Connected) then
      //if(not tcp.Connected) then
       exit;
       rx.stx := 0;
       rx.tamanho := 0;
       rx.id_destino := 0;
       rx.id_sub_destino := 0;
       rx.id_fonte := 0;
       rx.comando := '0000';
       rx.dados := '';
       rx.chksum := 0;
       rx.etx := 0;
     
     try
     
      FillChar(buf, SizeOf(buf), 0);
     // Comunicando := true;
      //tx.chksum := calculaCK(tx);
      hexComado := RxTxtoStr(tx);
      tamStr := length(hexComado) div 2;
      for i := 0 to tamStr - 1 do
       begin
          buf [i + 1] := HexToDec(hexComado[(i*2)+1] + hexComado[(i*2)+2]);
     
       end;
      if(tx.tamanho > 0) then
       begin
     
        byteBuf := Idglobal.RawToBytes(buf, 13 + tx.tamanho);
        //tcp.IOHandler.Write(byteBuf);
        Sockets[m].IOHandler.Write(byteBuf);
     
       end
      else
       begin
        byteBuf := Idglobal.RawToBytes(buf,12);
       // tcp.IOHandler.Write(byteBuf);
        Sockets[m].IOHandler.Write(byteBuf);
       end;
     
      if(resposta = true) then
       begin
     
        //tcp.IOHandler.ReadTimeout := 2000;
        //tcp.IOHandler.ReadBytes(byteBuf, 12, false);
        Sockets[m].IOHandler.ReadTimeout := 2000;
        Sockets[m].IOHandler.ReadBytes(byteBuf, 12, false);
        idGlobal.BytesToRaw(byteBuf, rxBuf, 12);
     
        hexComado := '';
        for i := 1 to 12 do
         begin
           hexComado := hexComado + IntToHex(Ord(rxBuf[i]), 2);
         end;
     
         rx := strToRxTx(hexComado);
         rx.id_fonte := 0;
     
         if(rx.tamanho > 0) then
          begin
            FillChar(bufSec, SizeOf(bufSec), 0);
           // tcp.IOHandler.ReadTimeout := 2000;
            //tcp.IOHandler.ReadBytes(byteBuf, rx.tamanho +1, false);
            Sockets[m].IOHandler.ReadTimeout := 2000;
            Sockets[m].IOHandler.ReadBytes(byteBuf, rx.tamanho +1, false);
     
            idGlobal.BytesToRaw(byteBuf, bufSec, rx.tamanho +1);
             sComandoSec := '';
             for i := 1 to rx.tamanho +1 do
              begin
                sComandoSec := sComandoSec + IntToHex(Ord(bufSec[i]), 2);
              end;
              rx.dados := RetZero(sComandoSec, 1024, false);
     
          end;
         Result := rx;
     
       end;
       except on e:Exception do
        begin
          rx.stx := 0;
          rx.tamanho := 0;
          rx.id_destino := 0;
          rx.id_sub_destino := 0;
          rx.id_fonte := 0;
          rx.comando := '0000';
          rx.dados := '';
          rx.chksum := 0;
          rx.etx := 0;
          Result := rx;
          //tcp.Disconnect;
          Sockets[m].Disconnect;
        end;
     end;
    end;
    end;

    Pessoal estou com um problema,onde o meu TcpServer Esta enviando a resposta para todos os Tcpclient Que estão conectados. 

    Como faço para o TcpServer enviar a resposta somete  para o tcpClient que enviou o comando,resumindo o Cliente que mandar o comando so ele receber a resposta do servidor.

    esses são os códigos que estou usando,alguém pode me ajudar onde estou errando aqui ?

    • Curtir 1

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!