3 3 123
21 27 27

: object Pascal (Delphi)

  1. #21

    Mar 2012
    Athens, , Greece.
    :
    99


    TCP/IP Server
    TCP/IP (Server) TServerSocket & TClientSocket components.
    ( package /bin/dclsockets70.bpl)
    http://delphi.about.com/od/networking/l/aa112602a.htm components.
    http://www.overbyte.be/frame_index.h...ducts/ics.html Indy Delphi socket components.

    (clients)

    1. browser http
    :
    http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
    http://127.0.0.1:1024

    H HTTP Server
    :
    GET / HTTP/1.1
    Host: 127.0.0.1:1024
    User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: en,el;q=0.8,el-gr;q=0.5,en-us;q=0.3
    Accept-Encoding: gzip, deflate
    DNT: 1
    Connection: keep-alive
    Server
    :
    HTTP/1.1 200 OK
    Content-Length: 11
    Hello World
    Connection: close
    [empty line]


    2. telnet (echo).
    Start/Run Cmd
    Dos box telnet 127.0.0.1 1024
    Hello Server Enter
    :
    Client  ---------- initate connection -----------> Server
    ...
    Client  <---------------command------------------- Server
    Client  ----------------response-----------------> Server
    ...
    Client  <---------------command------------------- Server
    Client  ----------------response-----------------> Server
    RFC 854, modbus Web server


    3. To modbus client.
    Server 502.

    :
    |Modbus Application Protocol (MBAP) Header | Protocol Data Unit (PDU)|
    |------------------------------------------+-------------------------|
    |Transaction |Protocol  | Length  | Unit   | Function   |   Data     |
    |Identifier  |Identifier| Field   | ID     | Code       |            |
    |--------------------------------------------------------------------|
    | (2 bytes)  |(2 bytes) |(2 bytes)|(1 byte)|(1 byte)    |   |
    |------------------------------------------+-------------------------|
    , TCP/Server, modbus client functions.

    03 (0x03) Read Holding Registers
    MODBUS Application Protocol Specification V1.1b 19/51
    http://www.modbus.org/docs/Modbus_Ap...ocol_V1_1b.pdf

    06 (0x06) Write Single Register
    . MODBUS Application Protocol Specification V1.1b



    Statefull
    WebServer, SessionId Client Cookie. SessionId, WebServer, client (user name, db connection )
    client, , SessionId (statefull).

    .
    server 1024 thread (ServerType= stThreadBlocking). , pool. (stateless).
    (threads) TServerClientThread Server event OnGetThread.
    :
    procedure TFrmServer.ServerGetThread(Sender: TObject;  ClientSocket: TServerClientWinSocket;   var SocketThread: TServerClientThread);
    begin
     SocketThread:=TSrvThread.Create(ClientSocket, Handle);
    end;
    
    TSrvThread=class(TServerClientThread)
               private
                fHandle:THandle;
    ...
               protected
                procedure ClientExecute; override; 
               public
                constructor Create(aSocket:TServerClientWinSocket;
               aHandle:THandle);
               end;
    procedure TSrvThread.ClientExecute; TCP/IP .

    RequestType rtHTTP, rtText, rtModBus rtBinary .

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

    :
    Request-Line   = Method SP Request-URI SP HTTP-Version CRLF
     Method         = "OPTIONS"                ; Section 9.2
                          | "GET"                    ; Section 9.3
                          | "HEAD"                   ; Section 9.4
                          | "POST"                   ; Section 9.5
                          | "PUT"                    ; Section 9.6
                          | "DELETE"                 ; Section 9.7
                          | "TRACE"                  ; Section 9.8
                          | "CONNECT"                ; Section 9.9
                          | extension-method
           extension-method = token
    http HTTP/1. GET HEAD.

    telnet, . To socket control .

    .

    TModbus modbus (C Pascal) .

    :
      
      rtModBus : begin
             case pbFC^ of
               $03 : begin // mbfReadHoldingRegs = $03;
                      pa:=pword(FRequest);
                      pd:=pa;
                      inc(pa, 4); // points to Start Address
                      inc(pd, 5); // point to count
                      move(FRequest^,data[0],8);
                      cnt:=swap(pd^);
                      data[ 8]:=cnt*2;
                      pd:=pword(@data[9]);
                      for i:=0 to pred(cnt) do begin
                        w:=SendMessage(fHandle,REG_GET,swap(pa^)+i,0);
                        pd^:=swap(w);
                        inc(pd);
                       end;
                      size:=8+1+cnt*2;
                      data[5]:=2 + 1 + cnt*2;
                      ClientSocket.SendBuf(data,size);
                     end;
    source
    http://www.filefactory.com/file/dftv...n/Server01_rar

    ( . 19)
    Bugus humanum est


  2. #22

    Mar 2012
    Athens, , Greece.
    :
    99
    ;

    . .

    .

    Server server . Server tcp/ip , (handler threads).
    HTTP Telnet TModbus .

    . . , .

    TModbus
    modbus & client/server TCP/IP. (function code FC). modbus byte (big Endian).
    4 Modbus
    , bits .
    Coils, (solid-state relay) bits &
    Input Registers, 16-bit word, .
    Holding Registers, 16-bit word, &

    (0x01) Read Coils
    (0x02) Read Discrete Inputs
    (0x03) Read Holding Registers
    (0x04) Read Input Registers
    (0x05) Write Single Coil
    (0x06) Write Single Register

    O modbus Master PLC. To PCL slave Master.
    .

    Master/client socket component TCP/IP Server/ Slave. object TModbus .

    array properties.

    :
    Modbus:=TModbus.Create(FSocket);
    If Modbus.ReadHoldingRegisters(1,1) then
     Modbus.HoldingRegs[1]
    .

    Server/Slave TCP/IP .
    2 constructor object . , exception.

    TModbus (encapsulates) modbus ( Chuck http://blog.magicsoftware.com/2011/0...rogrammer.html )
    :
      // check for ModBus request
      if FReqSize>=12 then begin
       pwTI :=pword(p);    // 0..1 Transaction Identifier
       pwPI :=pword(p+2);  // 2..3 Protocol Identifier
       pwlf :=pword(p+4);  // 4..5 Length Field
       pbUID:=pbyte(p+6);  // 6    Unit ID
                           //      MB_IGNORE_UNITID = 255
       pbFC :=pbyte(p+7);  // 7;   Function Code
                           //      mbfReadHoldingRegs = $03;
                           //      mbfWriteOneReg = $06;
                           // 8;   Data
    
       if ((pbFC^=$06) or (pbFC^=$03)) and (pwlf^=swap($0006)) then begin
        result:=rtModBus;
        exit;
       end;
      end;

    :
      try
       FModbus:=TModbus.Create(FRequest^,FReqSize);
       FModbus.OnGetData:=ModbusGetData;
       FModbus.OnSetData:=ModbusSetData;
       result:=rtModBus;
       exit;
      except
       Log('Not modbus message');
      end;

    :
    rtModBus : begin
             case pbFC^ of
               $03 : begin // mbfReadHoldingRegs = $03;
                      pa:=pword(FRequest);
                      pd:=pa;
                      inc(pa, 4); // points to Start Address
                      inc(pd, 5); // point to count
                      move(FRequest^,data[0],8);
                      cnt:=swap(pd^);
                      data[ 8]:=cnt*2;
                      pd:=pword(@data[9]);
                      for i:=0 to pred(cnt) do begin
                        w:=SendMessage(fHandle,REG_GET,swap(pa^)+i,0);
                        pd^:=swap(w);
                        inc(pd);
                       end;
                      size:=8+1+cnt*2;
                      data[5]:=2 + 1 + cnt*2;
                      ClientSocket.SendBuf(data,size);
                     end;

    :
           rtModBus : begin
                       FModbus.PrepareResponse;
                       FModbus.Respond(ClientSocket);
                       FreeAndNil(FModbus);
                      end
    Modbus .

    :
     TBytes = array of byte;
    
     TMBAP = packed record { 7 bytes }
               TrnId : word;
               ProtocolIdent: word;
               Len:word;
               UnitId:byte;
              end;
    
     TPDU  = packed record { 253 bytes }
               FC  : byte;
               Data: packed record case integer of
                      0 : (b: array[0..251] of byte);
                      1 : (w: array[0..125] of word);
                      2 : (o: packed record
                                len : byte;
                                w   : array[0..124] of word;
                                b   : byte;
                               end)
                     end;
              end;
    
     TADU  = packed record // 260 bytes
               MBAP : TMBAP;
               PDU  : TPDU;
              end;
    
    |Modbus Application Protocol (MBAP) Header | Protocol Data Unit (PDU)|
    |------------------------------------------+-------------------------|
    |Transaction |Protocol  | Length  | Unit   | Function   |   Data     |
    |Identifier  |Identifier| Field   | ID     | Code       |            |
    |--------------------------------------------------------------------|
    | (2 bytes)  |(2 bytes) |(2 bytes)|(1 byte)|(1 byte)    |   |
    |------------------------------------------+-------------------------|
    
     TModbus = class
               Private
                 ...
               Public
                // Client Side
                constructor Create(aSocket:TCustomWinSocket); overload;
                // Server Side
                constructor Create(const Request; size:word); overload;  
                procedure   PrepareResponse;
                procedure   Respond(aSocket:TCustomWinSocket);
    
                function ReadCoils(StartAddress, Count:word):boolean;
                function ReadDiscreteInputs(StartAdd,Count:word):boolean; 
                function ReadHoldingRegisters(RegNo,Count:word):boolean; 
                function ReadInputRegisters(RegNo,Count:word):boolean;
                procedure WriteSingleCoil(Addr:word;value:boolean);
                procedure WriteSingleRegister(RegNo,value:word);
    
                property  DiscreteInput[idx:integer]:boolean read ...
                property  Coils[idx:integer]:boolean read ...
                property  InputRegs[idx:integer]:word read ...
                property  HoldingRegs[idx:integer]:word read ...
    
                property  FC:byte read FFC;
                property  Request:TBytes read GetRequest;
                property  Response:TBytes read GetResponse;
    
                property  OnGetData:TOnExchangeData read ...         
                property  OnSetData:TOnExchangeData read ...
    
               end;
    objects.
    .

    , TModbus TCP/IP ?
    connected sockets .

    Server/Slave , , TModbus . Slave TModbus event Handlers
    :
    TOnExchangeData = procedure(FC:byte; param1,param2:word;var value:word) of object;
    source
    http://www.filefactory.com/file/vjru...n/Server02.rar

    .


  3. #23

    Mar 2012
    Athens, , Greece.
    :
    99


    PSTN modem internet.
    (RS232) . USB 480 kbits.
    2 2 2 . .
    .

    byte ?. , bit=1 byte. . UART ( 8051) (Tx) .
    http://www.slideshare.net/pantechsol...municationuart
    , 2 (, parity, stop bits).

    .
    byte 1. bit () 1 byte (parity odd / even). 1 (odd/even parity).
    UART interrupts (transmit register) (receive register) . interrupts driver events .
    buffer . (drivers) .
    2 3 (Tx, Rx, Gnd) Tx Rx Gnd.

    .
    ( super market).
    ftp://ftp.accordgroup.ro/Pentru%20de...20PROTOCOL.pdf

    STX (0x02) (0x03).
    :
    +-----+---------------+-----+
    | STX |      Data     | ETX |
    +-----+---------------+-----+
    Data ascii 0x32 0xFF.

    Tx data packet
    :
    <--------------------- Data -------------------><--Checksum -->
    +---------+---------+---------+------+---------+---------------+
    | Request / Field 1 / Field 2 / .... / Field N /       CC      |
    +---------+---------+---------+------+---------+---------------+
    Request 1 char

    Rx data packet
    :
    <--------------------- Data -----------------------------><-Checksum->
    +---------+--------+--------+-------+-------+------------+-----------+
    |Response /Status 1/Status 2/Field 1/Field 2/..../Field N/    CC     |
    +---------+--------+--------+-------+-------+------------+-----------+
    Response,status1, status2 2 numeric digits

    :
     BYTE CalcChecksum(BYTE *packet)                        
     {                                                      
       BYTE sum = 0;                                        
       int  checklength = strlen(packet) - 2;               
                                                            
       while(check length--) sum += (BYTE) (*packet++);     
       return (sum % 100));                                 
     }


    1.

    Sender (host) Receiver (device)
    :
    ---------------------------------------------------------
         IDLE                           IDLE
         ENQUIRE ---------------------> 
                 <--------------------- ACKNOWLEDGE
       x PACKET ---------------------> 
                 <--------------------- ACKNOWLEDGE
                 <--------------------- Rx PACKET
     ACKNOWLEDGE --------------------->
            IDLE                        IDLE
    2. , ,
    3. .
    4. blocking .

    (state transition diagram).

  4. #24

    Mar 2012
    Athens, , Greece.
    :
    99
    .



    post.

  5. #25

    Mar 2012
    Athens, , Greece.
    :
    99


    . , , ( ) . . .

    TPacket
    & .
    :
    TPacketType = (ptTx, ptRx);
    
    TPacket = class(TObject)
    ...
     public
      constructor Create;            overload;
      constructor Create(aReq:char); overload;
      destructor  destroy; override;
      class function CalcCheckSum(const data: string): string;
      class function ChecksumValid(const data:string):boolean;
      property Fields[index:integer]:string read GetField write SetField;
      property Data:string read GetData write SetData;
      property PacketType:TPacketType read fPacketType write fPacketType;
      property RxResposeCode:integer read fRxResposeCode;
      property RxStatus1:integer  read fRxStatus1;
      property RxStatus2:integer read fRxStatus2;
     end;


    :
    tx:=TPacket.Create('b'); // Request = SetVatRates
    for i:=1 to 5 do //  5  
     tx.Fields[i]:=format('%.2f/',[VatRates[i]]);
    property data checksum


    :
    rx:=TPacket.Create;
    rx.data:=msg; //  STX, ETX
    o property RxResposeCode:integer /.
    array property fields .

    class functions. ( function) object.

    .
    "" objects constructor . objects functions.

    object. O constructor, objects class function.

    TSession
    . .
    . .
    :
    TSession = class
    ... 
     public
      constructor Create(const params:string); // COM1,9600,N,8,1
      destructor destroy; override;
      // <0  
      // 0    &   
      // >0  
      function  Communicate(const TxPacket:TPacket; 
                            out RxPacket:TPacket):integer;
     end;


    TFiscalPrinter
    .
    TSession & Tpacket .

    , (application).

    :
    TFiscalPrinter = class
    .. .
     public
      constructor create(const COMPort:string);
      destructor destroy; override;
    
      function FiscalTime:TDateTime;
      function SerialNumber:String;
      function TicketNumber:integer;
      function ZNumber:integer;
    
      procedure OpenDay;        // Logical operation
      procedure CloseDay;       // Issue Z, read EJ
    
      procedure OpenTicket(tt:TTicketType; const Title:string ='';                 
                           const Seira:string=''; number:integer=0);
      procedure SaleItem(const code, descr, barcode:string; 
                         price, quant, amount:double; vatCat:integer);
      procedure VoidItem(const code, descr, barcode:string; 
                         price, quant, amount:double; vatCat:integer);
      procedure RefoundItem(const code, descr, barcode:string; 
                            price, quant, amount:double; vatCat:integer);
      procedure DiscountItem(const code, descr, barcode:string; 
                         amount:double; vatCat:integer);
      procedure SubTotalTicket;
      procedure SubTotalDiscount(amount:double);
    
      procedure CancelTicket;
      procedure CloseTicket;
    ...
     property capEJ:boolean read FcapEJ; // capability has Electronic Journal
     property capSlip:boolean read FcapSlip;// capability has SlipPrinter
    
     property Header[index:integer]:string read GetHeader write SetHeader;
     property Footer[index:integer]:string read GetFooter write SetFooter;
     property VatRates[index:integer]:string read GetVatRate write SetVatRate;    
     end;
    .
    1. / (field separator) .
    FS = 28 0x 1C

    2. async32 component. (open source)
    http://www.general-files.com/downloa...s554ee144h32i0

    3. post .
    .


  6. #26

    Jun 2008
    Glyfada, , Greece.
    :
    3,101
    ! !
    forum .
    forum !

    modbus . 23 acrobat.
    (Pascal) !! !


    ..
    . ܻ. ' . !



  7. #27

    Mar 2012
    Athens, , Greece.
    :
    99


    ,

    . . . ( ) ( ).

    , soft , .
    . objects .
    quote: !
    . . .
    quote: ..
    . . unit functions .

    , . .

    , , .

    , .

  • You may not post new threads
  • You may not post attachments
  •