W
wuchunhua
Unregistered / Unconfirmed
GUEST, unregistred user!
通过网络虚拟串口对几个PLC进行数据采集,一个PLC占用一个串口,PLC每间隔1s左右就自动上发一组数据(采用电力部门的CDT规约循环发送),现在需要对不同串口发来的数据进行处理、显示和保存到数据库中。以下是我程序中的一些代码:
//创建SPCOMM控件监听串口
for i:=0 to recordcount -1do
begin
//一些其他处理
freccomm:=TComm.Create(self);
FRecComm.CommName :='COM'+FComminfo[i,2] ;
freccomm.ReadIntervalTimeout :=readtime;
freccomm.BaudRate :=strtoInt(fcomminfo[i,3]);
freccomm.OnReceiveData :=comm1.OnReceiveData ;
try
freccomm.StartComm ;
except
application.MessageBox ('直流屏监听串口打开失败,请检查串口是否被占用!','错误提示',MB_iconwarning or MB_OK);
freccomm.StopComm;
continue ;
end;
next;
application.ProcessMessages ;
end;
//数据接收触发事件 Comm1ReceiveData
procedure Tfrmzlpxx.Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i,icount:integer;
CommRecBuf:array of byte ;
begin
//进入临界
EnterCriticalSection(cs);
setlength(CommRecBuf,BufferLength) ;
setlength(FCommDataBuf,1) ;
Move(Buffer^,CommRecBuf[0],BufferLength);
CommName:=(sender as TComm).CommName;
(sender as TComm).StopComm ;
for i:=low(FRecComm) to High(FRecComm)do
begin
if 'COM'+FComminfo[i,2] =CommName then
begin
if (FComminfo[i,4] = 'GZDW31') or (FComminfo[i,4] = 'GZDW32') then
begin
//校验成功,保存数据及填充数据;失败则跳出循环,离开临界
if FillGZW31Data(CommRecBuf,i) then
FillGZDW31Data(i);
end
else
if (FComminfo[i,4] = 'GZDW41') or (FComminfo[i,4] = 'GZDW42') then
FillGZW41Data(CommRecBuf,i);
Break;
end;
end;
(sender as TComm).startcomm ;
//离开临界
LeaveCriticalSection(cs);
end;
//上面的程序总存在不同的问题,请大家给点意见
//创建SPCOMM控件监听串口
for i:=0 to recordcount -1do
begin
//一些其他处理
freccomm:=TComm.Create(self);
FRecComm.CommName :='COM'+FComminfo[i,2] ;
freccomm.ReadIntervalTimeout :=readtime;
freccomm.BaudRate :=strtoInt(fcomminfo[i,3]);
freccomm.OnReceiveData :=comm1.OnReceiveData ;
try
freccomm.StartComm ;
except
application.MessageBox ('直流屏监听串口打开失败,请检查串口是否被占用!','错误提示',MB_iconwarning or MB_OK);
freccomm.StopComm;
continue ;
end;
next;
application.ProcessMessages ;
end;
//数据接收触发事件 Comm1ReceiveData
procedure Tfrmzlpxx.Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i,icount:integer;
CommRecBuf:array of byte ;
begin
//进入临界
EnterCriticalSection(cs);
setlength(CommRecBuf,BufferLength) ;
setlength(FCommDataBuf,1) ;
Move(Buffer^,CommRecBuf[0],BufferLength);
CommName:=(sender as TComm).CommName;
(sender as TComm).StopComm ;
for i:=low(FRecComm) to High(FRecComm)do
begin
if 'COM'+FComminfo[i,2] =CommName then
begin
if (FComminfo[i,4] = 'GZDW31') or (FComminfo[i,4] = 'GZDW32') then
begin
//校验成功,保存数据及填充数据;失败则跳出循环,离开临界
if FillGZW31Data(CommRecBuf,i) then
FillGZDW31Data(i);
end
else
if (FComminfo[i,4] = 'GZDW41') or (FComminfo[i,4] = 'GZDW42') then
FillGZW41Data(CommRecBuf,i);
Break;
end;
end;
(sender as TComm).startcomm ;
//离开临界
LeaveCriticalSection(cs);
end;
//上面的程序总存在不同的问题,请大家给点意见