RobotStudio event

Using socketdev as INOUT parameter.

Options
DenisFR
DenisFR ✭✭✭
edited November 2017 in RAPID Programming
Hello,

I have to discuss between one robot and two computers using socket.
To get a generic module, I create a StartConnect function. Who call SocketCreate and SocketConnect and manage error.
Then I use other function to send and receive data.
So I imagine using a record for each computer with address, port socketdev and other things.
I put the record as function parameter like this:
  RECORD Cir_Param<br>    string sAdrServer;<br>    num nPortServer;<br>    socketdev sdSocketServer;<br>    num nMaxTimeSocket;<br>    num nMAXTRY;<br>    string sReceivedString;<br>  ENDRECORD<br>  LOCAL FUNC bool StartClient(INOUT Cir_Param pcirParam)<br>    SocketCreate pcirParam.sdSocketServer;<br>    SocketConnect pcirParam.sdSocketServer,pcirParam.sAdrServer,pcirParam.nPortServer\Time:=pcirParam.nMaxTimeSocket;<br>  ENDFUNC<br>
But when I check program, I've got :
Type error(83): Cir_Param not value type.
This is due to socketdev in RECORD. When I remove it and comment lines using it. No error is reported.
So, how can I passe socketdev as parameter?
As each computer must have own socketdev.
And I don't want to make a function for each.

Best regards

Comments

  • lemster68
    Options
    First, why a function with the return type bool?  I don't see that in there to return a true or false.  Why not a proc?  Second, why inout parameter?  That is why you cannot have a non-value parameter.  leave that off and you can use non-value parameter.
    Lee Justice
  • lemster68
    Options
    Sorry, wrong about the non-value parameter.  Trying to make a workaround.
    Lee Justice
  • lemster68
    Options
    Maybe this will work.  Good luck.

    MODULE moduletest
      ALIAS socketdev sdSocketServer;
        
      RECORD Cir_Param
        string sAdrServer;
        num nPortServer;
        num nMaxTimeSocket;
        num nMAXTRY;
        string sReceivedString;
      ENDRECORD
      
      CONST Cir_Param pcirParam := [" ",192,5,2," "];
      
      PROC StartClient(Cir_Param tempParam,
          string stSocketServer)

        SocketCreate ArgName(stSocketServer);
        SocketConnect ArgName(stSocketServer),tempParam.sAdrServer,tempParam.nPortServer\Time:=tempParam.nMaxTimeSocket;
      ENDPROC

      PROC first()
        StartClient pcirParam, "firstcomputer";
      ENDPROC

      PROC second()
        StartClient pcirParam, "secondcomputer";
      ENDPROC
    ENDMODULE

    Lee Justice
  • DenisFR
    Options
    Hello,
    This is an example without all code...
    Thanks, with your example I can get this code running.
    <code>Test OK"];

    PROC main()
    IF SendReceive("Test",cirParamC1) THEN
    TPWrite cirParamC1.sReceivedString;
    ENDIF
    IF SendReceive("Test",cirParamC2) THEN
    TPWrite cirParamC2.sReceivedString;
    ENDIF
    ENDPROC

    FUNC bool StartClient(INOUT Cir_Param pcirParam)
    !
    VAR socketstatus vssStatus;
    VAR socketdev sdSocket;
    !
    GetDataVal pcirParam.sdSocketServerName,sdSocket;
    vssStatus:=SocketGetStatus(sdSocket);
    IF vssStatus<>SOCKET_CLOSED THEN
    SocketClose sdSocket;
    ENDIF
    !
    SocketCreate sdSocket;
    SocketConnect sdSocket,pcirParam.sAdrServer,pcirParam.nPortServer\Time:=pcirParam.nMaxTimeSocket;
    SetDataVal pcirParam.sdSocketServerName,sdSocket;
    !
    RETURN TRUE;
    ERROR
    Stop;
    RETURN FALSE;
    ENDFUNC

    FUNC bool SendReceive(string psToSend,INOUT Cir_Param pcirParam)
    !
    VAR socketdev sdSocket;
    VAR string vsReceived;! Can't use pcirParam.sReceivedString directly in SocketReceive (40156 Argument error)
    !
    IF NOT StartClient(pcirParam) RETURN FALSE;
    GetDataVal pcirParam.sdSocketServerName,sdSocket;
    SocketSend sdSocket\Str:=psToSend;
    SocketReceive sdSocket\Str:=vsReceived\Time:=pcirParam.nMaxTimeSocket;
    pcirParam.sReceivedString:=vsReceived;
    SocketClose sdSocket;
    SetDataVal pcirParam.sdSocketServerName,sdSocket;
    !
    RETURN TRUE;
    ERROR
    Stop;
    SocketClose sdSocket;
    RETURN FALSE;
    ENDFUNC
    ENDMODULE
    MODULE MainModule<br> RECORD Cir_Param<br> string sAdrServer;<br> num nPortServer;<br> string sdSocketServerName;!!socketdev non value data.<br> num nMaxTimeSocket;<br> string sReceivedString;<br> ENDRECORD<br><br> VAR socketdev sdC1;<br> VAR socketdev sdC2;<br> PERS Cir_Param cirParamC1:=["192.168.1.12",20001,"sdC1",60,2,"Test OK"];<br> PERS Cir_Param cirParamC2:=["192.168.1.13",20001,"sdC2",60,2,"
  • lemster68
    Options
    Great!
    Lee Justice