Socket Messaging Test

  • Hello to the whole community. First thank you for all the help I received in my career as a programmer in this forum.


    Unfortunately, I've been trying to solve a problem for a couple of days and I can not find a solution.


    I want to communicate my robot with a server using SM.


    Right now I'm testing with Roboguide and a server on the same PC. I think the first step that I have to achieve in the project is "simply" to establish connection.


    The robot is a client and the "application" server.


    I already have the KAREL program, and a simple server. But I can not communicate.


    I would appreciate a little help.


    Thanks in advance.





    KAREL PROGRAM.

    -------------------------------------------------------------------------------
    PROGRAM HELLOWORLDSERVER


    %STACKSIZE = 4000
    %NOLOCKGROUP
    %NOPAUSE=ERROR+COMMAND+TPENABLE
    %ENVIRONMENT uif
    %ENVIRONMENT sysdef
    %ENVIRONMENT memo
    %ENVIRONMENT kclop
    %ENVIRONMENT bynam
    %ENVIRONMENT fdev
    %ENVIRONMENT flbt
    %INCLUDE klevccdf
    %INCLUDE klevkeys
    %INCLUDE klevkmsk





    --%NOLOCKGROUP
    --%NOPAUSE=ERROR+COMMAND+TPENABLE
    --%NOBUSYLAMP
    --%COMMENT = 'version 13'


    -------------------------------------------------------------------------------
    VAR
    aux : INTEGER


    STATUS_C1:INTEGER --communication status
    force_abort:BOOLEAN --Force ending program
    new_command:INTEGER -- command to send
    ComFile_C:FILE -- FOR CLIENT COMMUNICATION
    BufBytes,StatBuf: INTEGER -- socket buffer
    CurrPos:XYZWPR



    -------------------------------------------------------------------------------


    -----------------------------------------------------------------------------------


    -----------------------------------------------------------------------------


    CONST
    MULT_FACT_P=1010 --Multiplication factor for position data (mm)
    MULT_FACT_O=10000 --Multiplication factor for orientation data (degrees)
    PORT_NUMBER=5001 --Socket port number
    ----------------------------------------------------------
    --------------------------------------------------------
    ROUTINE start_socket
    VAR
    entry:INTEGER
    BEGIN


    REPEAT
    -- WRITE TPERROR('ping=',STATUS_C1,CR)
    MSG_DISCO('C3:',STATUS_C1)--CLOSING PORT BEFORE START
    CLOSE FILE ComFile_C

    SET_FILE_ATR(ComFile_C,ATR_IA)-- for binary file
    SET_VAR(entry,'*SYSTEM*','$HOSTC_CFG[3].$SERVER_PORT',PORT_NUMBER,STATUS_C1)

    --SET_FILE_ATR(ComFile_C,ATR_REVERSE)-- for byte swap
    WRITE TPERROR(CHR(128),'C3_CONNECTING.....',CR)
    DELAY 1000
    MSG_CONNECT('C3:',STATUS_C1)
    OPEN FILE ComFile_C ('RW','C3:')
    STATUS_C1 = IO_STATUS(ComFile_C)
    DELAY 1000
    IF STATUS_C1 = 0 THEN
    WRITE TPERROR(CHR(128),'C3_CONNECTED',CR)
    DELAY 1000
    else
    WRITE TPERROR(CHR(128),'C3_DISCONNECTED',CR)
    ENDIF
    DELAY 1000
    UNTIL (status_c1=0)
    END start_socket
    --------------------------------------------


    BEGIN

    -- MSG_PING('C3',AUX)



    start_socket


    END HELLOWORLDSERVER





    PYHTON SERVER:

    #
    #
    #
    import socket


    ip = "127.0.0.1"
    puerto = 6008


    if __name__ == '__main__':


    print ("Servidor Basico : \n")
    ip = input ( "Cual es la direccion IP de este servidor ? :")
    puerto = int(input ("Cual es el puerto que escucha : "))


    print ('\n')
    print ( "espere creando servidor ...")
    print ('\n')
    dataConection =(ip,puerto)
    conexionMax = 5
    servidorSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    servidorSocket.bind(dataConection)
    servidorSocket.listen(conexionMax)


    print ( "Servidor creado escuchando en " ,ip ,"puerto ",puerto)


    cliente,direccion = servidorSocket.accept()


    print ("un cliente!!!!")
    print ("conexion establecida con %s puerto: %s"%(direccion[0],direccion[1]))
    print ("y escribio :",cliente.recv(1024))
    print ('\n')
    input("una tecla para cerrar programa ")
    servidorSocket.close()

  • [SOLVED] .. easily put the same IP of your PC card (in my case i use the WiFi card). It works.


    HINT... do not flood the delay, istead use a simple BYTES_AHEAD and other.

    HINT_2 use ATR_IA and ATR_UF.


Advertising from our partners