First of all, is this the right place to put a question about Serial 
comunication? Well, let me explain about my problem...

I have done a program that communicate using serial COM 1,2,3 and 4 with 
various amplifiers, but I'm with a problem: With the computer reseted (just 
turned on) I open, setup COM and use my program - the readfile dont work. The 
writefile say that is ok, bute the amplifiers dont receive a thing. So I use a 
small C++ builder program that open and setup the COMs, send something and 
closes, and, after that, my program works forever... the strange thing is that 
my delphi program do exactly the same as C++ program do, anyway, it only works 
AFTER I first run C++ program.
There goes my routines, just like I found on internet:


function TXComPort.Open: boolean;
var
  Buffer: ^TCommConfig;
  s: string;
  dcbCommPort: DCB;
  size: cardinal;
  TimeOutBuffer: ^TCommTimeouts;
begin
  if Opened then begin
    raise Exception.Create('Porta '+Config.Port+' já está aberta');
  end;
  Handle:=CreateFile(PChar(Config.Port),
                     GENERIC_READ + GENERIC_WRITE,
                     0,
                     nil,
                     OPEN_EXISTING,
                     0,
                     0);

  IOpened:=Handle<>INVALID_HANDLE_VALUE;
  if IOpened then begin
    {Allocate a temporary buffer}
    GetMem(Buffer, sizeof(TCommConfig));
    {Get the size of the CommConfig structure}
    {as it may be different than documented}
    size := 0;
    GetCommConfig(Handle, Buffer^, size);
    {Free the temporary buffer}
    FreeMem(Buffer, sizeof(TCommConfig));
    {Allocate the CommConfig structure}
    GetMem(Buffer, size);
    GetCommConfig(Handle, Buffer^, size);
    {Change the baud rate}
    Buffer^.dcb.BaudRate := Config.BaudRate;
    Buffer^.dcb.ByteSize := Config.ByteSize;
    Buffer^.dcb.Parity   := XChoose(Config.Parity,1,0);
    Buffer^.dcb.StopBits := Config.StopBits;
    {Set the comm port to the new configuration}
    SetCommConfig(Handle, Buffer^, size);
    {Free the buffer}
    FreeMem(Buffer, size);
    // configura timeout
    GetMem(TimeoutBuffer, sizeof(COMMTIMEOUTS));
    GetCommTimeouts (Handle, TimeoutBuffer^);
    TimeoutBuffer.ReadIntervalTimeout        := 300;
    TimeoutBuffer.ReadTotalTimeoutMultiplier := 300;
    TimeoutBuffer.ReadTotalTimeoutConstant   := 300;
    SetCommTimeouts (Handle, TimeoutBuffer^);
    FreeMem(TimeoutBuffer, sizeof(COMMTIMEOUTS));
    //
    dcbCommPort.DCBlength := sizeof(DCB);
    GetCommState(Handle, dcbCommPort);
    s:='56000,n,8,1';
    BuildCommDCB(@s, dcbCommPort);
    SetCommState(Handle, dcbCommPort);


  end;

  Result:=IOpened;
end;


function TXComPort.Read(count: byte): boolean;
var
  r: cardinal;
begin
  Result:=ReadFile(Handle, FBuffer[0], count, r, nil);
end;

function TXComPort.Write(count: byte): boolean;
var
  w: cardinal;
begin
  Result:=Boolean(WriteFile(Handle,FBuffer[0],count,w,nil));
end;


Thanks
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to