You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FANUC KAREL XML解析器开发求助:无结果输出问题排查

Fixing FANUC KAREL XML Parser for Position Data Extraction

Hey there, let's break down why your current KAREL XML parser isn't producing output and how to adjust it to successfully pull out those position values (like x, y, z, rx, etc.) from your XML message.

Key Issues in Your Current Code

  • Mismatched Struct Definition: Your xmlstrct_t uses generic fields (first, second) that don't map to the actual XML tags (x, y, name, etc.). KAREL's XML library relies on matching struct fields to XML element names to populate data.
  • Incorrect Tag Association: The xml_addtag call references xmlstrct_t but doesn't link it to the specific XML elements you care about.
  • No Data Output: Even if parsing worked, your code doesn't actually display or store the extracted values anywhere.
  • Scanning Logic Gaps: The xml_scan and xml_getdata flow isn't properly capturing the text content of each XML element.

Step-by-Step Fix

1. Update the Struct to Match XML Tags

First, redefine your struct to include fields that mirror the XML elements you need (focus on position data first, but you can add others as needed):

TYPE xml_data_t = STRUCTURE
    name : string[32]
    job : integer
    x : real
    y : real
    z : real
    rx : real
    ry : real
    rz : real
    px : real
    py : real
    pz : real
ENDSTRUCTURE

2. Adjust the Parsing Routine

Rewrite the xml_parse routine to correctly associate XML elements with the struct, scan for the right tags, and extract data. Here's a revised version:

PROGRAM xmlparse
%NOLOCKGROUP
%NOPAUSESHFT
%NOPAUSE = ERROR + COMMAND + TPENABLE
%NOABORT = ERROR + COMMAND
%ENVIRONMENT xml
%include klerxmlf

CONST XML_POS_TAG = 1  -- Unique ID for our position data struct

-- Local Vars
VAR
    xml_file : file
    status : INTEGER
    tag_ident : integer
    func_code : integer
    textdone : boolean
    numattr : integer
    done : boolean
    pos_data : xml_data_t  -- Use our updated struct
    xml_success : boolean

Routine parse_xml:boolean
BEGIN
    WRITE TPDISPLAY('Starting XML Parse...',CR)
    
    -- Open XML file (verify MC:\result.xml exists and is accessible)
    OPEN FILE xml_file('RO','MC:\result.xml')
    status = IO_STATUS(xml_file)
    IF (status <> 0) THEN
        WRITE TPDISPLAY('File Open Error: '||STR(status),CR)
        POST_ERR(status, 'Failed to open XML file', 0, 0)
        RETURN(FALSE)
    ENDIF
    WRITE TPDISPLAY('File Opened Successfully',CR)
    
    -- Associate our struct with XML elements
    xml_addtag(xml_file, 'name', 32, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'job', 10, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'x', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'y', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'z', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'rx', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'ry', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'rz', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'px', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'py', 20, FALSE, XML_POS_TAG, status)
    xml_addtag(xml_file, 'pz', 20, FALSE, XML_POS_TAG, status)
    
    IF (status <> 0) THEN
        WRITE TPDISPLAY('Tag Association Error',CR)
        POST_ERR(status, 'Failed to map XML tags', 0, 0)
        CLOSE FILE xml_file
        RETURN(FALSE)
    ENDIF
    
    done = FALSE
    WHILE (done = FALSE) DO
        -- Scan for the root <message> element and its children
        xml_scan(xml_file, 'message', tag_ident, func_code, status)
        
        CASE status OF
            CASE 0:  -- End of scan
                done = TRUE
            CASE XML_FUNCTION:  -- Found a tag event
                IF tag_ident = XML_POS_TAG THEN
                    CASE func_code OF
                        CASE XML_START:
                            -- Link the struct to the XML element
                            xml_setvar(xml_file, 'pos_data', 'xml_data_t', status)
                        CASE XML_END:
                            -- Extract the data into our struct
                            xml_getdata(xml_file, numattr, , , , textdone, status)
                            -- Now display the extracted position data
                            WRITE TPDISPLAY('Extracted Data:',CR)
                            WRITE TPDISPLAY('X: '||STR(pos_data.x,8,6),CR)
                            WRITE TPDISPLAY('Y: '||STR(pos_data.y,8,6),CR)
                            WRITE TPDISPLAY('Z: '||STR(pos_data.z,8,6),CR)
                            WRITE TPDISPLAY('RX: '||STR(pos_data.rx,8,6),CR)
                            WRITE TPDISPLAY('RY: '||STR(pos_data.ry,8,6),CR)
                            WRITE TPDISPLAY('RZ: '||STR(pos_data.rz,8,6),CR)
                    ENDCASE
                ENDIF
            CASE XML_SCANLIM:
                -- Hit scan limit, continue
            ELSE:
                -- Unexpected error
                WRITE TPDISPLAY('Scan Error: '||STR(status),CR)
                POST_ERR(status, 'XML Scan Failed', 0, 0)
                done = TRUE
        ENDCASE
    ENDWHILE
    
    -- Cleanup
    xml_remtag(xml_file, 'xml_data_t', status)
    CLOSE FILE xml_file
    status = IO_STATUS(xml_file)
    
    IF (status = 0) THEN
        xml_success = TRUE
        WRITE TPDISPLAY('Parse Complete!',CR)
    ELSE
        xml_success = FALSE
        WRITE TPDISPLAY('File Close Error',CR)
    ENDIF
    
    RETURN(xml_success)
END parse_xml

BEGIN
    xml_success = parse_xml()
END xmlparse

Key Changes Explained

  • Struct Alignment: The xml_data_t struct now directly matches the XML elements you need to extract, ensuring KAREL can map the XML text to the correct variable types.
  • Tag Association: We call xml_addtag for each XML element we care about, linking them to our struct ID (XML_POS_TAG).
  • Data Output: After extracting data with xml_getdata, we write the position values to the TP display so you can see the results.
  • Error Handling: Added more detailed error messages to help diagnose issues like file access problems or tag mapping failures.

Additional Tips

  • Verify that MC:\result.xml exists on the controller's memory card and that your KAREL program has read access to it.
  • If your XML has nested elements, adjust the scanning logic to handle nested tags appropriately.
  • Test with a simplified XML file first to confirm parsing works, then add more elements as needed.

内容的提问来源于stack exchange,提问作者KaiF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:13:09