本人程序中的一部分,仅做参考喽:
接收事件:
procedure TfmCommServer.MSCommComm(Sender: TObject);
var
TemStr,InStr:String;
i:Integer;
InTempS:OleVariant;
LenInBuf:Integer;
begin
ActiveComm:=Sender as TMSComm;
//with CommStakeOuter[ActiveComm.Tag] do State:=True;
ActiveStipulation:=GetStipulation(ActiveComm);
InTempS:=ActiveComm.Input;
if VarIsArray(InTempS) then
begin
LenInBuf:=VarArrayHighBound(InTempS,1)+1;
TemStr:='';
for i:=1 to LenInBuf do TemStr:=TemStr+Chr(Byte(InTempS[i-1]));
InStr:=TemStr;
end
else InStr:=InTempS;
if ActiveStipulation <> nil then ActiveStipulation.GetCommData(InStr);
if CurrentComm = ActiveComm then
begin
if not MonitorPaused and cbCodeWatch.Checked then
begin
CommWatcher.GetData(InStr);
CommWatcher.Resume;
end;
end;
if CurrentStipulation = ActiveStipulation then
begin
CurrentStipulation:=ActiveStipulation;
if cbCodeWatch.Checked then FrameWatcher.Resume;
end;
if DataDispatcher.Suspended then DataDispatcher.Resume;
end;
发送:
procedure TCDTStipulation.BroadCast;
//-----广播命令
//入口: 无
//出口:向下发21命令
var
Control_Word,Send_String:string;
begin
Control_Word:=Chr($21)+Chr($9E)+Chr($0)+Chr(StationID)+Chr($FF);
Control_Word:=Control_Word+Chr(GetCheckCRC(Control_Word));
Send_String:=CDTSynword+Control_Word;
if (Comm = nil) or not(Comm.PortOpen) then Exit;
Comm.OutPut:=StrToVarByteArray(Send_String);
end;
设置部分:
procedure TfmCommServer.UpdateCommSettings;
var
CurKey:TRegistry;
i:Integer;
ItemStr:String;
begin
CurKey:=TRegistry.Create;
CurKey.OpenKey(Comm_Key,True);
comboCommPort.Clear;
for i:= 0 to CommCount-1 do
with CurKey,Comms do
begin
InPutMode:=comInputModeBinary;
Settings:='300,n,8,1';
OnComm:=MSCommComm;
if ValueExists('COM'+IntToStr(CommPort)) then
Settings:=ReadString('COM'+IntToStr(CommPort))
else
WriteString('COM'+IntToStr(CommPort),Settings);
ItemStr:='COM'+IntToStr(Comms.CommPort);
comboCommPort.Items.Add(ItemStr);
try
PortOpen:=True;
except
// ShowMessage('通讯端口已打开或不存在!请核查后再重新打开。');
end;
end;
comboCommPort.ItemIndex:=0;
comboCommPortChange(Self);
end;