Sample ABAP code

You are here:

To use RFC HU_GET_RFC_DATA from your own ABAP’s, there are two steps.

  1. If you have defined packing station profiles in customizing (transaction HUPAST_C), then you can use the function module read_packstation to determine the profile based on the hostname of the user’s PC
  2. Use the RFC call HU_GET_RFC_DATA in conjunction with ERP-Scale to determine the current weight from the serial device
    Note: You can use the packing station profile with ERP-Scale even if you haven’t activated handling unit management in SAP.
Determine Packing station profile based on hostname of user’s PC
FORM read_packstation.

  statics sf_terminal(20).
  DATA: lt_profile TYPE TABLE OF hupast_t.
* Get the Hostname from the PC.
  CALL FUNCTION 'TH_USER_INFO'
    IMPORTING
      terminal = sf_terminal.
* Prerequisite only one profile per PC
  SELECT * FROM hupast_t CLIENT SPECIFIED INTO TABLE lt_profile
        WHERE terminal = sf_terminal AND client = sy-mandt.
  IF NOT sy-subrc IS INITIAL.
    EXIT.
  ENDIF.
  READ TABLE lt_profile TRANSPORTING NO FIELDS INDEX 2.
  IF sy-subrc IS INITIAL.
    EXIT.
  ENDIF.
  READ TABLE lt_profile INTO gs_profile INDEX 1.
* PERFORM set_profile.
ENDFORM.                    "read_packstation
Call to RFC function HU_GET_RFC_DATA
data: lt_weight type table of hum_scale,
       ls_weight type hum_scale,
       L_RFC_DESTINATION LIKE RFCDISPLAY-RFCDEST.
  
  l_rfc_destination = RFC_FOR_SCALE.  "RFC Destination
  ls_weight-waage = NAME_OF_SCALE.    "Name of scale in ERP-Scale
  ls_weight-brgew = '0.000'.
  ls_weight-gewei = space.
  append ls_weight to lt_weight.

  CALL FUNCTION 'HU_GET_RFC_DATA' destination l_rfc_destination
    TABLES
      ET_WEIGHT             = lt_weight
    EXCEPTIONS 
      COMMUNICATION_FAILURE = 1  MESSAGE MSG_TEXT
      SYSTEM_FAILURE        = 2  MESSAGE MSG_TEXT.
  .
  if not sy-subrc is initial.
    concatenate text-040 l_rfc_destination msg_text
                   into msg_text separated by space.
    message s001(vl) with msg_text.
  leave to transaction 'ZTESTWEIGH'.
  endif.
  clear ls_weight.
  read table lt_weight into ls_weight index 1.
  if ls_weight-brgew is initial.
    message e203(hudialog) with l_rfc_destination.
  endif.
  catch system-exceptions CONVT_NO_NUMBER = 9 .
    p_weight = ls_weight-brgew.
  endcatch.
Complete example
*&---------------------------------------------------------------------*
*& Report  ZTESTSCALE                                                  *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Author: Pocket Programs 2013                                        *
*& =============================                                       *
*& Example ABAP for use with ERP-Scale see www.pocketprograms.com             *
*&                                                                     *
*& Requires transaction ZTESTWEIGH to be created to run this report    *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZPP_TEST_ERPSCALE                            .

TABLES: sscrfields.

parameter: p_packst type HUM_PROFILE default 'ERPSCALE'.
* Declaration of sel screen buttons
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON (30) w_button USER-COMMAND BUT1.
SELECTION-SCREEN END OF LINE.
parameter: p_weight(20) MODIF ID DSP.
parameter: p_unit(20) MODIF ID DSP.
parameter: p_text(20) MODIF ID DSP.
parameter: p_dummy1(20) MODIF ID DSP.
parameter: p_dummy2(20) MODIF ID DSP.

data: gs_profile type hupast_t.

data: lt_weight type table of hum_scale,
      ls_weight type hum_scale,
      MSG_TEXT(120) TYPE C, "Message text
      L_RFC_DESTINATION LIKE RFCDISPLAY-RFCDEST.
data: l_weight like vekp-brgew.
data: p_msg(80).

*=================================================
initialization.

* Make result fields read-only
  LOOP AT SCREEN.
    IF SCREEN-GROUP1 = 'DSP'.
      SCREEN-INPUT = 0.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.
