This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AM5728: TCP/IP client example

Part Number: AM5728
Other Parts Discussed in Thread: ADS1118,

Hi: I've been working with the Sitara AM5728 for about 6 weeks now.  I've just been prototyping, trying to build some libraries to perform some basic things like using using SPI to acquire data from ADS114 and ADS1118 boards (A/D data) or sending CAN messages between a Tiva and a Sitara processor.  I've gotten that stuff to work. I've tried to look through the TI forum for help in solving the various issues I've run into, but the common TI forum scenario is: 1. quite a few people have the same or similar issue as I'm having, 2. there will be a series responses and more questions between the user and TI support, 3. the thread finally gets locked 4. I read through the thread from beginning to end a few more times, 5. I usually never find the answer to the original question.

My current issue is that I need to: 1. have a TCP/IP client send and receive messages to a TCP/IP Server. This is very basic stuff. I've done this many times in many languages (C, C++, C#, VB) using many different OSs and SDKs. But I have not been successful with the Sitara.

What I would like to initially prototype is: just have the TCP/IP client send a message to a TCP/IP server. (I'll figure out the other use case, once I seen an actual example that works)

Could someone actually send me a code snippet that does this? There must be hundreds of users that are already doing this.

I'll have a PC acting as the server running some test code that will print out any message. I( can also watch the packets via Wireshark.

The code snippet can be for Linux or RTOS (I'll use either)

I need to have the TCP/IP communication pass through one of the 2 default PRU RJ45s : PRU1-ETH0 or PRU2-ETH1

Please don't refer me to another example that might be similar - I've already tried and failed to get those working

I just need one snippet  that shows 1. setting up the socket, 2. connecting to the server, and 3. sending 1 message

Thanks in advance.

  • Kenneth,

    Below is the code snippet from NDK v2.x user guide spru523j, not sure if you have also tried:

    void EchoTcp( IPN IPAddr )

    {

    SOCKET s = INVALID_SOCKET;

    struct sockaddr_in sin1;

    int i;

    char *pBuf = 0;

    struct timeval timeout;

    // Allocate the file descriptor environment for this Task

    fdOpenSession( (HANDLE)Task_self() );

    printf("\n== Start TCP Echo Client Test ==\n");

    // Create test socket

    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if( s == INVALID_SOCKET )

    {

    printf("failed socket create (%d)\n",fdError());

    goto leave;

    }

    // Prepare address for connect

    bzero( &sin1, sizeof(struct sockaddr_in) );

    sin1.sin_family = AF_INET;

    sin1.sin_addr.s_addr = IPAddr;

    sin1.sin_port = htons(7);

    // Connect socket

    if( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )

    {

    printf("failed connect (%d)\n",fdError());

    goto leave;

    }

    // Allocate a working buffer

    if( !(pBuf = malloc( 4096 )) )

    {

    printf("failed temp buffer allocation\n");

    goto leave;

    }

    // Fill buffer with a test pattern

    for(i=0; i<4096; i++)

    *(pBuf+i) = (char)i;

    // Send the buffer

    if( send( s, pBuf, 4096, 0 ) < 0 )

    {

    printf("send failed (%d)\n",fdError());

    goto leave;

    }

    ...

    Regards,

    Garrett

  • thanks Garrett - I'm home now - I'll try it 1st thing tomorrow - I looked at it in the guide before but I had never tried using it. The IPAddr  will determine the address of the destination server but where is it determined which of the Sitara's Ethernet ports is being used.  I need to have the communication pass through one of the PRU ports. Tomorrow I'll just see if I see if I can get it to build, loaded, and running. And then see via Wireshark if any data is coming out any of the 6 available ports.

  • Kenneth,

    >>where is it determined which of the Sitara's Ethernet ports is being used

    You can try to bind the IP address of Ethernet port after the socket is created and before calling connect().

    struct sockaddr_in client_addr;

    memset(&client_addr, 0, sizeof(struct sockaddr_in));

    client_addr.sin_family = AF_INET;
    client_addr.sin_addr.s_addr = inet_addr("192.168.1.4"); //eth0
    bind(s, (struct sockaddr*)&client_addr, sizeof(client_addr));

    Regards, Garrett

  • Does this imply that RTOS sets the PRU2 Eth0 IP Address to 192.168.1.4 on power up?
  • Kenneth,

    The NIMU_ICSS_FtpExample_idkAM572x_wSocLib_armExampleproject shows how the PRU eth0/eth1 IP address is set up.

    Actually the ftpserver_thread_entry() function in the project also demonstrates the TCP based ftp server setup that may help your project development as well.

    Regards,
    Garrett
  • Thanks again!!! - If you don't mind - Could you text me your phone number - I won't bug you - just a 5 minute chat - my number is 860.310.7141
  • I'm looking at the ftp sample and it is much easier to follow. I should be able to refactor it into a tcp client and a tcp server. Thanks again!
    I don't know why I never looked at that sample before.
  • I was finally able to get the ftp example app to build. It listens to connections at 192.168.1.4 port 21. I wrote a little C# application to run on my PC that was a TcpClient. I tried to connect to 192.168.1.4 port 21. The breakpoint after the accept() method on the Sitara was hit. So I'm good to go. I'll refactor the application have 2 additional threads. One to be a Tcp Server and the other to be a Tcp Client.