2010年(66)
分类:
2010-03-25 09:34:38
具体程序实现如下:
REPORT ZPM_IFSCADA MESSAGE-ID I4.
DATA:BEGIN OF SCADA OCCURS 0,
BDZ TYPE CHAR32,
DY TYPE CHAR8,
JG TYPE CHAR8,
JGMC TYPE CHAR64,
LNAME TYPE CHAR8,
VALUE TYPE CHAR30,
END OF SCADA.
DATA: BEGIN OF COMMANDS OCCURS 0,
CMD(100) TYPE C,
END OF COMMANDS.
DATA:HANDLE TYPE I.
DATA: BEGIN OF TAB OCCURS 100,
LINE(255),
END OF TAB.
DATA: BEGIN OF RESULT OCCURS 0,
LINE(200) TYPE C,
END OF RESULT.
DATA: W_80C(80) TYPE C,
W_PATH(80) TYPE C,
CNS_DIR(60) TYPE C .
DATA: COM(120) .
DATA: FTPFILE LIKE RLGRAP-FILENAME.
DATA:KEY TYPE I VALUE 26101957,
SLEN TYPE I.
DATA: ITABOUT(1200) TYPE C OCCURS 0 WITH HEADER LINE.
*FTP服务器设置
SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.
*服务器
PARAMETERS:P_HOST(16) TYPE C DEFAULT '192.168.1.10' LOWER CASE OBLIGATORY ,
*服务器文件路径
P_PATH LIKE RLGRAP-FILENAME LOWER CASE OBLIGATORY DEFAULT '/'. "'/realdata/'.
SELECTION-SCREEN END OF BLOCK BLK1.
*FTP服务器登录用户信息
SELECTION-SCREEN BEGIN OF BLOCK BLK2 WITH FRAME TITLE TEXT-002.
PARAMETERS:P_USER(20) TYPE C LOWER CASE,
P_PASS(20) TYPE C LOWER CASE.
SELECTION-SCREEN END OF BLOCK BLK2.
*SAP服务器设置
SELECTION-SCREEN BEGIN OF BLOCK BLK3 WITH FRAME TITLE TEXT-003.
*SAP服务器文件路径(临时存放文件)
PARAMETERS:S_PATH LIKE RLGRAP-FILENAME LOWER CASE OBLIGATORY
DEFAULT '/usr/sap/trans/public/scada/'.
SELECTION-SCREEN END OF BLOCK BLK3.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF SCREEN-NAME = 'P_PASS'.
SCREEN-INVISIBLE = 1.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
START-OF-SELECTION.
DEFINE UPLOAD_DATA_FROM_SAP.
CLEAR &2. REFRESH &2.
OPEN DATASET &1 FOR INPUT IN TEXT MODE ENCODING NON-UNICODE.
IF SY-SUBRC = 0.
DO.
READ DATASET &1 INTO &2.
IF SY-SUBRC <> 0.
EXIT.
ELSE.
APPEND &2.
CLEAR &2.
ENDIF.
ENDDO.
CLOSE DATASET &1.
ENDIF.
END-OF-DEFINITION.
PERFORM GET_FTP_FILE.
PERFORM FORMAT_DATA.
PERFORM WRITE_DATA.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Form GET_FTP_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM GET_FTP_FILE.
*&CMD1
CLEAR:COMMANDS.
REFRESH COMMANDS.
COMMANDS-CMD = 'ascii'.
APPEND COMMANDS.
*&CMD2 切换本地工作目录
CONCATENATE 'lcd' S_PATH INTO W_80C SEPARATED BY ' '.
COMMANDS-CMD = W_80C.
APPEND COMMANDS.
CLEAR W_80C.
*&CMD3 切换FTP服务器工作目录
CONCATENATE 'cd' P_PATH INTO W_80C SEPARATED BY ' ' .
COMMANDS-CMD = W_80C.
APPEND COMMANDS.
CLEAR W_80C.
*获取遥测数据
CONCATENATE 'get' 'zsmis_yc.txt' INTO W_80C SEPARATED BY ' ' .
COMMANDS-CMD = W_80C.
APPEND COMMANDS.
CLEAR W_80C.
*获取遥信数据
CONCATENATE 'get' 'zsmis_yx.txt' INTO W_80C SEPARATED BY ' ' .
COMMANDS-CMD = W_80C.
APPEND COMMANDS.
CLEAR W_80C.
SET EXTENDED CHECK OFF.
SLEN = STRLEN( P_PASS ).
*将密码转换成SAP内部码
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = P_PASS
SOURCELEN = SLEN
KEY = KEY
IMPORTING
DESTINATION = P_PASS.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
USER = P_USER
PASSWORD = P_PASS
HOST = P_HOST
RFC_DESTINATION = 'SAPFTPA'
IMPORTING
HANDLE = HANDLE.
IF HANDLE EQ 0.
MESSAGE I651 WITH '连接FTP服务器失败!'.
STOP.
ENDIF.
LOOP AT COMMANDS.
IF COMMANDS-CMD NE ' '.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
HANDLE = HANDLE
COMMAND = COMMANDS-CMD
TABLES
DATA = RESULT
EXCEPTIONS
COMMAND_ERROR = 1
TCPIP_ERROR = 2.
REFRESH RESULT.
ENDIF.
ENDLOOP.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
HANDLE = HANDLE.
REFRESH TAB.
CLEAR:CNS_DIR.
CNS_DIR = S_PATH.
CONCATENATE 'dir' CNS_DIR INTO COM SEPARATED BY SPACE.
SET BLANK LINES ON.
CALL 'SYSTEM' ID 'COMMAND' FIELD COM
ID 'TAB' FIELD TAB-*sys*.
* READ TABLE TAB INDEX 1.
* IF SY-SUBRC = 0.
* FTPFILE = TAB-LINE.
FTPFILE = 'zsmis_yc.txt'.
CLEAR:ITABOUT,CNS_DIR.
REFRESH:ITABOUT.
CONCATENATE S_PATH FTPFILE INTO CNS_DIR.
UPLOAD_DATA_FROM_SAP CNS_DIR ITABOUT.
CLOSE DATASET CNS_DIR.
DELETE DATASET CNS_DIR.
* ENDIF.
ENDFORM. "GET_FTP_FILE
*&---------------------------------------------------------------------*
*& Form FORMAT_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FORMAT_DATA.
DATA: FM_NAME(1000) TYPE C.
CLEAR:SCADA.REFRESH:SCADA.
* LOOP AT ITABOUT INTO FM_NAME.
* SCADA-BDZ = FM_NAME+0(32).
* SCADA-DY = FM_NAME+33(8).
* SCADA-JG = FM_NAME+42(8).
* SCADA-JGMC = FM_NAME+51(64).
* SCADA-LNAME = FM_NAME+116(8).
* SCADA-VALUE = FM_NAME+125(30).
* APPEND SCADA.
* CLEAR SCADA.
* CLEAR FM_NAME.
* ENDLOOP.
LOOP AT ITABOUT INTO FM_NAME.
CONDENSE FM_NAME.
SPLIT FM_NAME AT SPACE INTO SCADA-BDZ SCADA-DY SCADA-JG SCADA-JGMC SCADA-LNAME SCADA-VALUE.
IF SY-SUBRC = 0.
SHIFT SCADA-DY BY 1 PLACES.
SHIFT SCADA-JG BY 1 PLACES.
SHIFT SCADA-JGMC BY 1 PLACES.
SHIFT SCADA-LNAME BY 1 PLACES.
SHIFT SCADA-VALUE BY 1 PLACES.
APPEND SCADA.
CLEAR SCADA.
ENDIF.
ENDLOOP.
ENDFORM. "GET_FTP_FILE
*&---------------------------------------------------------------------*
*& Form WRITE_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM WRITE_DATA.
WRITE:/(32) '名称',(8) '等级',(8) '编号',(64) '间隔名称',(8) '量名',(30) '量值' .
ULINE.
LOOP AT ITABOUT.
WRITE:/ ITABOUT.
ENDLOOP.
ULINE.
LOOP AT SCADA.
WRITE:/(32) SCADA-BDZ,(8) SCADA-DY,(8) SCADA-JG,(64) SCADA-JGMC,(8) SCADA-LNAME,(30) SCADA-VALUE.
ENDLOOP.
ENDFORM. "WRITE_DATA