io.writeport TCP



  • Hello all!

    I'm trying to implement a very simple ASCII tcp protocoll to control some ali-express ethernet relais.
    The protocoll is very very easy, no security.

    ASCII string: 11 pulls relay 1
    ASCII string: 21 releases relay 1

    So, this works when using packetsender from my phone, works when using packetsender from my laptop. But it doesn't work using io.writeport from a ST file.

    Using wireshark i found out that communication is working, but the packets are not valid (len=0). So i'm doing something wrong.

    The code sending the string is as follows:

    FUNCTION_BLOCK EthernetRelais
    (*
    EXTENDS //base type
    IMPLEMENTS //interface type list
    *)
    	VAR
    		connection 	: string;
    		conn_handle	: io.commhandle;
    		fsm			: int := 0;
    		command		: array[0..10] of byte;
    		local_state : byte := 0;
    		initial		: bool := 1;
    		portstatus : int;
    		timeout : int;
    		i : int;
    		(* add local variables here *)
    	END_VAR
    	
    	VAR_INPUT
    		(* add in variables here *)
    		state : bool;
    	END_VAR
    	
    	VAR_OUTPUT
    		(* add out variables here *)
    		current_state : bool;
    	END_VAR
    	
    	
    	case fsm OF
    		0:
    			if state <> local_state or initial THEN
    				local_state := state;
    				fsm := 1;
    				initial := false;
    				timeout := 1000;
    				END_IF;
    		1:
    			connection := 'tcp:192.168.1.5:6722';
    			conn_handle := io.openport(connection);		
    			fsm := 2;
    		2:
    			portstatus := io.getportstatus(conn_handle);
    			if  portstatus = 0 THEN	
    				fsm := 3;
    			END_IF;
    			timeout := timeout -1;
    			if timeout = 0 THEN
    			io.closeport(conn_handle);
    			fsm := 0;
    			END_IF;
    		
    		3:
    				if local_state = 1 THEN
    					command[0] := 49;
    					command[1] := 49;
    				ELSE
    					command[0] := 50;
    					command[1] := 49;
    				END_IF;
    			
    			
    			io.writeport(conn_handle,adr command[0],10);
    			io.closeport(conn_handle);
    			
    				
    	
    			fsm := 0;
    		ELSE
    			fsm := 0;
    			
    	
    		
     	
    end_case;
    
     
    (*
    METHOD abc //: return type
      // method's body
    END_METHOD
    *)
    (*function block body*)
    END_FUNCTION_BLOCK
    
    

    This is the output of wireshark:

    192.168.1.5 is my laptop
    192.168.1.7 is my unipi

    0_1552505853462_5d700fce-4c1a-467b-bfdc-5b2f53e0cd81-afbeelding.png

    This is the communication of the ST code only. No idea why everything has zero length. And my data is nowhere to be found in the packages.

    Any help would be appreciated!

    Regards,

    Jasper Jackson



  • @jasperjackson said in io.writeport TCP:

    io.writeport(conn_handle,adr command[0],10);

    Got it... one has to keep calling
    io.writeport(conn_handle,adr command[0],10);
    until it returns true :)