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_V ALUE 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.ReadInterval Timeout := 1;
TimeoutBuffer.ReadTotalTim eoutMultip lier := 1;
TimeoutBuffer.ReadTotalTim eoutConsta nt := 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,dcb Port);
dcbPort.BaudRate:=9600;
dcbPort.XonChar:= char(17);
dcbPort.XoffChar:= char(19);
SetCommState(hCommFile,dcb port);
end;
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_V
begin
showmessage('Error');
Exit;
end
else
begin
//Sets communication timeout
GetMem(TimeoutBuffer, sizeof(COMMTIMEOUTS));
GetCommTimeouts (hCommFile, TimeoutBuffer^);
TimeoutBuffer.ReadInterval
TimeoutBuffer.ReadTotalTim
TimeoutBuffer.ReadTotalTim
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,dcb
dcbPort.BaudRate:=9600;
dcbPort.XonChar:= char(17);
dcbPort.XoffChar:= char(19);
SetCommState(hCommFile,dcb
end;
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.