1. Home
    1. Dashboard
    2. Search
  2. Forum
    1. Unresolved Threads
    2. Members
      1. Recent Activities
      2. Users Online
      3. Team Members
      4. Search Members
      5. Trophys
  3. Articles
  4. Blog
  5. Videos
  6. Jobs
  7. Shop
    1. Orders
  • Login or register
  • Search
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Blog Articles
  • Products
  • More Options
  1. Robotforum - Support and discussion community for industrial robots and cobots
  2. Forum
  3. Industrial Robot Support and Discussion Center
  4. KUKA Robot Forum
Your browser does not support videos RoboDK Software for simulation and programming
Visit our Mainsponsor
IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Sponsored Ads

How to open and write the same file multiple times (CWRITE)?

  • GaborGraczer
  • February 20, 2024 at 1:58 PM
  • Thread is Unresolved
  • GaborGraczer
    Trophies
    1
    Posts
    6
    • February 20, 2024 at 1:58 PM
    • #1

    Hello

    I am trying to write multiple lines into one file but in different time (like a log file), but I did not success.

    I tried this on KSS 8.6.x

    I am able to write multiple lines at one time like this, but this is not what I need:

    Code
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 1")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 2")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 3")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)

    I want this to work:

    Code
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 1")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
    
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 2")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
    
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 3")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
    Display More

    I tried all the "Mode for opening" variables (r, a, w ... ) without success.

    The krl_fopen works fine at the first time, creates the file, write into it and close it, but at the 2nd time I am not able to open it, I got error MSG_NO = -2 => "Operation failed"

    How can I open and write the same a file several time?

    This is the source code:

    Code
    &ACCESS RVO
    &PARAM EDITMASK = *
    DEF FileTest()
       CHAR sLogFileName[100]
       CHAR sPath[100]
       
       sLogFileName[] = "log.txt"
       
       WriteFile(sLogFileName[], "alma")
       WriteFile(sLogFileName[], "korte")
    
       HALT
       
    END
    
    GLOBAL DEF WriteFile(sPath[]:IN,sData[]:IN)
       CHAR sPath[]
       CHAR sData[]
       
       DECL STATE_T stateT
       DECL MODUS_T modusT
       INT iHandle
       
       modusT=#sync
       iHandle=0
       
       CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
       CheckSTAT("krl_fopen", stateT)
       
       CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, sData[])
       CheckSTAT("krl_fprintf", stateT)
       
       CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
       CheckSTAT("krl_fclose", stateT)
    END
    
    DEF CheckSTAT(sInKRL:IN, stateTIN:IN, sPath[]:IN)
       CHAR sInKRL[]
       DECL STATE_T stateTIN
       CHAR sPath[]
       
       DECL STATE_T stateTSW
       CHAR sInfo[30]
       INT iOffset
       
       iOffset = 0
       
       IF stateTIN.RET1 == #CMD_ABORT THEN
          
          ON_ERROR_PROCEED
          sPath[] = sPath[]
          IF $ERR.Number==0 THEN
             SWRITE(sInfo[], stateTSW, iOffset, "%s: %s MSG_NO: %i", sInKRL[], sPath[], stateTIN.MSG_NO)
          ELSE         
             SWRITE(sInfo[], stateTSW, iOffset, "%s MSG_NO: %i", sInKRL[], stateTIN.MSG_NO)
          ENDIF
          
          SWITCH stateTIN.MSG_NO
             CASE -1
                MsgQuit("Internal error!", sInfo[])
             CASE -2
                MsgQuit("Operation failed!", sInfo[])
             CASE -3
                MsgQuit("There is no file open!", sInfo[],)
             CASE -4
                MsgQuit("End of file has been reached!", sInfo[],)
             CASE -5
                MsgQuit("Maximum size of directory C:\KRC\ROBOTER\UserFiles has been reached!", sInfo[])
             CASE -6
                MsgQuit("File already open!", sInfo[])
             CASE -7
                MsgQuit("Positioning of the file pointer outside the limits of the file!", sInfo[])
             CASE -8
                MsgQuit("File system not available: error in specification of target directory!", sInfo[])
             CASE -10
                MsgQuit("Invalid number of function parameters transferred!", sInfo[])
             CASE -11
                MsgQuit("At least one function parameter has an invalid value!", sInfo[])
             CASE -12
                MsgQuit("At least one function parameter has an incorrect data type!", sInfo[])
             CASE -13
                MsgQuit("At least one function parameter is not a variable although a variable is expected!", sInfo[])
             CASE -14
                MsgQuit("At least one function parameter string is blank!", sInfo[])
             CASE -15
                MsgQuit("At least one function parameter string is too long!", sInfo[])
             CASE -16
                MsgQuit("At least one function parameter string contains invalid characters!", sInfo[])
             CASE -30
                MsgQuit("Direct access to the ROBOTER directory is not permissible!", sInfo[])
             CASE -31
                MsgQuit("Invalid absolute path!", sInfo[])
             CASE -32
                MsgQuit("Mount point not found!", sInfo[])
             CASE -40
                MsgQuit("File cannot be opened because 11 files are already open!", sInfo[])
             CASE -42
                MsgQuit("The format string contains an invalid format statement!", sInfo[])
             CASE -43
                MsgQuit("The nbr of values to be read in the format str does not correspond to the nbr of transferred parameters!", sInfo[])
             CASE -44
                MsgQuit("An invalid format was specified for one parameter!", sInfo[])
             DEFAULT
                MsgQuit("Unknown MSG_NO: %1!", sInKRL[], stateTIN.MSG_NO)
          ENDSWITCH
       ENDIF
       
    END
    Display More
  • Lemster68 February 20, 2024 at 2:14 PM

    Approved the thread.
  • DannyDJ
    Reactions Received
    62
    Trophies
    6
    Posts
    493
    • February 20, 2024 at 3:30 PM
    • #2

    Hello, did you checked this part in the code(line 30 & 31), in CWRITE, you have "krl_fwriteln" and checkSTAT you have "krl_fprintf".

    Maybe if you put in CWrite also "krl_fprintf" it would be OK.

    Code
      CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, sData[])   
      CheckSTAT("krl_fprintf", stateT)
  • Online
    SkyeFire
    Reactions Received
    1,038
    Trophies
    12
    Posts
    9,373
    • February 20, 2024 at 3:34 PM
    • #3

    So, you want to append to an existing file, correct? Is this a text or binary file? This would be mode "a" for a text file, "ab" for binary.

    krl_fopen returning a -2 indicates that the file is already outside of KRL, or does not exist:

    Do you have this file open in some other program, like Notepad, when this error occurs? Is this file local on the robot, or on a remote network mount?

    I've attached a utility module that I've used on many robots over the years, and the File Open and File Append routines worked reliably. You could give them a shot.

    Files

    krlfunctions.zip 21.69 kB – 15 Downloads
  • GaborGraczer
    Trophies
    1
    Posts
    6
    • February 20, 2024 at 3:52 PM
    • #4
    Quote from DannyDJ

    Hello, did you checked this part in the code(line 30 & 31), in CWRITE, you have "krl_fwriteln" and checkSTAT you have "krl_fprintf".

    Maybe if you put in CWrite also "krl_fprintf" it would be OK.

    Code
      CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, sData[])   
      CheckSTAT("krl_fprintf", stateT)

    I checked the CheckSTAT("krl_fprintf", stateT) line, krl_fprintf is just an info for the Error handling part.

    I also tested printf, without success.

  • GaborGraczer
    Trophies
    1
    Posts
    6
    • February 20, 2024 at 4:00 PM
    • #5
    Quote from SkyeFire

    So, you want to append to an existing file, correct? Is this a text or binary file? This would be mode "a" for a text file, "ab" for binary.

    krl_fopen returning a -2 indicates that the file is already outside of KRL, or does not exist:

    Do you have this file open in some other program, like Notepad, when this error occurs? Is this file local on the robot, or on a remote network mount?

    I've attached a utility module that I've used on many robots over the years, and the File Open and File Append routines worked reliably. You could give them a shot.

    Yes, append an existing .txt file, I use "a" parameter.

    I got error -2, but as far as I see, the file is not used by anything, because I am able to delete the file in Windows Explorer.

    It is a local file, not a network one, actually I use KUKA.OfficeLite, that must be the same behavior like a real controller.

    I checked your program, the fundamental is the same like in my program.

    Robot is not able to open the file, only once when it is created.

    The below line simple doesn't work if the file already exist, although it has 1 line content and Windows is able to delete it ....

    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)

  • DannyDJ
    Reactions Received
    62
    Trophies
    6
    Posts
    493
    • February 20, 2024 at 4:18 PM
    • #6

    i have tested this code in my OL 8.6.7 and it works, i only replaced the sPath with "logg.txt"

    Code
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 1")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
    
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 2")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
    
    CWRITE($FCT_CALL,stateT,modusT,"krl_fopen",sPath[],"a",iHandle)
    CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "Data 3")
    CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
    Display More

    Maybe the second time while trying to write in the same file your sPAth[] variable gets corrupted in here

    Code
    IF stateTIN.RET1 == #CMD_ABORT THEN            
    ON_ERROR_PROCEED      
    sPath[] = sPath[]

    Images

    • file_test.jpeg
      • 150.63 kB
      • 969 × 1,109
      • 13
  • DannyDJ
    Reactions Received
    62
    Trophies
    6
    Posts
    493
    • February 20, 2024 at 6:49 PM
    • #7

    I have tested also your posted code and it works in my OfficeLight 8.6.7, only I have left out the global declaration for the WriteFile.

    Code
    GLOBAL DEF WriteFile(sPath[]:IN,sData[]:IN)

    Images

    • file_test_2.jpeg
      • 104.19 kB
      • 893 × 859
      • 9
  • GaborGraczer
    Trophies
    1
    Posts
    6
    • February 21, 2024 at 10:30 AM
    • #8

    This simple code does not work either on my KUKA.OfficeLite 8.6.

    Works only once at first run, but not able to open the file anymore, I always got error -2.

    Something is wrong with my OfficeLite? But what?

    Code
    &ACCESS RVP
    &PARAM EDITMASK = *
    DEF FileTest2()
       DECL STATE_T stateT
       DECL MODUS_T modusT
       INT iHandle
       
       modusT=#sync
       iHandle=0
       
       CWRITE($FCT_CALL,stateT,modusT,"krl_fopen","log.txt","a",iHandle)
       IF stateT.RET1 == #CMD_ABORT THEN
          MsgQuit("Error: %1!",, stateT.MSG_NO)
       ENDIF   
       
       CWRITE($FCT_CALL,stateT,modusT,"krl_fwriteln",iHandle, "alma")
       IF stateT.RET1 == #CMD_ABORT THEN
          MsgQuit("Error: %1!",, stateT.MSG_NO)
       ENDIF   
       
       CWRITE($FCT_CALL,stateT,modusT,"krl_fclose",iHandle)
       IF stateT.RET1 == #CMD_ABORT THEN
          MsgQuit("Error: %1!",, stateT.MSG_NO)
       ENDIF   
       
    END
    Display More
  • DannyDJ
    Reactions Received
    62
    Trophies
    6
    Posts
    493
    • February 21, 2024 at 12:04 PM
    • #9
    Quote from GaborGraczer

    This simple code does not work either on my KUKA.OfficeLite 8.6.

    Works only once at first run, but not able to open the file anymore, I always got error -2.

    Something is wrong with my OfficeLite? But what?

    Do you have robot and try on real robot controller? Your code worked fine on my OfficeLight KSS 8.6.7 but this is inside VMWARE Player, maybe you have some setting there. Or maybe talk to KUKA directly(they will know if they have some bug in your version, describe the problem) and maybe they wil send you newer version of OL. In the past I have had also some problems with some versions of OL throwing weird errors that it shouldn't be throwing, they send me newer version, and same program worked OK in newer version worked OK, or try and reinstall your version, but the I guess you will have to redo licence.

  • GaborGraczer
    Trophies
    1
    Posts
    6
    • February 21, 2024 at 12:43 PM
    • #10
    Quote from DannyDJ

    Do you have robot and try on real robot controller? Your code worked fine on my OfficeLight KSS 8.6.7 but this is inside VMWARE Player, maybe you have some setting there. Or maybe talk to KUKA directly(they will know if they have some bug in your version, describe the problem) and maybe they wil send you newer version of OL. In the past I have had also some problems with some versions of OL throwing weird errors that it shouldn't be throwing, they send me newer version, and same program worked OK in newer version worked OK, or try and reinstall your version, but the I guess you will have to redo licence.

    I made several restarts ... did not help. I will test it on a real robot, but today I am at Home Office, that's why the OfficeLite would be good ...

    I will reinstall the OfficeLite 8.6 to see if it helps. I also have OfficeLite 8.7, unfortunately there I have license issues.

    I will come back with the outcome once ...

    Thanks for the comments and replays, I far as I see based on your comments my code is working but not on my OfficeLite system.

Advertising from our partners

IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Advertise in Robotics
Advertise in Robotics

Job Postings

  • Anyware Robotics is hiring!

    yzhou377 February 23, 2025 at 4:54 AM
  • How to see your Job Posting (search or recruit) here in Robot-Forum.com

    Werner Hampel November 18, 2021 at 3:44 PM
Your browser does not support videos RoboDK Software for simulation and programming

Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Thread Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000
  1. Privacy Policy
  2. Legal Notice
Powered by WoltLab Suite™
As a registered Member:
* You will see no Google advertising
* You can translate posts into your local language
* You can ask questions or help the community with your knowledge
* You can thank the authors for their help
* You can receive notifications of replies or new topics on request
* We do not sell your data - we promise

JOIN OUR GREAT ROBOTICS COMMUNITY.
Don’t have an account yet? Register yourself now and be a part of our community!
Register Yourself Lost Password
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on Google Play
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on the App Store
Download