I'm creating a simple program in Delphi, to send character through COM port using 2 parameters, the first parameter is the port number and the second parameter is the character to be sent. So if i save it as p.exe, "p.exe 20 A" will send "A" through COM20.
try
PhoneNumber := ParamStr(2);
if(StrToInt(ParamStr(1))>=10)then
CommPort := '\\.\COM'+ParamStr(1)
else
CommPort := 'COM'+ParamStr(1);
hCommFile := CreateFile(PChar(CommPort),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if hCommFile=INVALID_HANDLE_VALUE then begin
ShowMessage('Unable to open '+ CommPort);
end;
if WriteFile(hCommFile, PChar(PhoneNumber)^, Length(PhoneNumber),NumberWritten, nil)=false then
showmessage('Unable to send');
PurgeComm(hCommFile,PURGE_TXCLEAR);
FlushFileBuffers(hCommFile);
CloseHandle(hCommFile);
Application.Terminate;
except
PurgeComm(hCommFile,PURGE_TXCLEAR);
FlushFileBuffers(hCommFile);
Application.Terminate;
end;
And I also using hyperterminal with the same COM number baudrate=9600, flow_control=none and it gives the same result. The character sent well. The problem is, I cant run my program (p.exe) before I do the following steps each time i logged on to my Windows XP: Connect through hyperterminal to the designated COM, disconnect it. then my executable can be run. Otherwise, just like you run two session of hyperterminal in the same COM, it wouldn't work. Anybody got a hint bout this? Did I miss anything in my code?