* Add displayed text string to buttons
  w_button = 'Get weight using ERP-Scale'.
* Determine packing station profile from PC hostname
  perform read_packstation.
  p_packst = gs_profile-packing_station.

*================================================
AT SELECTION-SCREEN.
* Check if button has been pressed
  if sscrfields-ucomm eq 'BUT1'.
    perform read_profile using p_packst.
* rfc-destination aus Customizing:
    l_rfc_destination = gs_profile-RFC_FOR_SCALE.
    ls_weight-waage = gs_profile-NAME_OF_SCALE.

    if l_rfc_destination is initial.
      clear: p_weight, p_unit, p_text, p_dummy1, p_dummy2.
      message e203(hudialog) with l_rfc_destination.
    endif.
* Function module must be called with at least one
* entry in the table
    ls_weight-brgew = '0.000'.
    ls_weight-gewei = space.
    append ls_weight to lt_weight.

    CALL FUNCTION 'HU_GET_RFC_DATA' destination l_rfc_destination
      TABLES
        ET_WEIGHT             = lt_weight
      EXCEPTIONS "<< begin note 205878
        COMMUNICATION_FAILURE = 1  MESSAGE MSG_TEXT
        SYSTEM_FAILURE        = 2  MESSAGE MSG_TEXT.

    if not sy-subrc is initial.
      concatenate text-040 l_rfc_destination msg_text
                     into msg_text separated by space.
      message s001(vl) with msg_text.
      clear: p_weight, p_unit, p_text, p_dummy1, p_dummy2.
      return.
    endif.
    clear ls_weight.
    read table lt_weight into ls_weight index 1.
    if ls_weight-brgew is initial.
      message e203(hudialog) with l_rfc_destination.
    endif.
    catch system-exceptions CONVT_NO_NUMBER = 9 .
      p_weight = ls_weight-brgew.
    endcatch.
    if sy-subrc = 9.
      message s000(zz) with ls_weight.
    endif.
    concatenate p_weight ls_weight-GEWEI into p_msg
    separated by space.
*    message s000(zz) with p_msg.
    p_unit = ls_weight-GEWEI.
    p_text = ls_weight-text.
    p_dummy1 = ls_weight-dummy1.
    p_dummy2 = ls_weight-dummy2.
  endif.
*==============================================================
at SELECTION-SCREEN OUTPUT.
* Make result fields read-only
  LOOP AT SCREEN.
    IF SCREEN-GROUP1 = 'DSP'.
      SCREEN-INPUT = 0.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.
*==============================================================

*&---------------------------------------------------------------------*
*&      Form  READ_PROFILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM read_packstation.

  statics sf_terminal(20).
  DATA: lt_profile TYPE TABLE OF hupast_t.
* Get hostname from PC
  CALL FUNCTION 'TH_USER_INFO'
    IMPORTING
      terminal = sf_terminal.
* prerequisite there is only one profile per PC
  SELECT * FROM hupast_t CLIENT SPECIFIED INTO TABLE lt_profile
        WHERE terminal = sf_terminal AND client = sy-mandt.
  IF NOT sy-subrc IS INITIAL.
    EXIT.
  ENDIF.
  READ TABLE lt_profile TRANSPORTING NO FIELDS INDEX 2.
  IF sy-subrc IS INITIAL.
    EXIT.
  ENDIF.
  READ TABLE lt_profile INTO gs_profile INDEX 1.
* PERFORM set_profile.
ENDFORM.                    "read_packstation

*&---------------------------------------------------------------------*
*&      Form  READ_PROFILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM read_profile using p_st.

  DATA: lt_profile TYPE TABLE OF hupast_t.
* prerequisite there is only one profile per PC
  SELECT * FROM hupast_t CLIENT SPECIFIED INTO TABLE lt_profile
        WHERE PACKING_STATION = p_st AND client = sy-mandt.
  IF NOT sy-subrc IS INITIAL.
    EXIT.
  ENDIF.
  READ TABLE lt_profile TRANSPORTING NO FIELDS INDEX 2.
  IF sy-subrc IS INITIAL.
    EXIT.
  ENDIF.
  READ TABLE lt_profile INTO gs_profile INDEX 1.
* PERFORM set_profile.
ENDFORM.                    "read_profile

 

Was this article helpful?
Dislike 0
Views: 43
© Copyright - Pocket Programs