Writing to external file

  • Hello everyone,
    I'm trying to count, how much time the welding process was avtive, so I wrote next program,that I'm planning to call in sps:


    bool mem ; bit memory 1
    IF ($IN[2371] AND (NOT mem) ) THEN ; if we caught the front of signal 2371
    $Timer_STOP[5] = FALSE ; started the timer for 5 seconds
    ENDIF


    IF ($TIMER[5] >= 5000) THEN ; if 5 seconds already gone
    $TIMER_STOP[1] = FALSE ;started welding timer
    $TIMER_STOP[5] = TRUE ;stopped timer of 5 seconds
    $TIMER[5] = 0 ;set timer of 5 seconds to 0
    ENDIF


    IF ((NOT $IN[2371]) AND mem) THEN
    $TIMER_STOP[1] = TRUE
    CWRITE($FCT_CALL, STAT, MODE, "krl_fopen", "timers.txt", "a", HANDLE) ;open file
    CWRITE($FCT_CALL, STAT, MODE, "krl_fprintf", HANDLE, "%f, %f, %f, %f, %f, %f, %f, %f, ",$TIMER[1]) ;write to file
    CWRITE($FCT_CALL, STAT, MODE, "krl_fclose", HANDLE) ; close file

    $TIMER[1] = 0
    ENDIF

    mem = $IN[2371] ;remembered the last state of 2371


    So what the program exactly do: in bg logic it scans for active signal 2371, since its going active - its waiting for 5 seconds (5 second timer), then, start the other timer and counts how much time 2371 was active writing that numbers to timers.txt file


    My question - I'm getting the "variable not defined" error in all cwrite lines. Also, Im getting error "The type of sentence is not equal to STAT_T" and "The type of sentence is not equal to MODUS_T". I checked manual many times and my code for writing into file exactly the same, also, i checked another thread: https://www.robot-forum.com/ro…d-hour/msg55503/#msg55503
    And as you see, my code seems to be ok. Whats wrong?

    Edited once, last by Acapulka ().

  • your code is not ok...


    1. you need to declare AND initialize all variables, including STAT, MODE, HANDLE
    2. your second CWRITE is trying to write 8 values (you have eight placeholders %F) but you are only passing one value ($TIMER[1])



    at begin of your program add something like

    Code
    ; this is declaratation section
    DECL STAT_T STAT ; declare STAT variable
    DECL MODUS_T MODE ; declare MODE variable
    DECL INT HANDLE; declare HANDLE variable


    then you need to do initialization section

    Code
    HANDLE = 0 ; set initial value for HANDLE
    ;etc.


    and finally you can do rest of programs like you wrote...

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • btw, only use recent version of CWRITE manual for KRC4, check manuals download section, do not use one in topic linked in your post.

    1) read pinned topic: READ FIRST...

    2) if you have an issue with robot, post question in the correct forum section... do NOT contact me directly

    3) read 1 and 2

  • There is no assignment to a variable called $ACC_VEL inside bas.src. Do you mean $ACC_AXIS? $ACC_AXIS is used to program axis acceleration for motion commands. Since no motion commands are allowed inside a sub-program These assignments are prohibited. Probably you are calling the INI-Fold in your submit. So just delete it.


    Fubini

  • Yep,already fixed that, but now there is ANOTHER problem (oh God!). Somehow there is nothing that is writing into file. I checked C:\KRC\ROBOTER\UserFiles and there is timers.txt file (that is good, it was created by itself,that means the line with opeping worked), but there is nothing into that txt file. Only 1.
    I started to think,that my code is not working and made a test - instead of writing result into txt file i wrote it into INT variable. And it worked! that mean that countings are ok,the problem is in the writing to the file. Any ideas?


    UPD:
    I wrote separate program just to test writing to file,and as it shows when im trying to write a string to a file-all works fine. But as soon as I chose variable- nothing works. I think I'm sending wrong params,thats the problem. My variable is $timer[1], its int.

    Edited once, last by Acapulka ().

  • In CWRITE($FCT_CALL,STAT,MODE,"krl_fputs",HANDLE,"%f,",$TIMER[1]):


    %f is usually the type qualifer for floating point values. $TIMER is of integer type so maybe should try %d and use krl_fprintf(). fputs is for strings.


    Fubini

    Edited once, last by Fubini ().

  • Thanks a lot,it worked! THe only last problem-how to write variables in list? Right now its just adding the nubmers to the previous, and it looks like " 4523412467123876128476"
    but what I need is
    1234
    5678
    9101112


    etc.


    I can just can add another cwrite, with spacebar but hot to write /n?

    Edited once, last by Acapulka ().


  • Hello Acapulka,


    Have you already achieve the LF or CF spacer ? or just use separator to identify them out like 12,34,56....?


    来自我的 MI 5s 上的 Tapatalk

Advertising from our partners