这是几年前做的了,一直都不想分享出来,后来想想为了能够给大家点想法,献出来了。。。
这是一个电脑读称的方法,一般用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
对读数做了个简单的处理
FORM PRM_READ_PORT02 CHANGING E_OUTPUT. DATA: WA_BUFFER TYPE I, INDE TYPE I,LV_CHAR TYPE C. DATA:LV_STR TYPE STRING VALUE '0123456789.'. DATA:LV_STR2 TYPE STRING. DATA:LV_STR3 TYPE STRING. DATA POS TYPE I. CLEAR:LV_CHAR,LV_STR2,INDE,LV_STR3,POS. WHILE LV_CHAR IS INITIAL. GET PROPERTY OF O_OBJ 'InBufferCount' = WA_BUFFER. IF WA_BUFFER > 0. GET PROPERTY OF O_OBJ 'Input' = E_OUTPUT. SEARCH E_OUTPUT FOR '+'. IF SY-SUBRC = 0. POS = SY-FDPOS. SHIFT E_OUTPUT BY ( POS + 1 ) PLACES. ELSE. CONTINUE. ENDIF. SEARCH E_OUTPUT FOR 'g'. IF SY-SUBRC = 0. POS = SY-FDPOS. E_OUTPUT = E_OUTPUT+0(POS). CONDENSE E_OUTPUT NO-GAPS. E_OUTPUT = E_OUTPUT. ELSE. CONTINUE. ENDIF. * SEARCH E_OUTPUT FOR '.'. * IF SY-SUBRC = 0. * ELSE. * CONTINUE. * ENDIF *. CLEAR:LV_STR2. MOVE E_OUTPUT TO LV_STR2. IF LV_STR2 CO LV_STR. * INDE = INDE + 1. * IF INDE = 1. * MOVE E_OUTPUT TO LV_STR3. * ELSEIF INDE = 2. * CLEAR:INDE. * IF LV_STR2 = LV_STR3. LV_CHAR = 'X'. * ENDIF. * ENDIF. ENDIF. ENDIF. ENDWHILE. CLEAR:WA_BUFFER,INDE. ENDFORM. " PRM_READ_PORT02
以字符流传过来的,在处理的时候,自己看着办了。。。