{ Вспомогательные процедуры сохранения состояния контроллеров } {--------------------------------------------------------------------------- The control units for mass-spectrometer MI1201-AGM (c) Copyright Aleksandrov O.E., 1998 Модуль управления масс-спектрометром МИ1201-АГМ (c) Собственность Александрова О.Е., 1998 Molecular Physics department 620002, Екатеринбург, К-2 USTU, Ekaterinsburg, K-2, 620002 УГТУ, RUSSIA Кафедра молекулярной физики phone 75-48-39 тел. 75-48-39 E-mail: aleks@dpt.ustu.ru ----------------------------------------------------------------------------} Unit DataSave; INTERFACE USES uICR; const cMaxDataSize=$FF00; type tDSErrorCode=(ecOK, ecInvalidCRC, ecStoreSizeMismatch, ecExtractDataInvalid, ecExtractSizeMismatch); tName=string[16]; tPtrData=^tData; tData=object procedure Init(Sz:word); function Valid:boolean; function Version:word; function Size:word; function FullSize:word; function Next:tPtrData; function Name:tName; function LastError:tDSErrorCode; procedure Store(var DataToStore; DataSize, DataVersion:word; const DataName:tName; var OK:boolean); procedure Extract(var PlaceForData; DataSize:word; var OK:boolean); private prSize:word; prLastError:tDSErrorCode; prVersion:word; prName:tName; prCRC:uICR.tCRC; prICRC:longint; prDataStart:longint; function ControlSum:longint; function ValidControlSum:boolean; end; function SizeOfData(Size:word):word; procedure Allocate(var d:tPtrData; Size:word); procedure Free(var d:tPtrData); IMPLEMENTATION USES MiscFunc; type tDataArray=array[1..cMaxDataSize] of byte; tPDataArray=^tDataArray; tData0=object(tData) DataArray:tDataArray; end; function SizeOfData(Size:word):word; begin SizeOfData:=SizeOf(tData)+Size; end; procedure Allocate; begin GetMem(d, SizeOfData(Size)); d^.Init(Size); end; procedure Free; begin if d<>NIl then begin FreeMem(d, SizeOfData(d^.prSize)); d:=NIL; end; end; procedure tData.Init; begin prSize:=Sz; end; function tData.LastError; begin LastError:=prLastError; end; function tData.ControlSum; begin ControlSum:=uICR.iCRC(prCRC, prDataStart, prSize, 0,FALSE); end; procedure tData.Store(var DataToStore; DataSize, DataVersion:word; const DataName:tName; var OK:boolean); var i:word; begin if prSize=DataSize then begin prName:=DataName; for i:=Succ(Length(prName)) to Pred(SizeOf(prName)) do begin prName[i]:=' '; end; prVersion:=DataVersion; Move(DataToStore, prDataStart, DataSize); prICRC:=uICR.iCRC(prCRC, prDataStart, DataSize, 0,FALSE); prLastError:=ecOK; end else begin prLastError:=ecStoreSizeMismatch; end; Ok:=LastError=ecOK; end; procedure tData.Extract; begin if not Valid and (DataSize=Size) then begin prLastError:=ecExtractDataInvalid; end else if not (DataSize=Size) then begin prLastError:=ecExtractSizeMismatch; end else begin Move(prDataStart, PlaceForData, Size); prLastError:=ecOK; end; Ok:=LastError=ecOK; end; function tData.Valid; begin Valid:=ValidControlSum and (Size>0); end; function tData.Version; begin Version:=prVersion; end; function tData.Name; begin Name:=prName; end; function tData.Size; begin Size:=prSize; end; function tData.FullSize; begin FullSize:=Size+SizeOf(tData); end; function tData.Next; type tByteArray=array[0..$FFFF-1] of Byte; tPtrByteArray=^tByteArray; begin if Valid then Next:=Addr(tPtrByteArray(@Self)^[FullSize]) else Next:=NIL; end; { PRIVATE ---------------------------------------- } function tData.ValidControlSum; begin ValidControlSum:=(prICRC = ControlSum); end; END.