zoukankan      html  css  js  c++  java
  • 【Delphi】PLC.OMRON.FINS

    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.
    
    
    
  • 相关阅读:
    心态--编程+成长
    【Oracle】容易犯的错误和技巧集合
    【WPF】DataGrid的Row样式设置
    【WPF】给TextBox添上Label
    【Oracle】异常信息的加工处理
    【.Net 】Json和Xml解析
    【Win32 API】利用SendMessage实现winform与wpf之间的消息传递
    【WCF】利用WCF实现上传下载文件服务
    【Oracle】实现Oracle数据库对象的一键升级
    感触
  • 原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147083.html
Copyright © 2011-2022 走看看