还是用mscomm实现同部机的两个串口通讯问题,救命!!!!!!(100分)

  • 主题发起人 主题发起人 IcanYoucan
  • 开始时间 开始时间
I

IcanYoucan

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我用mscomm控件就是做不了串口的通讯程序,我始终有这样的一个疑问:
当用MSCOMM的OUTPUT方法时,比如MScomm1.output:='abcd';它是把'abcd发到发送瑗冲区,但
瑗冲区中的数据该怎么样发到对方的串口中去呢?如果发过去的话是不是就发到对方串口的接收
区中去呢?
希望各位高手指点迷津,最好能发一个简单且完整的程序给我,谢谢.
 
是啊.接收数据是事件呀,OnReceivedData什么的.
 
首先你要将发送缓存设定为一个值,否则你的'abcd'太短就会存在在缓存里不即时发送,其次
在对方接收缓存中也是同样道理,还有用MSCOMM的ONMSCOMM事件来接收,具体可去
xiaoywh.51.net 查看,有程序和源码
 
本人程序中的一部分,仅做参考喽:

接收事件:
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;

 
后退
顶部