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