Communication going between cognex and fanuc robot

  • I am new at socket messaging and got this code for communication between cognex and fanuc. I made few modification.
    I have few doubts.


    Question
    1) What will the code line mp_str='' will do ?
    2) the status of the connection is coming a hug number not a 1 or a 0 ? How should I approach?
    3) What does READ file_var (xs::0::2, ys::0::2, angles::0::2) represent?
    4) READ file_var(mp_str) ?? the file_var will access which file or variable from the cognex? Where am I able to get all the variables read in the fanuc tp pendant?



    PROGRAM SOCKET_MESSAGING
    %COMMENT = 'KL_MAIN V9.1'
    %STACKSIZE = 8096
    %NOLOCKGROUP
    %NOPAUSESHFT
    %NOPAUSE = ERROR + COMMAND + TPENABLE
    %NOBUSYLAMP
    %NOABORT = ERROR + COMMAND
    %INCLUDE KLEVCCDF -- predefined key character code constants
    %INCLUDE KLIOTYPS -- karel IO type definitions
    %INCLUDE KLEVKEYS -- predefined karel key code constants
    %INCLUDE KLEVKMSK -- predefined mask codes for built in functions


    VAR
    file_var : FILE -- for client communication
    tmp_int : INTEGER
    mp_str : STRING[128]
    status : INTEGER -- communication status
    entry : INTEGER
    try : INTEGER
    statuss :STRING[1]
    xs :STRING[9]
    ys :STRING[9]
    angles :STRING[9]
    x :REAL
    y :REAL
    angle :REAL



    CONST
    port_number= 23 --- Socket port number


    BEGIN

    SET_FILE_ATR(file_var, ATR_IA)
    SET_VAR(entry,'*SYSTEM*','$HOSTC_CFG[3].$SERVER_PORT',port_number,status)


    -- Connect the tag
    WRITE TPDISPLAY('Connecting…',CR)
    MSG_DISCO('C3:', status) ----- closing port before start
    -- Need to be called before any tag can be used for socket messgaing (1st parameter is the tag name ,2nd parameter integer that will contain status of the operation)
    MSG_CONNECT ('C3:',status)
    WRITE('Connect Status =',status,CR)
    OPEN FILE file_var('rw','C3:')

    -- Read the In-Sight Welcome message
    WRITE TPDISPLAY('Logging In',CR)
    mp_str =''
    READ file_var(mp_str)
    WRITE TPDISPLAY(mp_str,CR)


    -- Read User: prompt
    READ file_var(mp_str::6::0)
    IF UNINIT(mp_str) THEN
    mp_str = ''
    ENDIF
    WRITE TPDISPLAY(mp_str,CR)
    IF (mp_str <>'User: ') THEN
    WRITE TPDISPLAY('User Failed',CR)
    --Error
    RETURN
    ENDIF


    --Send User Name
    WRITE file_var('admin',CHR(13),CHR(10))


    -- Read Password prompt
    mp_str =''
    READ file_var(mp_str::10::0)
    IF UNINIT(mp_str) THEN
    mp_str = ''
    ENDIF
    WRITE TPDISPLAY(mp_str,CR)
    IF (mp_str <>'Password: ') THEN
    WRITE TPDISPLAY('Password Failed',CR)
    --Error
    RETURN
    ENDIF


    --Send Password
    WRITE file_var('',CHR(13),CHR(10))


    -- Read Login Response
    mp_str =''
    READ file_var(mp_str)
    IF UNINIT(mp_str) THEN
    mp_str = ''
    ENDIF
    WRITE TPDISPLAY(mp_str,CR)
    IF (mp_str <> 'User Logged In') THEN
    WRITE TPDISPLAY('Log In Failed',CR)
    --Error
    RETURN
    ENDIF


    -- Instruction In-Sight to Acquire an Image
    -- (wait for a response)
    WRITE TPDISPLAY('Trigger',CR)
    WRITE file_var ('sw8',CHR(13),CHR(10))

    -- Read Status
    READ file_var (statuss)
    IF statuss <> '1' THEN
    WRITE TPDISPLAY('sw8 Failed',CR)
    RETURN
    ENDIF

    -- Get the value in cell C7
    WRITE TPDISPLAY('Get Value C7',CR)
    WRITE file_var ('gvc007',CHR(13),CHR(10))

    -- Read Status
    READ file_var (statuss)
    IF statuss <> '1' THEN
    WRITE TPDISPLAY('gvc007 Failed',CR)
    RETURN
    ENDIF

    -- Read the data
    -- This will split the information at the quotations "'".
    -- Example String '10.5''9.8''15.9'
    READ file_var (xs::0::2, ys::0::2, angles::0::2)


    --Disconnect Socket
    MSG_DISCO('C3:', status)


    -- Convert the data
    CNV_STR_REAL(xs, x)
    CNV_STR_REAL(ys, y)
    CNV_STR_REAL(angles, angle)
    WRITE TPDISPLAY('X:',x,CR,'Y:',y,CR,'R',Angle,CR)


    END SOCKET_MESSAGING

  • Hello,
    Although I am not an expert, I will take a stab at answering these questions...


    1) What will the code line mp_str='' will do ?
    A) This instruction will clear the string before reading it, so that data from the next line will be the only data in this variable, and there will be no residual data.


    2) The status of the connection is coming a hug number not a 1 or a 0 ? How should I approach?
    A) I have created an Excel file that will convert the status value to an error code. Then the error code can be researched in the Fanuc eDocs. I believe this may help point you in the right direction.
    https://www.robot-forum.com/ro…c-karel-status-converter/



    3) What does READ file_var (xs::0::2, ys::0::2, angles::0::2) represent?
    A) This will read the string values from the cognex system. xs, ys, and angles are all strings of 9 characters long. The numbers ::0 and ::2 are format specifiers. Look in the KAREL eDocs under 7.8 FORMATTING TEXT (ASCII) INPUT/OUTPUT for more information about these.


    4) READ file_var(mp_str) ?? the file_var will access which file or variable from the cognex? Where am I able to get all the variables read in the fanuc tp pendant?
    A) similar to #3


    I hope this helps,
    MiRobotGuy

    +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+

    MI Robot Guy

  • Regarding question 3:
    Take a look at the example string provided: '10.5''9.8''15.9'.

    Code
    READ file_var (xs::0::2, ys::0::2, angles::0::2)

    would cut this example string into substrings to xs, ys, angles with " being the separator. Unfortunately, I can recall, that you can't use " as separator in Cognex - correct my if I'm wrong. So what Cognex might actually send to you will be more like '10.5,9.8,15.9', and you will have to separate the numbers yourself.


    Regarding question 4:
    "The file_var will access which file or variable from the cognex?"
    The ones which you request with Get Value command. In this example code, there's:

    Code
    WRITE file_var ('gvc007',CHR(13),CHR(10))


    So from Fanuc you're sending a command to Cognex, saying: "Get me value of C7". And as an anwer, cognex will send you this value.
    In Cognex In-Sight Explorer you configure the output for the robot system to be available under "Job.Robot.FormatString", so the command to get your inspection results from Cognex would be:

    Code
    WRITE file_var ('gvJob.Robot.FormatString',CHR(13),CHR(10))
  • thank you all for the help. I data format to be transferred from cognex 3d scanner can be in the Ascii type, 16 bit- signed integer and soo on. Please see the attachment. I have created variable in my karel program to accept this data. Ca you please let me know if the variables I declared are wrong and wont accept the data ?
    is my line of code to read the file from cognex application correct?


    PROGRAM Cognex
    %COMMENT = 'KL_SocketMessaging V9.1'
    %STACKSIZE = 8096
    %NOLOCKGROUP
    %NOPAUSESHFT
    %NOPAUSE = ERROR + COMMAND + TPENABLE
    %NOBUSYLAMP
    %NOABORT = ERROR + COMMAND
    %INCLUDE KLEVCCDF -- predefined key character code constants
    %INCLUDE KLIOTYPS -- karel IO type definitions
    %INCLUDE KLEVKEYS -- predefined karel key code constants





    VAR ---- Global Variable declaration
    file_var : FILE -- for client communication
    enc_count: INTEGER
    tmp_int : INTEGER
    tmp_int1 : INTEGER
    tmp_str : STRING[128]
    status : INTEGER -- communication status
    entry : INTEGER
    try : INTEGER
    xs : STRING[128]
    ys : STRING[128]
    angles : STRING[128]
    x : REAL
    y : REAL
    angle : REAL
    comment : STRING[16]
    loop1 :BOOLEAN
    my_pos :XYZWPR




    BEGIN

    MSG_PING('S3:',status)
    WRITE TPDISPLAY('pinging',status)

    SET_FILE_ATR(file_var, ATR_IA)
    SET_VAR(entry,'*SYSTEM*','$HOSTC_CFG[3].$SERVER_PORT',5001,status)


    -- Connect the tag
    WRITE TPDISPLAY('Connecting…',CR)
    MSG_DISCO('S3:', status) ----- closing port before start


    MSG_CONNECT ('S3:',status)
    WRITE('Connect Status =',status,CR)
    loop1= TRUE

    IF status = 0 THEN
    WHILE loop1 = TRUE DO
    WRITE TPDISPLAY('OPENING FILE..',CR)
    OPEN FILE file_var('RW','S3:')
    status=IO_STATUS(file_var)
    IF status = 0 THEN
    FOR tmp_int =1 TO 10 DO
    READ file_var(tmp_str ::10)
    READ file_var(tmp_int ::10)
    IF status <> 0 THEN
    WRITE('Loop Test Fails',CR)
    loop1 = FALSE
    tmp_int = 10
    ENDIF
    ENDFOR
    ENDIF
    ENDWHILE
    ENDIF

    READ file_var(xs::10)
    READ file_var(ys::10)
    READ file_var(ys::10)
    WRITE TPDISPLAY ('disconnecting', CR)
    MSG_DISCO('S3:',status) --Disconnect Socket

    -- Convert the data
    CNV_STR_REAL(xs, x)
    CNV_STR_REAL(ys, y)
    CNV_STR_REAL(angles, angle)
    WRITE TPDISPLAY('X:',x,CR,'Y:',y,CR,'R',Angle,CR)
    END Cognex


    Please let me know how can I proceed to get the right data in the correct format.
    How can I read daa from the cognex file into group inputs?

Advertising from our partners