This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

receive data and send date LPC2294 ARM7

Hello folks,

I have made a connection between a PC and the LPC2294 with the TCP/IP Port. I am able to send some data but if I want to receive something from the PC I get nothing.

The PC send me a string with only the number "1" in it. Then the PC encodes this number into bytes and sends it over and over again...about 100 times.

My problem now is, how can i receive this bytes from the PC. I catch this bytes and check if it is a "1".

Here is my code. The LED switch and client server example code. But only the parts which I have changed. Hope there is someone who can help me! :-)

/*--------------------------- Process received data  ------------------------*/
void procrec (U8 *buf) {
        if (buf[0] == 1)
                {
            IOCLR0 = 0x00000100;             /* Turn LED On  (P0.8 = 0)      */
         }
         else {
            IOSET0 = 0x00000100;             /* Turn LED Off (P0.8 = 1)      */
         }
}
/*--------------------------- TCP send --------------------------------------*/
void send_data (){
   U8 *sendbuf;
   U8 p2;
   U32 max;
   int i;
   static char array2[26] = {"5566.77.555.4.1.23.43.90."};
   /* TCP */
   if (socket_tcp != 0) {
      /* Start Connection */
      switch (tcp_get_state(socket_tcp)) {
         case TCP_STATE_FREE:
         case TCP_STATE_CLOSED:
            tcp_connect (socket_tcp, Rem_IP, PORT_NUM, 0);
            break;
         case TCP_STATE_CONNECT:
            if (tcp_check_send (socket_tcp) == __TRUE) {
               sendbuf = tcp_get_buf(SENDLEN);
              for (i = 0; i<max; i++)
                          {
                                sendbuf[i] = array2[i];
                                }

              tcp_send (socket_tcp, sendbuf, SENDLEN);
            }
            break;
      }
   }
}

int main (void) {
   /* Main Thread of the TcpNet */
   U8 p2val, cnt, lshf;
   U8 protocol;
    init ();
   init_TcpNet ();
   protocol = PROTOCOL;
   switch (protocol) {
      case TCP:
         socket_tcp = tcp_get_socket (TCP_TYPE_CLIENT, 0, 5005, tcp_callback);
         break;
   }
   p2val = 1;
   cnt   = 0;
  lshf  = 1;  /* left shift */
   while (1) {
     timer_poll ();
      main_TcpNet ();
   if (tick == __TRUE) {
         if (++cnt == SPEED) {
           if (p2val & 1)
                    {
               IOCLR0 = 0x00000100;

                        }
                else
                        {
               IOSET0 = 0x00000100;
                }
                send_data ();
                p2val = lshf ? (p2val << 1) : (p2val >> 1);
            if (p2val == 0x80) lshf = 0;
            if (p2val == 0x01) lshf = 1;
            cnt = 0;
        }
        tick = __FALSE;

     }
         if (socket_tcp != 0) {
      tcp_listen (socket_tcp, PORT_NUM);
          }
  }
}

PS: Sorry about my bad english!

  • The first thing I saw was your use of uninitialized 'max' in send_data(). I stopped looking at that point.

  • @ Dan Henry

    I have initialized max in send_data(). But I can not receive something. Find you something else what wrong is?

    Thx!

  • Hallo,

    Sorry, i expressed myself in an unclearly way. I have my code with the part "max" changed but not post it again. I have set max for example on 9. The Problem is the same. I can send the array to the PC but I can not receive something from the PC. I do not know were my mitakes are. Thanks for your help! Maybe you find some other mistakes in my code.

    Thx!

  • Debugging?

    Does your code run the send_data() function?

    What parts?

    What is the return values from any tcp calls?

    Do you get to tcp_send()? Does it give the expected return value?

  • Hallo and sorry for the late answer I had not enough time the last days ;-)
    So I have check the Code at the Debug-Mode and my Programm runs. The Programm send data to the PC but it receive nothing.
    My questions stand in the variable *buf that what the PC send to the MC and in the example LEDswitch in the main-funtion the do not call the funktion "procrec" why? I have change my code with function call procrec and in the Debug-Mode the programm runs in the funktion but the variable *buf is 0x00.
    Maybe somebody have a solution for the problem.

  • Ok I have found one error in my deliberation.
    In the function tcp_callback have I a pointer(U8 *ptr) what on show me what is send to my MC. In the function procrec I hand over the pointer and in the variable buf stand that was may MC receive from the PC or is that wrong?
    On the PC I code a programm with VB-Net to send me a string to the MC. So I want to convert the buf into a string and search in the string after that was the PC send. But it does not work. Have someone an idea for that problem. Sorry about my bad english!
    Thx
    Hotte

    U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par)
    (evt) {
          case TCP_EVT_DATA:
             procrec(ptr);
             break;
             ......
    
    void procrec (U8 *buf)
    {
    int i;
    int zw;
    char data_reception[1];
    char data[10];
    for (i= 0; i<10; i++)
    {
     zw=buf[i];
     sprintf(data_reception, "%d", zw);
     strcat(data ,data_reception );
    }
    .....
    }
    

  • First off, a C string of size 1 can only contain the terminating '\0' character - so it doesn't matter how many times you concatenat - your string will not grow.

    Second: You use sprintf() to convert a byte into a decimal number in ASCII format - but the largest value that may fit in a byte is 255 - and the text buffer to take this value converted to decimal must be at least four bytes large: '2', '5', '5', '\0'. Your code will get a buffer overflow.

    Third: You never set data[10] to empty, so what start value do you think you have when you later call strcat()? strcat() may think there already is 1317 characters in data[] - even if data[] was only declared to hold 10 characters including any termination.

    Fourth: Concatenating 10 ASCII strings after each other, each with up to three characters, would require a destination string that is 10*3+1 = 31 bytes large. But starting to compute exact buffer sizes will soon end up in a buffer overflow. Your current code does overflow the buffer.

    Fifth: What happens if you concatenate two numbers in ASCII form after each other? Let's say one was the value 10 and the other was the value 127. Then you get "10127". How will you manage to know if this represents 10,127 or 101,2?

  • Thank-you Per Westermark for your helpful tips. My program runs. It is not beautiful from the code but for that I have now enough time to check it and to make it better.;-)

    Thx
    Hotte