Ver Mensaje Individual
  #4  
Antiguo 06-04-2009
Avatar de delphi.com.ar
delphi.com.ar delphi.com.ar is offline
Federico Firenze
 
Registrado: may 2003
Ubicación: Buenos Aires, Argentina *
Posts: 5.932
Reputación: 27
delphi.com.ar Va por buen camino
Sigo viendo incoherente te código, entiendo que lo debes haber recortado de tu aplicación. Fijate el tratamiento que haces con la variable Result, primero que no se trata de una función sino de un procedure, luego le asignas un valor y posteriormente lo sobreescribes a True. Los parámetros de este procedimiento siguen siendo "String". Ademas, veo que en las funciones anteriores no se le da importancia al resultado de "AbirPuerto".

Hice un breve resumen de la DLL si cambiar las partes que no entiendo del código, y no me falla, salvo que no exista el puerto o este este abierto.
Código Delphi [-]
library dllcredicard;

uses
  SysUtils,
  Classes,
  Windows,
  Math;

var
  hComFile: THandle;

procedure CerrarPuerto;
begin
  CloseHandle(hComFile);
end;

function AbrirPuerto(Puerto, velocidad, paridad, tamano, parada: PChar): boolean;
const
  RxBufferSize = 256;
  TxBufferSize = 256;
var
  DCB: TDCB;
  Config: string;
  CommTimeouts: TCommTimeouts;
begin
  CerrarPuerto;

  hComFile := CreateFile(Puerto, GENERIC_READ or GENERIC_WRITE, 0,  nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,  0);

  Config := 'baud=' + velocidad + ' parity=' + paridad + 'data=' + tamano + ' stop=' + parada;

  with CommTimeouts do
  begin
    ReadIntervalTimeout         := $FFFFFFFF;
    ReadTotalTimeoutMultiplier  := 0;
    ReadTotalTimeoutConstant    := 1000;
    WriteTotalTimeoutMultiplier := 0;
    WriteTotalTimeoutConstant   := 1000;
  end;

  Result :=(hComFile <> INVALID_HANDLE_VALUE) and
            SetupComm(hComFile, RxBufferSize, TxBufferSize) and
            GetCommState(hComFile, DCB) and
            BuildCommDCB(@Config[1], DCB) and
            SetCommState(hComFile, DCB) and
            SetCommTimeouts(hComFile, CommTimeouts);
end;

procedure Banda;stdcall;
var
  Bytes: DWORD;
const
  Texto = #2'Z1'#3'h'#2'Z903'#3'c';
begin
  WriteFile(hComFile, Texto[1], Length(Texto), Bytes, nil);
end;

function leebanda(var Buff: PChar; Size: DWORD): DWORD; stdcall;
var
  sTmp: string;
  c1,rebut: integer;
  chBuffer: array[0..150] of char;
  NumberOfBytesRead: dword;
begin
  Result := 0;
  if AbrirPuerto('COM4','19200','N','8','2') then
    try
      Banda;
      repeat
        rebut:=0;
        repeat
          if hComFile=INVALID_HANDLE_VALUE then
            Exit;

          if not ReadFile(hComFile, chBuffer[rebut],1, NumberOfBytesRead, nil) then
          begin
            Result := INVALID_HANDLE_VALUE;

          end else
            for c1 := 0 to NumberOfBytesRead - 1 do
              sTmp:= sTmp + chBuffer[c1];

        until (rebut<100) and (Result = 0);

        if chBuffer[rebut]=chr(04) then
          CerrarPuerto;

      until (rebut=100) and (Result = 0);

      if Result = 0 then
      begin
        Result := Length(sTmp);
        StrLCopy(Buff, @sTmp[1], Min(Size, Result));
      end;
    finally
      CerrarPuerto;
    end;
end;

exports
  leebanda;

begin
end.
Cita:
Private Declare Function leebanda Lib "dllcredicard.dll" (ByRef Buff As String, Size As Long) As Long

Private Function DoLeeBanda() As String
Dim Buff As String * 255
Dim lLen As Long
lLen = leebanda(Buff, 255)
DoLeeBanda = Left$(Buff, lLen)
End Function

Private Sub boton_leer_banda_Click()
Caption = Now & ">" & DoLeeBanda & "<"
End Sub
Saludos!
__________________
delphi.com.ar

Dedique el tiempo suficiente para formular su pregunta si pretende que alguien dedique su tiempo en contestarla.
Responder Con Cita