SAP连接电脑串口读数(电子称,磅等数据读取)

这是几年前做的了,一直都不想分享出来,后来想想为了能够给大家点想法,献出来了。。。

这是一个电脑读称的方法,一般用COMM口连接的电子设备都可参考。

 

网上搜索MSCOMM32.OCX

1.将上面的MSCOMM32.OCX保存到系统目录下C:\Windows\SysWOW64\MSCOMM32.OCX

然后注册:REGSVR32 C:\Windows\SysWOW64\MSCOMM32.OCX

 

2.修改注册表:

REGEDIT

在HKEY_CLASSES_ROOT\Licenses下建一项:

4250E830-6AC2-11cf-8ADB-00AA00C00905

修改数值数据:kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun

重新启动电脑,这样COMM组件就能使用了

以上是在PC上配置的数据。

 

登陆GUI

T-code: SOLE

新建条目:

OLE 应用程序         MSCOMMLIB.MSCOMM.1

版本号

类标识               {648A5600-2C6E-101B-82B6-000000000014}

CLSID 库类型

OLE 对象名

类型信息键值

包含程序

语言

检查权限

文本

 

FUNCTION z_bc_get_balance.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(MODE) TYPE  I DEFAULT 0
*"     REFERENCE(COMMPORT) TYPE  I DEFAULT 1
*"     VALUE(SETTINGS) TYPE  C DEFAULT '1200,N,8,1'
*"     REFERENCE(I_INPUT) TYPE  C OPTIONAL
*"  EXPORTING
*"     REFERENCE(E_OUTPUT) TYPE  C
*"  EXCEPTIONS
*"      NO_CREATE_OBJECT
*"----------------------------------------------------------------------

  TYPE-POOLS:sabc.
  INCLUDE ole2incl.
  DATA pos TYPE i.
  CLEAR: pos,balance,value,e_output.
  PERFORM prm_get_settings CHANGING settings.
*  WAIT UP TO 3 SECONDS.
  PERFORM prm_init.
  PERFORM prm_open_port USING commport settings.
  IF mode = 0."读取串口数据
    PERFORM prm_read_port CHANGING e_output.
  ELSEIF mode = 1."写串口数据
    PERFORM prm_write_port USING i_input CHANGING e_output.
  ENDIF.
  PERFORM prm_final.
  SEARCH balance FOR '+'.
  IF sy-subrc = 0.
    pos = sy-fdpos.
    SHIFT balance BY ( pos + 1 ) PLACES.
  ENDIF.
  SEARCH balance FOR 'g'.
  IF sy-subrc = 0.
    pos = sy-fdpos.
    balance = balance+0(pos).
    CONDENSE balance NO-GAPS.
    e_output = balance.
  ENDIF.
  COMMIT WORK
ENDFUNCTION.



此块为配置接口类型模块(自建表用IP地址来配置相应的comm口参数)
*****************************************************************************
FORM prm_get_settings  CHANGING p_settings.
  DATA addr TYPE ni_nodeaddr.
  CLEAR addr.
  CALL FUNCTION 'TH_USER_INFO'
    IMPORTING
      addrstr = addr.
  SELECT SINGLE setting INTO p_settings FROM zbctaddr WHERE addr = addr.
  IF sy-subrc = 0.
    CLEAR addr.
  ENDIF.
ENDFORM.                    " PRM_GET_SETTINGS



初始化接口对象
******************************************************************************
FORM prm_init .
  DATA:
    wa_repid LIKE sy-repid.
  CLEAR wa_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.                    " PRM_INIT


接口打开
***************************************************************************
FORM prm_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.                    " PRM_OPEN_PORT



读取接口数据:因为接口有缓存所以每次读取时都是去掉前面20次的数据
而且每次读取的数据都是不完整的,所以需要将多次读取的数据连接起来,然后截取
****************************************************************************
FORM prm_read_port  CHANGING e_output.
  DATA:
    wa_buffer TYPE i,
    inde      TYPE i.
  DO 70 TIMES.
     inde = inde + 1.
    GET PROPERTY OF o_obj 'InBufferCount' = wa_buffer.
    IF wa_buffer > 0.
      GET PROPERTY OF o_obj 'Input' = e_output.
      IF inde < 20.
        ELSE.
          CONCATENATE balance e_output INTO balance.
      ENDIF.
    ENDIF.
  ENDDO.
  CLEAR:wa_buffer,inde.
ENDFORM.                    " PRM_READ_PORT



往串口写数据,因为没有需求所以没做测试
*********************************************************************
FORM prm_write_port  USING    i_input
                     CHANGING e_output.
  DATA:
    wa_buffer TYPE i.
  SET PROPERTY OF o_obj 'Output' = i_input.
  DO 40 TIMES.
    GET PROPERTY OF o_obj 'InBufferCount' = wa_buffer.
    IF wa_buffer > 0.
      GET PROPERTY OF o_obj 'Input' = e_output.
      EXIT.
    ENDIF.
  ENDDO.
ENDFORM.                    " PRM_WRITE_PORT


读数结束,关闭串口,释放对象,清空变量
************************************************************************
FORM prm_final .
  SET PROPERTY OF o_obj 'PortOpen' = 0.
  FREE OBJECT o_obj.
  CLEAR o_obj.
  COMMIT WORK.
ENDFORM.                    " PRM_FINAL

 

你可能感兴趣的:(SAP连接电脑串口读数(电子称,磅等数据读取))