我在 Delphi 中创建了一个简单的程序,使用 2 个参数通过 COM 端口发送字符,第一个参数是端口号,第二个参数是要发送的字符。因此,如果我将其保存为 p.exe,“p.exe 20 A”将通过 COM20 发送“A”。
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;
而且我还使用具有相同 COM 号 baudrate=9600, flow_control=none 的超级终端,它给出了相同的结果。角色发送良好。问题是,每次登录到我的 Windows XP 时,在执行以下步骤之前,我无法运行我的程序 (p.exe): 通过超级终端连接到指定的 COM,然后断开它。然后我的可执行文件可以运行。否则,就像您在同一个 COM 中运行两个超级终端会话一样,它不会工作。有人对此有提示吗?我错过了代码中的任何内容吗?