OMRON FINS DEMO
unit PLC.OMRON;
interface
uses
System.StrUtils, System.SysUtils;
const
HEAD_CODE = '@';
DEFAULT_UNIT = 0;
PC_CODE = 'FA';
HEAD_BODY = '000000000';
READ_CMD = '0101';
WRITE_CMD = '0102';
DEFAULT_BIT = '00';
END_CODE = '*' + Chr(13);
WORD_PER_ASCII = 4;
BYTE_PER_ASCII = 2;
type
TFINS = class(TObject)
public
{ 组装 Bit 读命令 }
class function BuildReadBitCommand(uno: Integer; area: string; channel: Integer; bit: Integer; num: Integer): string;
{ 组装 Bit 写命令 }
class function BuildWriteBitCommand(uno: Integer; area: string; channel: Integer; bit: Integer; num: Integer; data: array of Integer): string; overload;
{ 组装 Bit 写命令 }
class function BuildWriteBitCommand(uno: Integer; area: string; channel: Integer; bit: Integer; num: Integer; hex: string): string; overload;
{ 组装 Word 读命令 }
class function BuildReadWordCommand(uno: Integer; area: string; channel: Integer; num: Integer): string;
{ 组装 Word 写命令 }
class function BuildWriteWordCommand(uno: Integer; area: string; channel: Integer; num: Integer; data: array of Integer): string; overload;
{ 组装 Word 写命令 }
class function BuildWriteWordCommand(uno: Integer; area: string; channel: Integer; num: Integer; hex: string): string; overload;
end;
////////////////////////////////////////////////////////////////////////////////
TFCSUtil = class(TObject)
public
class function IntArrToHexStr(data: array of Integer): string;
class function GetFCS(data: string; offset: Integer = 0): string;
class function CheckFCS(data: string): Boolean;
end;
implementation
{ 组装 Bit 读命令 }
class function TFINS.BuildReadBitCommand(uno: Integer; area: string; channel: Integer; bit: Integer; num: Integer): string;
var
cmd: string;
fcs: string;
begin
cmd := HEAD_CODE + IntToHex(uno, BYTE_PER_ASCII) + PC_CODE + HEAD_BODY + READ_CMD + area + IntToHex(channel, WORD_PER_ASCII) + IntToHex(bit, BYTE_PER_ASCII) + IntToHex(num, WORD_PER_ASCII);
fcs := TFCSUtil.GetFCS(cmd);
Result := cmd + fcs + END_CODE;
end;
{ 组装 Bit 写命令 }
class function TFINS.BuildWriteBitCommand(uno: Integer; area: string; channel: Integer; bit: Integer; num: Integer; data: array of Integer): string;
var
cmd: string;
fcs: string;
begin
cmd := HEAD_CODE + IntToHex(uno, BYTE_PER_ASCII) + PC_CODE + HEAD_BODY + WRITE_CMD + area + IntToHex(channel, WORD_PER_ASCII) + IntToHex(bit, BYTE_PER_ASCII) + IntToHex(num, WORD_PER_ASCII) + TFCSUtil.IntArrToHexStr(data);
fcs := TFCSUtil.GetFCS(cmd);
Result := cmd + fcs + END_CODE;
end;
{ 组装 Bit 写命令 }
class function TFINS.BuildWriteBitCommand(uno: Integer; area: string; channel: Integer; bit: Integer; num: Integer; hex: string): string;
var
cmd: string;
fcs: string;
begin
cmd := HEAD_CODE + IntToHex(uno, BYTE_PER_ASCII) + PC_CODE + HEAD_BODY + WRITE_CMD + area + IntToHex(channel, WORD_PER_ASCII) + IntToHex(bit, BYTE_PER_ASCII) + IntToHex(num, WORD_PER_ASCII) + hex;
fcs := TFCSUtil.GetFCS(cmd);
Result := cmd + fcs + END_CODE;
end;
{ 组装 Word 读命令 }
class function TFINS.BuildReadWordCommand(uno: Integer; area: string; channel: Integer; num: Integer): string;
var
cmd: string;
fcs: string;
begin
cmd := HEAD_CODE + IntToHex(uno, BYTE_PER_ASCII) + PC_CODE + HEAD_BODY + READ_CMD + area + IntToHex(channel, WORD_PER_ASCII) + DEFAULT_BIT + IntToHex(num, WORD_PER_ASCII);
fcs := TFCSUtil.GetFCS(cmd);
Result := cmd + fcs + END_CODE;
end;
{ 组装 Word 写命令 }
class function TFINS.BuildWriteWordCommand(uno: Integer; area: string; channel: Integer; num: Integer; data: array of Integer): string;
var
cmd: string;
fcs: string;
begin
cmd := HEAD_CODE + IntToHex(uno, BYTE_PER_ASCII) + PC_CODE + HEAD_BODY + WRITE_CMD + area + IntToHex(channel, WORD_PER_ASCII) + DEFAULT_BIT + IntToHex(num, WORD_PER_ASCII) + TFCSUtil.IntArrToHexStr(data);
fcs := TFCSUtil.GetFCS(cmd);
Result := cmd + fcs + END_CODE;
end;
{ 组装 Word 写命令 }
class function TFINS.BuildWriteWordCommand(uno: Integer; area: string; channel: Integer; num: Integer; hex: string): string;
var
cmd: string;
fcs: string;
begin
cmd := HEAD_CODE + IntToHex(uno, BYTE_PER_ASCII) + PC_CODE + HEAD_BODY + WRITE_CMD + area + IntToHex(channel, WORD_PER_ASCII) + DEFAULT_BIT + IntToHex(num, WORD_PER_ASCII) + hex;
fcs := TFCSUtil.GetFCS(cmd);
Result := cmd + fcs + END_CODE;
end;
////////////////////////////////////////////////////////////////////////////////
class function TFCSUtil.IntArrToHexStr(data: array of Integer): string;
var
i: Integer;
tmpStr: string;
begin
tmpStr := '';
for i := 0 to Length(data) do begin
tmpStr := tmpStr + IntToHex(data[i], WORD_PER_ASCII);
end;
Result := tmpStr;
end;
class function TFCSUtil.GetFCS(data: string; offset: Integer = 0): string;
var
i: Integer;
fcs: Integer;
begin
fcs := 0;
for i := 1 to (Length(data) + offset) do begin
fcs := fcs xor ord(data[i]);
end;
Result := IntToHex(fcs, BYTE_PER_ASCII);
end;
class function TFCSUtil.CheckFCS(data: string): Boolean;
var
len: Integer;
fcs: string;
begin
len := Length(data);
if len < 22 then begin
Result := False;
Exit;
end;
if '@' <> data[1] then begin
Result := False;
Exit;
end;
if ('*' <> data[len - 1]) or (Chr(13) <> data[len]) then begin
Result := False;
Exit;
end;
fcs := TFCSUtil.GetFCS(data, -4);
Result := fcs = data[len - 3] + data[len - 2];
end;
end.
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!