Skip to Content
Author's profile photo S M Firoz Ashraf

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:

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. Please click here to get detailed steps on how to register/activate this control.

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

You may find additional help from the following links:

http://forums.sdn.sap.com/thread.jspa?threadID=1766170

http://www.sapfans.com/forums/viewtopic.php?f=13&t=145782&start=0&st=0&sk=t&sd=a

Regards,

Firoz.

Assigned tags

      13 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo sarvesh rai
      sarvesh rai

      Hi Firoz,

      I did all the activities as per your above Blog but I am facing below issue,

      In The program    SET PROPERTY OF o_obj 'PortOpen' = 1.I am getting sy-subrc = 3.

      I have all ports open. Firewall Off.

      I am using COMM1 port and passed following  parameters  9600,N,8,1.Please let me guide from here. Its very urgent.

      Thanks & Regards

      Sarvesh Rai

      Author's profile photo Firoz Ashraf
      Firoz Ashraf
      Blog Post Author

      Hi Sarvesh,

      Ideally after setting the property it should work. It could be due to authorization issue. Just login from admin account and check.

      Regards,

      Firoz.

      Author's profile photo Former Member
      Former Member

      Hello Firoz,

      Is it possible to send hex data to a device connected on the serial port using this function module? I started a new thread here: Write hex string to serial port directly from ABAP

      Thanks,

      Stefan

      Author's profile photo Firoz Ashraf
      Firoz Ashraf
      Blog Post Author

      Hi Stefan,

      I believe you have solved this issue by converting the XSTRING to STRING using the CL_ABAP_CONV_IN_CE Class (as I can see this in your post).

      Cheers!

      Author's profile photo Former Member
      Former Member

      Hi Firoj,

      Could you please help me here ?

      GET PROPERTY OF o_obj 'Input' = input. ---------> Is giving SUBRC 4----> Need your help here

      I stuck here, above statement is giving subrc 4. Please see below my code line.


      FORM read_port

      CHANGING input.

      DATA:

      wa_buffer TYPE i.

      DO 10 TIMES.

      GET PROPERTY OF o_obj 'InBufferCount' = wa_buffer. ----------------------------------> Is SUCCESS

      IF wa_buffer > 0.

      GET PROPERTY OF o_obj 'Input' = input. ---------> Is giving SUBRC 4----> Need your help here

      EXIT.

      ENDIF.

      ENDDO.

      ENDFORM. " read_port

      -------------------------------------------------

      Author's profile photo Sam Indra
      Sam Indra

      Hi Firoz,

       

      I have a problem doing step 4

      Open RUN execute : regedit

      Go to u201CHKEY_CLASSES_ROOT\Licenses\u201D

      What is u201CHKEY_CLASSES_ROOT\Licenses\u201D ?

      I cannot find it anywhere in regedit

      could you please explain this step in more detail? or maybe there was a typo?

      1. Have you tried to send data to other system through mscomm ? And not just read data?

      thanks,

      Sam

      Author's profile photo Sudhakar Varma Y
      Sudhakar Varma Y

      Hi Firoz,

      I know it is quite old blog but now we have request to get the data from weighting scale from COMM port. We are currently working on SAP HANA Implementation. I have implemented the code as given by you. Below are issue I had during implementation.

      • Don't have the location u201CHKEY_CLASSES_ROOT\Licenses\u201D. So created under HKEY_CLASSES_ROOT\Licenses\
      • Data is not returning by functional module.

      Please advise.

       

      Thanks & Regards,

      Varma

      Author's profile photo Guillermo Jose Ponte Hernandez
      Guillermo Jose Ponte Hernandez

      Hi, did you find the solution? I have the same problem

      Author's profile photo Ketankumar Patel
      Ketankumar Patel

      Hi..I have implemented the Solution, there are no errors ,does not returns any value

      DO 10 TIMES.

      GET PROPERTY OF o_obj ‘InBufferCount’ = wa_buffer. --> Value 0

      IF WA_BUFFER GT 0 -- > fails

      Author's profile photo Satyanarayana Madarapu
      Satyanarayana Madarapu

      Hi Firoz,

      I have implemented the given solution, but the Function Module is returning some junk characters along with Weight value. And it is varying each and every time and unable to suppress as it is not fixed value. I am using ESSAE DS-852 model Weighing machine. Please find the attached images and suggest.

       

      FM Output - 1

      FM%20Output%201

       

      FM Output - 2

      FM%20output%203

      Thanks & Regards,

      Satya.

      Author's profile photo Jose Noel Baez
      Jose Noel Baez

      Hi all,

      in step 4), the correct path is: HKEY_CLASSES_ROOT\Licenses\

      not u201CHKEY_CLASSES_ROOT\Licenses\u201D

      don't confuse with strings u201C and u201D because are ascii characters for double quotes (").

      You can also use the path HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Licenses\ as stated in this link.  

      https://github.com/davidanger/MSCOMM32

      In my tests everything worked successfully.

      Author's profile photo wz zhang
      wz zhang

      Very useful!

      and many Thanks!

      Author's profile photo VAMSIKRISHNA SRIRANGAM
      VAMSIKRISHNA SRIRANGAM

      Hi Everyone,

      we are also reading the WB Weight Through Com port but on specific PC sometimes Port is not accessible we don't know why Once the client system maintenance person change the Comport Value in REGEDIT & then restart the system it is working but again getting the issues some times is there any stable solution for reading the Weigh Bridge Weight ? also some times the BUFFER is returning 0 value instead of string of weights..

      we requested for API from WB Vendor but he is saying he can't provide, is there any way to read WB Weight Directly from SAP. Also will it be possible to update the system com port from  SAP incase of Port is not accessible ( With out restarting system ) so that operations can update the new comport & continue operations.

      Also is there anyway we can find what are all the different applications that are accessing this particular port so that we can terminate those applications.