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.

Communicating via Ethernet between Concerto and .NET C# Program

I am attempting to communicate via ethernet to the Concerto eval board.  On the Concerto, I am running the enet_uip example on the M3 core, modified to use a fixed IP address.  On the host side, I am running the C# code appended below.  I am not using the C28 at all.  Using WireShark, I see that the ping is successful, but during connection the Concerto acknowleges the host, but then returns an RST.  After that, the writes in the C# code do not create any communication seen by Wireshark.

 

    

Has anyone done this before?  What changes are required in the enet_uip code?

I am using a recent version of controlSUITE and CCS version 5.1.0.09000.  I am using the controlCARD TMDXCNCDH52C1 with the XDS100v2 emulator.

The connect and write routines of the C# program follows.  I am simply trying to write the byte 187 to the Concerto.

       

protected void ConnectPort()

        {

string hostIp;

           

int hostPort;

           

if(rdoConcerto.Checked)

{

               

hostIp = ConcertoHostIp;

               

hostPort = ConcertoHostPort;

            }

           

else


            {

               

hostIp = AzLvlHostIp;

               

hostPort = AzLvlHostPort;

            }

            

mClient = new TcpClient();

           

if (!(mClient != null && mClient.Connected))

            {

               

// Make sure that host IP exists


               

Ping pingSender = new Ping();

               

// Attempt to connect to the host


               

try


                {

                   

PingReply reply = pingSender.Send(hostIp, 200);

                   

if (reply.Status != IPStatus.Success)

                    {

                       

txtGet.Text = "Ping failed.";

                    }

                   

else


                    {

                       

mClient = new TcpClient(hostIp, hostPort);

                       

mNs = mClient.GetStream();

                       

txtGet.Text = "Connection Established";

                    }

                }

               

catch (Exception e)

                {

                   

txtGet.Text = "Unable to open Ethernet connection to " + hostIp + ":" + hostPort.ToString() + " : " + e.Message;

                }

            }

        }

        

