*-----------------------------------------------------------------------------
* PLC SERVER: : Data Exchange via  TCP/IP (jh.TCPcomm)
*-----------------------------------------------------------------------------
*  Module Deskription
*
*  Start the PLC servers and open the TCP-IP Port .
*  The data will be received by the python script on the port.
*  This writes the data to the PLC receive structure.
*  The PLC will read out the structure with the module 9373 and call the function.
*  The send structure can be written in that function and send return to sender.
*
*  - Announce the send- and receive- structure for the PLC-Python Communication.
*
*  - Start the python script PLC_server.py.
*    Parameter:
*      -s:<Name for  the send structure >
*      -r:< Name for  the receive structure >
*      -p:< TCP-IP server port>
*    Example: #define /s KS_Process_Parameter
*      S"-s:STG_PLC_server_transmit -r:STG_PLC_server_receive -p:8076"
*
*  - Analysis the incoming data packet and call the function.
*
*  - Return the Data to the sender
*-----------------------------------------------------------------------------

#define /s    KL_mode_synchronous                      K +0
#define /s    KL_mode_asynchronous                     K +1

#define /s    KL_memory_5_MB                           K +5
#define /s    KL_memory_8_MB                           K +8
#define /s    KL_memory_10_MB                          K +10

#define /s    KL_python_process_active                 K +0
#define /s    KL_python_process_not_active             K +2
#define /s    KL_terminate_python_process              K +0
#define /s    KL_kill_python_process                   K +1
#define /s    KL_ReadByPlc                             K +0
#define /s    KL_WriteByPlc                            K +1
#define /s    KL_Read_Write_status                     K +0
#define       KL_No_registered_clients                 K +1
#define       KL_without_wait_overwrite_data           K%000
#define       KL_without_wait                          K +0
#define       KL_with_wait                             K +1
#define       KL_new_data                              K +0

#define /s    KS_Process_path                          S"PLC:\\Python\\PLC_Server.py"
#define /s    KS_Process_parameter                     S"-s:STG_PLC_server_transmit -r:STG_PLC_server_receive -p:8076"
#define /s    KS_Process_name                          S"PLC_SERVER"

#define /s    ML_TCP_IP_server_started                 M

#define /s    BL_return_value                          B
#define /s    BL_Submit_Ident_Start                    B
#define /s    BL_submit_ident_monitor                  B

#define /s    DL_Error_9295                            D
#define /s    DL_Exit_9295                             D
#define /s    DL_Spawn_receive_Ident                   D

#include Mac_Lib.inc

GLOBAL TCP_IP_Server

;----------------------------
LBL TCP_IP_Server
;----------------------------

; Connection buildup by server

  LN     ML_TCP_IP_server_started
  AN     PN_error_TCP_server_connect_error
  AN     PN_error_submit_spawn_queue_full
  A      MG_pulse_500_ms
  IFT
    RPLY   BL_Submit_Ident_Start
    ==     K+0
    IFT
      SUBM   Server_Start
      =      BL_Submit_Ident_Start
      ==     K+0
      S      PN_error_submit_spawn_queue_full
    ENDI
  ENDI

  L      ML_TCP_IP_server_started
  A      MG_pulse_1_min
  IFT
    RPLY   BL_submit_ident_monitor
    ==     K+0
    IFT
      SUBM   Process_monitor
      =      BL_submit_ident_monitor
      ==     K+0
      S      PN_error_submit_spawn_queue_full
    ENDI
  ENDI


EM

