ABAP-串列埠通訊-道閘裝置 VB-串列埠通訊詳解
最近SAP系統需要與道閘裝置整合,通過串列埠通訊模式控制道閘欄杆升降,在此將開發過程中的思路及問題點做個備註。
一.相關裝置
道閘裝置型號:富士智慧FJC-D618
串列埠模組:康耐德 C2000-A1-PXD0020-BX1
轉換器:DTECH DT-9020 RS-232<=>RS485
二.裝置整合方式:呼叫系統元件MSComm32.OCX
64位檔案路徑:C:\Windows\SysWOW64\MSCOMM32.OCX
若找不到MSCOMM32.OCX元件,可以網上下載。
三.元件MSComm32.OCX 相關屬性說明
路徑:VB-串列埠通訊詳解
四.SAP與道閘裝置整合操作步驟
1.控制元件註冊
a.將MSComm32.OCX檔案放置在 C:\Windows\SysWOW64 路徑下(32位作業系統放在 C:\Windows\System32 )
b.控制元件註冊:管理員許可權執行CMD->Regsvr32 C:\Windows\SysWOW64\MSCOMM32.OCX
c.修改登錄檔(這一步感覺可有可無):
c1.執行CMD->Regedit
c2.在HKEY_CLASSES_ROOT\Licenses下新建項:4250E830-6AC2-11cf-8ADB-00AA00C00905 修改數值資料:kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun
d.重啟電腦,控制元件啟用
2.SAP系統應用程式維護
a.查詢CLSID:執行CMD->Regedit,搜尋“MSCOMMLib.MSComm.1”,獲取CLSID:{648A5600-2C6E-101B-82B6-000000000014}
b.登入GUI,T-CODE:SOLE 修改-新條目
OLE 應用程式: MSCOMMLIB.MSCOMM.1
版本號:
CLSID: {648A5600-2C6E-101B-82B6-000000000014}
CLSID 庫型別:
OLE 物件名:
型別資訊程式碼:
包含程式:
語言:
檢查許可權:
文字:
3.SAP直接呼叫MSCOMM32.OCX控制元件(16進位制多條記錄無法傳送下位機)
*&---------------------------------------------------------------------* *& Report ZRICO_MSCOMM *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* report zrico_mscomm. data: lv_cur type string, lv_rst type string, lv_buffer type i. data: lo_dz type ole2_object. selection-screen begin of block block1 with frame title text-001. parameters: p_port type char30 default '1', p_sett type char30 default '9600,n,8,1', p_inpt type char30 default '0', p_open type char30 default '1', p_intxt type string default '01 01 00 64 00 01 BC 15', p_ottxt type string. selection-screen end of block block1. start-of-selection. call function 'AUTHORITY_CHECK_OLE' exporting program = sy-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 lo_dz 'MSCOMMLib.MSComm.1'. if sy-subrc <> 0. raise no_create_object. endif. "初始引數 set property of lo_dz 'CommPort' = p_port. set property of lo_dz 'Settings' = p_sett. set property of lo_dz 'InputMode' = 1. set property of lo_dz 'InputLen' = p_inpt. set property of lo_dz 'InBufferSize' = 1024. set property of lo_dz 'OutBufferSize' = 512. "開啟串列埠 set property of lo_dz 'PortOpen' = p_open. "傳送資料-多條記錄-16進位制 "此處不知如何處理:需將16進位制轉換為字元陣列Byte[]方式傳送至下位機 "若將16進位制轉換為10進位制內表傳值則直接Dump,若直接傳輸16進位制字串則無返回值 set property of lo_dz 'Output' = p_intxt. "接收資料 do 40 times. get property of lo_dz 'InBufferCount' = lv_buffer. if lv_buffer > 0. get property of lo_dz 'Input' = lv_rst. exit. endif. enddo. "關閉串列埠 set property of lo_dz 'PortOpen' = 0. "釋放 free object lo_dz.
3.1因傳送16進位制資料為多條記錄,控制元件MSCOMM傳送資料型別為Byte[],ABAP無法將16進位制數轉換為位元組陣列,若轉換為內表傳輸則直接Dump,最終無解。
3.2ABAP無法一次傳送多條記錄至下位機,則改用其他方式處理(詳見4):
a.用VB建立DLL檔案,引用MSCOMM32.OCX控制元件並建立多個類方法。
b.系統註冊DLL檔案
c.SAP呼叫DLL檔案,並訪問自定義方法類
4.解決16進位制多條記錄傳送問題
4.1用VB建立DLL檔案,封裝MSCOMM32.OCX控制元件相關屬性及自定義方法
4.2SAP呼叫自定義類檔案 MSCommPrj.DLL
a.註冊檔案MSCommPrj.DLL
執行CMD->regsvr32 檔案路徑
b.ABAP呼叫類檔案 :16進位制多條記錄用"&"符號拼接為字串
*&---------------------------------------------------------------------* *& Report ZRICO_MSCOMMPRJ *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* report zrico_mscommprj. data: gv_rst type string. data: go_mscomm type ole2_object. selection-screen begin of block block1 with frame title text-001. parameters: p_port type char10 default '1', p_sett type char30 default '9600,n,8,1', p_mode type char10 default '0', p_time type char10 default '100', p_data type char200 default '01 01 00 64 00 01 BC 15 & 01 0F 00 64 00 01 01 FF 1F 1F & 01 01 00 64 00 01 BC 15 & 01 0F 00 64 00 01 01 00 5F 5F'. selection-screen end of block block1. start-of-selection. call function 'AUTHORITY_CHECK_OLE' exporting program = sy-repid activity = sabc_act_call application = 'MSCommPrj.msCommCls' 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 go_mscomm 'MSCommPrj.msCommCls'. if sy-subrc <> 0. raise no_create_object. endif. "初始化引數 call method of go_mscomm 'frm_initial_parameters' = gv_rst exporting #1 = p_port #2 = p_sett #2 = p_mode . write:/10 gv_rst. "開啟串列埠 call method of go_mscomm 'frm_open_serialport' = gv_rst. write:/10 gv_rst. "傳送資料 call method of go_mscomm 'frm_send_data' = gv_rst exporting #1 = p_mode #2 = p_time #2 = p_data . write:/10 gv_rst. "接收資料 call method of go_mscomm 'frm_receive_data' = gv_rst exporting #1 = p_mode. write:/10 gv_rst. "關閉串列埠 call method of go_mscomm 'frm_close_serialport' = gv_rst. write:/10 gv_rst. free object go_mscomm.