這是幾年前作的了,一直都不想分享出來,後來想一想爲了可以給你們點想法,獻出來了。。。node
這是一個電腦讀稱的方法,通常用COMM口鏈接的電子設備均可參考。緩存
若是是對串口參數不肯定的,能夠網上找個串口測試工具,輕鬆測出串口參數app
網上搜索MSCOMM32.OCX工具
1.將上面的MSCOMM32.OCX保存到系統目錄下C:\Windows\SysWOW64\MSCOMM32.OCX測試
而後註冊:REGSVR32 C:\Windows\SysWOW64\MSCOMM32.OCXspa
2.修改註冊表:code
REGEDIT對象
在HKEY_CLASSES_ROOT\Licenses下建一項:blog
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
以字符流傳過來的,在處理的時候,本身看着辦了。。。