Link to home
Start Free TrialLog in
Avatar of tomerlei
tomerlei

asked on

Com1 Port Problem

Hello,
I wrote a program that uses the com port, the com device i'm working with is working on 9600 baud rate.
every time i restart the computer my program fails to communicate with the device.
To make it work i close my program, run the Hyperterminal and creating a com1 connection setting it to 9600 baud rate, closing the hyperterminal and running my program.

i will appreciate any help, because im already desperate.
this is the com port initialization procedure:

var
   TimeoutBuffer: PCOMMTIMEOUTS;
   Buffer : PCommConfig;
   size : DWORD;
   dcbPort: TDCB;
   hCommFile: THandle;
   CommPort: string;
begin
  CommPort := 'COM1';
  hCommFile := CreateFile(PChar(CommPort),
                          GENERIC_WRITE or GENERIC_READ,
                          0,
                          nil,
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          0);
  if hCommFile=INVALID_HANDLE_VALUE then //Com Port Could not be reached
  begin
    showmessage('Error');

    Exit;
  end
  else
  begin
   //Sets communication timeout
   GetMem(TimeoutBuffer, sizeof(COMMTIMEOUTS));
   GetCommTimeouts (hCommFile, TimeoutBuffer^);
    TimeoutBuffer.ReadIntervalTimeout        := 1;
    TimeoutBuffer.ReadTotalTimeoutMultiplier := 1;
    TimeoutBuffer.ReadTotalTimeoutConstant   := 1;
   SetCommTimeouts (hCommFile, TimeoutBuffer^);
   FreeMem(TimeoutBuffer, sizeof(COMMTIMEOUTS));

   //Sets Communications Properties

 {Allocate the CommConfig structure}
  GetMem(Buffer, sizeof(TCommConfig));
  GetCommConfig(hCommFile, Buffer^, size);

 {Change the baud rate}
  Buffer.dcb.BaudRate := 9600;

 {Set the comm port to the new configuration}
  SetCommConfig(hCommFile, Buffer^, size);

 {Free the buffer}
  FreeMem(Buffer, size);

 {Set Comm State}
  GetCommState(hCommFile,dcbPort);
  dcbPort.BaudRate:=9600;
  dcbPort.XonChar:= char(17);
  dcbPort.XoffChar:= char(19);
  SetCommState(hCommFile,dcbport);

end;
Avatar of atul_parmar
atul_parmar
Flag of India image

I wonder why you are not using any comport component?
Look there
https://www.experts-exchange.com/questions/21009218/89C52-Serial-Port-PC.html

There is no need for commconfig.

Also Your timeout values seems too small
ASKER CERTIFIED SOLUTION
Avatar of atul_parmar
atul_parmar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial