使用delphi7 + TPSScript 脚本控件 来执行PSACAL脚本, 速度很快,效率非常高,比LUA好,但是后面发现脚本执行效果不对,查了好久才发现脚本不是按顺序执行的,某些脚本代码提前执行了,
脚本如下:
function ChannelReadSensorID(Channel: Byte): Boolean;
var
devAddr: Byte;
i, n: Integer;
ID: Cardinal;
begin
Result := False;
if (Channel < 0) or (not (Channel < ChannelCount)) then exit;
n :=0;
for devAddr:=minDeviceAddr to maxDeviceAddr do
begin
if not CheckChannelExist(devAddr, Channel) then Continue;
CAN_SendShortCMD(devAddr, Channel, CAN_CMD_SelectChannel , Channel);
SetSensorID(devAddr, Channel, 0);
CAN_DelayMS(devAddr, Channel, 200); // 继电器切换,等待200ms时间
CAN_SendShortCMD(devAddr, Channel, CAN_CMD_ForceRestToOWIMode , 0);
CAN_DelayMS(devAddr, Channel, 300);
RegistersClearRegsStatus(devAddr, Channel);
RegistersSetReadStatus(devAddr, Channel, NSA_REG_OFFSET_ID, 4);
RegistersTxReadCmdByStatus(devAddr, Channel);
Inc(n);
end;
if n > 0 then DelayMs(500);
// wait when all ok, or Timeout
for i:= 0 to 5 do
begin
DelayMs(100);
n := 0;
for devAddr:=minDeviceAddr to maxDeviceAddr do
begin
if CheckChannelExist(devAddr, Channel) and RegistersReadErrCheck(devAddr, Channel) then Inc(n);
end;
if n = 0 then break;
end;
// if read error, Read cmd again, and wait a moment
n :=0;
for devAddr:=minDeviceAddr to maxDeviceAddr do
begin
if CheckChannelExist(devAddr, Channel) and RegistersReadErrCheck(devAddr, Channel) then
begin
RegistersReadAgainIfError(devAddr, Channel);
inc(n);
end;
end;
if n > 0 then DelayMs(500);
//----------------------以下的代码提前执行了-----------------------------------------
for devAddr:=minDeviceAddr to maxDeviceAddr do
begin
if CheckChannelExist(devAddr, Channel) and (not RegistersReadErrCheck(devAddr, Channel)) then
begin
ID := SensorID(devAddr, Channel);
if (not CheckSensorID(ID)) then
begin
ID := NewSensorID();
SetSensorID(devAddr, Channel, ID);
end
else begin
GetHistoryDataByID(devAddr, Channel, ID);
end;
end;
end;
Result := True;
end;
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>