;----------------------------
LBL Server_Start

  ; registration of the PLC structure
  PS     STG_PLC_server_status_transmit.Handle
  PS     KL_Read_Write_status
  CM     9372
  PLW
  <      K+0                           ;invalid handle
  IFT
    PS     S"STG_PLC_server_transmit"
    PS     KL_WriteByPlc
    PS     STG_PLC_server_status_transmit.Event
    CM     9370
    PL     STG_PLC_server_status_transmit.Handle
    PL     STG_PLC_server_status_transmit.LogOnReturn

    M_display_module_error
  ENDI

  PS     STG_PLC_server_status_receive.Handle
  PS     KL_Read_Write_status
  CM     9372
  PLW
  <      K+0                           ;invalid handle
  IFT
    PS     S"STG_PLC_server_receive"
    PS     KL_ReadByPlc
    PS     STG_PLC_server_status_receive.Event
    CM     9370
    PL     STG_PLC_server_status_receive.Handle
    PL     STG_PLC_server_status_receive.LogOnReturn

    M_display_module_error
  ENDI

  ; Python script start
  PS     KS_Process_name               ;process name
  CM     9297                          ;Status of a Python instance
  PLW
  ==     KL_python_process_active
  IFF
    PS     KL_mode_asynchronous
    PS     KL_memory_8_MB
    PS     KS_Process_path
    PS     KS_Process_name
    PS     KS_Process_parameter
    CM     9295                        ;Start python process
    PL     DL_Error_9295
    PL     DL_Exit_9295

    M_display_module_error
  ENDI

  ; Start spawn receive
  L      DL_Spawn_receive_Ident
  ==     K+0
  IFT
    SPAWN  Spawn_Server_Receive
    =      DL_Spawn_receive_Ident
    ==     K-1
    S      PN_error_submit_spawn_queue_full
  ENDI

  L      DL_Spawn_receive_Ident
  >      K+0
  S      ML_TCP_IP_server_started



EM

;--------------------------------------------------------
; Server Functions
;--------------------------------------------------------

; Endless loop for Spawn (wait for Events)
LBL Spawn_Server_Receive

  REPEAT
    PS     STG_PLC_server_status_receive.Handle
    PS     KL_with_wait
    CM     9373
    PL     STG_PLC_server_status_receive.AccessReturn

    L      STG_PLC_server_receive.Client
    >      K+0
    IFT

      L      STG_PLC_server_receive.Client
      =      STG_PLC_server_transmit.Client

      L      STG_PLC_server_receive.Function
      =      STG_PLC_server_transmit.Function

      CASE   STG_PLC_server_receive.Function
        CM     Ping                    ;00
        CM     Axis_Positions          ;01
        CM     Machine_Status          ;02
        CM     Function_03             ;03
        CM     Function_04             ;04
      ENDC

      CM     Server_send
    ENDI

    L      STG_PLC_server_status_receive.AccessReturn
    >      K+0
    O      PN_error_submit_spawn_queue_full
  UNTILT

  L      K+0
  =      DL_Spawn_receive_Ident
EM


;----------------------------
LBL Ping

EM

;----------------------------
LBL Axis_Positions

  L      DG_position_axis_X_ref
  =      STG_PLC_server_transmit.D_1

  L      DG_position_axis_Y_ref
  =      STG_PLC_server_transmit.D_2

  L      DG_position_axis_Z_ref
  =      STG_PLC_server_transmit.D_3

EM

;----------------------------
LBL Machine_Status

  L      MG_control_operational
  =      STG_PLC_server_transmit.M_1

  L      MG_spindle_stand_still
  =      STG_PLC_server_transmit.M_2

  L      MG_automatic_mode
  =      STG_PLC_server_transmit.M_3

  L      MG_TC_auto_active
  =      STG_PLC_server_transmit.M_4

  L      MG_guard_open
  =      STG_PLC_server_transmit.M_5

  L      PN_W524_servo_drive_release_axes_groups
  =      STG_PLC_server_transmit.D_1

  L      NP_D1104_axes_in_position
  =      STG_PLC_server_transmit.D_2

EM

;----------------------------
LBL Function_03

;; Data complete
;  L
;  =      STG_PLC_server_transmit.M_1
;
;  L
;  =      STG_PLC_server_transmit.D_1

EM

;----------------------------
LBL Function_04

;; Data complete
;  L
;  =      STG_PLC_server_transmit.M_1
;
;  L
;  =      STG_PLC_server_transmit.D_1

EM


;----------------------------
LBL Server_send

  PS     STG_PLC_server_status_transmit.Handle
  PS     KL_without_wait_overwrite_data
  CM     9374
  PL     STG_PLC_server_status_transmit.AccessReturn

  L      STG_PLC_server_status_transmit.AccessReturn
  >      K+0
  S      PN_error_TCP_server_connect_error
  RN     PN_error_TCP_server_connect_error

EM

;----------------------------
LBL Process_monitor

  PS     KS_Process_name
  CM     9297
  PLW
  ==     KL_python_process_not_active
  IFT
    L      MG_marker_one
    R      ML_TCP_IP_server_started
    S      PN_error_TCP_server_connect_error
  ENDI

EM
