• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1087
  • Last Modified:

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;
0
tomerlei
Asked:
tomerlei
  • 2
1 Solution
 
atul_parmarCommented:
I wonder why you are not using any comport component?
0
 
mokuleCommented:
Look there
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21009218.html

There is no need for commconfig.

Also Your timeout values seems too small
0
 
atul_parmarCommented:
Or You might missing to set one/few of the TDCB value.

You said that after setting it with Terminal it works fine. So compare the Comport state before and after you set it with Terminal.

e.g. After you start your PC.
1. Run your program and note the comport state. i.e. check the value of dcbPort after call to SetCommState(hCommFile,dcbport);
2. Run terminal and note the comport state. i.e. check the value of dcbPort after call to GetCommState(hCommFile,dcbPort);
3. Compare 1 and 2 and see what you are missing
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now