Sunday 7 July 2019

Read COMM port using ABAP w/o third party software

Case: Weighbridge scale sends the weight data to COMM Port.

Earlier we had to write a .NET program using SAP.NET Connector (Using VB.NET) and then from SAP side we had to create RFC so that the COMM port data gets utilized in SAP.

Now there is no need to use the third party software (VB.NET, C# etc) Using the following steps we can directly fetch the weight data at the COMM Port to SAP:

SAP ABAP Study Materials, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP Tutorial and Materials

Step 1: Create a Function Module (code given below)

Step 2: Register the Windows activeX control MSCOM32.OCX on the client PC where the Weighbridge’s serial port is connected.

Step 3: Implement this control in Transaction SOLE in SAP (Create an entry MSCOMMLIB.MSCOMM.1 and enter the CLSID.        {648A5600-2C6E-101B-82B6-000000000014}

Step 4: Active this MSCOMM32.OCX with Licence Key on the client PC where the Weighbridge’s serial port is connected.

Open RUN execute : regedit

Go to u201CHKEY_CLASSES_ROOT\Licenses\u201D

Create new key (Folder) name with ‘4250E830-6AC2-11cf-8ADB-00AA00C00905’

Give the default VALUE: kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun

Restart the system and run the FM. if the data is coming on the serial port then you will get the result.

FM Code:


FUNCTION z_serial_comport.
"-------------------------------------------------------------------------------"
""""Local Interface:
""  IMPORTING
""     REFERENCE(MODE) TYPE  I DEFAULT 0
""     REFERENCE(COMMPORT) TYPE  I DEFAULT 1
""     REFERENCE(SETTINGS) TYPE  C DEFAULT '2400,N,8,1'
""     REFERENCE(OUTPUT) TYPE  C OPTIONAL
""  EXPORTING
""     REFERENCE(INPUT) TYPE  C
""  EXCEPTIONS
""      NO_CREATE_OBJECT
"-------------------------------------------------------------------------------"
  TYPE-POOLS: sabc.
  INCLUDE ole2incl.
  PERFORM init.
  PERFORM open_port USING commport settings.
  IF mode = 0.
    PERFORM read_port
      CHANGING input.
  ENDIF.
  IF mode = 1.
    PERFORM write_port
      USING output
      CHANGING input.
  ENDIF.
  PERFORM final.
ENDFUNCTION.
DATA: o_obj TYPE ole2_object.
"-------------------------------------------------------------------------------"
FORM init.
  DATA:
    wa_repid LIKE sy-repid.
  wa_repid = sy-repid.
  CALL FUNCTION 'AUTHORITY_CHECK_OLE'
    EXPORTING
      program          = wa_repid
      activity         = sabc_act_call
      application      = 'MSCOMMLIB.MSCOMM.1'
    EXCEPTIONS
      no_authority     = 1
      activity_unknown = 2
      OTHERS           = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  CREATE OBJECT o_obj 'MSCOMMLib.MSComm.1'.
  IF sy-subrc <> 0.
    RAISE no_create_object.
  ENDIF.
ENDFORM.                    " Init
"-------------------------------------------------------------------------------"
FORM open_port USING commport settings.
  SET PROPERTY OF o_obj 'CommPort' = commport.
  SET PROPERTY OF o_obj 'Settings' = settings.
  SET PROPERTY OF o_obj 'InputLen' = 0.
  SET PROPERTY OF o_obj 'PortOpen' = 1.
ENDFORM.                   "open_port
"-------------------------------------------------------------------------------"
FORM read_port
  CHANGING input.
  DATA:
    wa_buffer TYPE i.
  DO 10 TIMES.
    GET PROPERTY OF o_obj 'InBufferCount' = wa_buffer.
    IF wa_buffer > 0.
      GET PROPERTY OF o_obj 'Input' = input.
      EXIT.
    ENDIF.
  ENDDO.
ENDFORM.                    " read_port
"-------------------------------------------------------------------------------"
FORM write_port
      USING output
      CHANGING input.
  DATA:
    wa_buffer TYPE i.
  SET PROPERTY OF o_obj 'Output' = output.
  DO 10 TIMES.
    GET PROPERTY OF o_obj 'InBufferCount' = wa_buffer.
    IF wa_buffer > 0.
      GET PROPERTY OF o_obj 'Input' = input.
      EXIT.
    ENDIF.
  ENDDO.
ENDFORM.                    "write_port
"-------------------------------------------------------------------------------"
FORM final.
  SET PROPERTY OF o_obj 'PortOpen' = 0.
  FREE OBJECT o_obj.
ENDFORM.                    " final

No comments:

Post a Comment