protected void SendReceive()

        {

           

int length;

           

try


            {

               

byte[] writeData = new byte[10];

               

while (mNs.DataAvailable)

                {

                   

// Receive the response


                   

int bytes = mNs.Read(writeData, 0, writeData.Length);

                }

               

if(cbConvertStringToBytes.Checked)

                {

                   

// Trasfer command to a byte array


                   

txtSend.Text += "\r\n";

                   

writeData = Encoding.ASCII.GetBytes(txtSend.Text);

                   

length = txtSend.Text.Length + 2;

                }

               

else


                {

                   

writeData[0] = Convert.ToByte(txtSend.Text);

                   

length = 1;

                }

               

// Send message out port


               

mNs.Write(writeData, 0, length);

               

Thread.Sleep(500);

               

byte[] readData = new byte[100];

               

int bytesRead = 0;

               

byte[] oneByte = new byte[1];

               

while (mNs.DataAvailable && !(readData[bytesRead] == '\n' && readData[bytesRead -1] == '\r'))

                {

                   

// Receive the response


                   

//bytes += mNs.Read(writeData, bytes, 1);


                   

bytesRead += mNs.Read(oneByte, 0, 1);

                   

readData[bytesRead - 1] = oneByte[0];

                }

               

txtGet.Text = Encoding.ASCII.GetString(readData,2,4);

            }

           

catch (Exception e)

            {

               

txtGet.Text = "Unable to send command " + txtSend.Text + " : " + e.Message;

            }

        }

    }

}

  • hi Don,

    I've been on the same place you're now. I had to fight for literally WEEKS for having my Concerto F28M35H52C1 to communicate over the ethernet on a standard that I wanted my code to run.

    To just to run the example as it is, I'm assuming you're not having much problems. You flash it, type in your browser (I hope you use Chrome or Firefox) the IP address you set and voilá... a nice webpage comes up. Now you want to send individual messages from your C# GUI to it. That is EXACTLY what I've been through, so let me give you some hints on what I've learned:

    - Do not try to mess with the the DMA stuff of the example, just keep using it.

    - the file you want to change is: httpd.c. That code is actually handling the communication. On my code I eventually changed to ethernet_handler.c because it's not a HTTP protocol at all, but don't do that for now, it complicates some stuff with the config file 'uip-conf'.

    - On this thread I started http://e2e.ti.com/support/microcontrollers/tms320c2000_32-bit_real-time_mcus/f/171/t/180339.aspx you can follow some problems I had on trying to change the linker file and messing up the ethernet, might be useful reading

    - Below is a (hopefully useful) snippet of my ethernet_handler.c, specifically the void EthernetAppCall(void) (in the example is called void httpd_appcall(void).

    #define BUF_CHAR ((char *)uip_appdata) // <<< this is defined just to avoid compiler warnings

    void EthernetAppCall(void) {

    switch (uip_conn->lport) {

    case HTONS(TCP_PORT_NUMBER): {

    if (uip_connected()) {
    // If just connect, just return and wait for the message
    return;
    } else if (uip_poll()) {
    // I don't think this will ever happen, but it's no problem
    return;
    } else if (uip_newdata()) {
    // If we have actual useful data coming, parse it and answer accordingly

    // Check if its a 'Hello World' test message // Delete this code before final release
    if (BUF_APPDATA[0] == 'x') {
    uip_send("Hello world!", sizeof("Hello world!"));
    } else {


    /*
    this part of the code i'm using the #include "utils/cmdline.h" to process the commands coming from my VB.net

    // Try parse the message
    int response = COMMS_parse_message(BUF_CHAR); <<< wrapper to the cmdline.h

    than depending on the value of response I can:
    uip_send() <<< send a message back
    uip_abort() <<< abort if it's crap

    */

    }
    }

    else if (uip_acked()) {
    // If it's an acknowledge, just close the connection
    uip_close();
    }
    return;
    }

    default: {
    // Should never happen, unless someone else is trying to communicate with my MY board
    uip_abort();
    break;
    }
    }
    }

    after a bit I was while to clear out all the little functions that was being used by the javascripts on the webpage, and to send the parts of the webpage, but for now I would try to to change the minimum as possible until it works. Than you can start clean the code.

    in case you're interested here is my VB.net code for a form with a button, 3 input fields and a listbox, it's a very basic and dumb code as I'm still testing it out how this interface will happen, but it's operational.

    Imports System.Net
    Imports System.Net.Sockets

    Public Class Form1

    Dim conn As TcpClient = Nothing


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    Me.Cursor = Cursors.WaitCursor

    Dim address As IPAddress = IPAddress.Parse(ipbox.Text.Replace(" ", ""))
    Dim port As Int32 = Integer.Parse(portBox.Text.Replace(" ", ""))

    Try
    conn = New TcpClient(ipbox.Text.Replace(" ", ""), port)

    ' Get the message from the box, translate it into ASCII and store it as a Byte array.
    Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(messageBox.Text)

    ' Get a client stream for reading and writing.
    ' Stream stream = client.GetStream();
    Dim stream As NetworkStream = conn.GetStream()

    ' Send the message to the connected TcpServer.
    stream.Write(data, 0, data.Length)

    logit(messageBox.Text)

    ' Receive the TcpServer.response.
    ' Buffer to store the response bytes.
    data = New [Byte](256) {}

    ' String to store the response ASCII representation.
    Dim responseData As [String] = [String].Empty

    ' Read the first batch of the TcpServer response bytes.
    Dim count As Int32 = stream.Read(data, 0, data.Length)
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, count)

    logit(responseData)

    stream.Close()
    conn.Close()

    Catch ex As Exception
    logit("Error: " + ex.Message)

    End Try


    Me.Cursor = Cursors.Default

    End Sub

    Private Sub logit(ByVal message As String)

    Dim hr As String = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() + " - "
    logList.Items.Insert(0, hr + message)
    logList.Refresh()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    logList.Items.Clear()
    End Sub

    Hope it helps, happy coding!

  • That worked! Thank you.  You saved me a lot of time, I appreciate it.

    Just some notes:

    else if (uip_poll()) {
          // I don't think this will never happen, but it's no problem

    I saw this condition being true continuously after connection -- I think that is how it is supposed to work.

    I was able to keep the web page functionality in addition to connecting with my .NET program by using a port other than 80 in my .NET program.  I added one line to httpd_init() in the same file to make it listen to both ports, then added your code as a second case in the switch.

    void httpd_init(void)

    {

      // Listen to port 80.

    uip_listen(HTONS(80));

     // Add this line to listen to port 2000 as well.

    uip_listen(HTONS(2000));

    }

     

     

  • well..... I'm not sure on the uip_poll(), I guess it depends on what your C# code is doing, if you checked my VB.net code it connects, sends the message, get a reply and closes. It doesn't stay up polling the processor, so that "shouldn't happen" comment" it's for my application, you can check the original example is doing something different. But I'll check on mine to see if it's receiving anything on there.

    regarding the listening ports;

    yep, I knew. On my app it's running on port 83 to not be confused to some web service. And I totally removed the web-server as it's useless for my app. But if it's good for yours.

    happy you got it working.

    cheers

  • check this basic tcp/ip operations

    http://csharp.net-informations.com/communications/csharp-socket-programming.htm

    wills.

  • This is a very good post.
    I was trying to explore a way to do real-time control from MATLAB/SIMULINK to concerto via ethernet.
    It seems that you can control the concerto via ethernet and communicate with C#.
    Are you doing real-time control directly from MATLAB? If so, how you achieve that? If not, do you have any suggestions on how to do that? 
    I am quite new and I would like to learn from your experience.
    Thanks and regards,
    Junyi
  • Very good post. But now I need to stabilish just a UDP connection to send some sensor data and I can't see how and where I need to define this connection.

  • zmeira,
    I see that your query is also in other forum thread . I've responded over there.
    e2e.ti.com/.../2260110

    Best Regards
    Santosh Athuru