Home AMX User Forum NetLinx Studio
Options

Simple UDP communication

Hi there.
I would send UDP packets from my Netlinx controller to a PC (with IP address 192.16.0.10).
My Netlinx source looks something like this in the DEFNE_START section:
IP_CLIENT_OPEN (12345, '192.16.0.10', 54321, 2) (*open the communication*)

instead the DEFINE_EVENT contains:
SEND_STRING 0:12345:0, 'UDP test!'

I cannot receive anything on my PC but I'm sure the listener is ok.
Could someone suggest me how to achieve this simple communication?

Thanks a lot in advance!

Stefano

Comments

  • Options
    AMXJeffAMXJeff Posts: 450
    Why did you pick such a high port number. That means NetLinx has to create a port from 2 all the way upto 12345. That is alot of devices on NetLinx. Pick something smaller, like, lets say...2.

    Not Much to UDP.
    DEFINE_DEVICE
    
    dvTP = 10001:1:0
    dvSocket = 0:2:0;
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1]
    {
         PUSH:
         {
               // was this the actual IP address of the PC???? Maybe 192.168.0.10????
               IP_CLIENT_OPEN (dvSocket.Port, '192.16.0.10', 54321, IP_UDP) (*open the communication*)
    
               // Unlike TCP, UDP opens immediately
               SEND_STRING dvSocket, 'UDP test!'
    
               IP_CLIENT_CLOSE(dvSocket.Port);
         }
    }
    
    


    // Little UDP Test Program, May help to explain UDP Communications
    DEFINE_DEVICE
    
    dvMaster = 0:1:0;
    dvServer = 0:2:0;
    dvServerClient = 0:3:0;
    dvClient = 0:4:0;
    
    dvTP		 = 10001:1:0;
    
    DEFINE_CONSTANT
    
    SERVER_PORT = 50000;
    
    DEFINE_VARIABLE
    
    IP_ADDRESS_STRUCT uMyIPStuff;
    
    DEFINE_START
    
    GET_IP_ADDRESS(dvMaster, uMyIPStuff);
    
    WAIT 100 'START UDP SERVER'
    {
    	IP_SERVER_OPEN(dvServer.Port, SERVER_PORT, IP_UDP);
    }
    
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1]
    {
    	PUSH:
    	{
    		IP_CLIENT_OPEN(dvClient.PORT, uMyIPStuff.IPADDRESS, SERVER_PORT, IP_UDP_2WAY);
    		
    		SEND_STRING dvClient,"'Hello, World!'"
    	}
    }
    
    
    DATA_EVENT[dvClient]
    {
    	ONLINE:
    	{
    		SEND_STRING 0,"'CLIENT OPEN!'";
    	}
    	STRING:
    	{
    		SEND_STRING 0,"'SERVER SAID: ',DATA.TEXT";
    		
    		IP_CLIENT_CLOSE(dvClient.PORT);
    	}
    }
    
    DATA_EVENT[dvServer]
    {
    	STRING:
    	{
    		SEND_STRING 0,"'CLIENT SAID: ',DATA.TEXT";
    		
    		IP_CLIENT_OPEN(dvServerClient.PORT, DATA.SOURCEIP, DATA.SOURCEPORT, IP_UDP);
    		SEND_STRING dvServerClient,'GOT IT!';
    		IP_CLIENT_CLOSE(dvServerClient.PORT);
    	}	
    	OFFLINE:
    	{
    		SEND_STRING 0,"'SERVER CLOSED!'";
    	
    		IP_SERVER_OPEN(dvServer.Port, SERVER_PORT, IP_UDP);
    	}
    	ONLINE:
    	{
    		SEND_STRING 0,"'SERVER OPEN!'";
    	}
    }
    
  • Options
    Thank you very much for the answer AMXJeff!
    I've tried the following code:


    PROGRAM_NAME='UDPTest'
    DEFINE_DEVICE
    SERIAL= 5001:1:0
    dvSocket = 0:2:0

    DEFINE_VARIABLE
    LONG TL_ARRAY[] = {1000, 1000}

    DEFINE_START
    TIMELINE_CREATE (1, TL_ARRAY, 2, TIMELINE_RELATIVE, TIMELINE_REPEAT)

    DEFINE_EVENT
    DATA_EVENT [SERIAL]
    {
    ONLINE:
    {
    SEND_COMMAND DATA.DEVICE, 'SET BAUD 9600,N,8,1'
    SEND_STRING SERIAL, 'Online'
    }
    }
    TIMELINE_EVENT[1]
    {
    SWITCH(TIMELINE.SEQUENCE)
    {
    CASE 1: SEND_STRING SERIAL, 'Serial Test'
    FOR (CONT=1; CONT<=10; CONT++)
    {
    OFF [Relay, 1]
    WAIT 1
    ON [Relay, 1]
    }

    IP_CLIENT_OPEN (dvSocket.Port, '192.16.0.10', 54321, IP_UDP) //IP address is 192.16.0.10 ;-)
    SEND_STRING dvSocket, 'UDP test!'
    IP_CLIENT_CLOSE(dvSocket.Port)

    CASE 2: SEND_STRING SERIAL, 'Serial Test 2'
    OFF [Relay, 1]

    }
    }

    DEFINE_PROGRAM


    but the listener still doesn't receive anything.
    The listener is on port 54321 on the PC and the serial data is shown correctly on my PC using HyperTerminal.
    Can you help me a little more?
  • Options
    AMXJeffAMXJeff Posts: 450
    I would say off hand that the problem is with the UDP Server, or some network issue. I would try the "Little UDP Test Program" that I also gave you and test that you get the responses from Telnet of the master with MSG ON. You should see the following messages.

    CLIENT SAID: Hello, World!
    SERVER SAID: GOT IT!
  • Options
    Thank you very much AMXJeff!!!
    After some attempts I've made it works! Now I can receive packets from a PC and send to another using UDP protocol!

    Thanks thanks thanks!
Sign In or Register to comment.