Thread: receiving in a value and i am storing it in a buff. explain me logic here

  1. #1
    Registered User
    Join Date
    Jan 2014
    Location
    Bangalore
    Posts
    2

    receiving in a value and i am storing it in a buff. explain me logic here

    Code:
    im=0;		i=0;
    		while(im<12)                                                
    		   {
    		   	 m[im]=buff[i]; 
    			 i++;
    			 im++;
    		   }
    	   		 im=0;
    			 j=0;
    	  	   while(m[im]==command1[im])
    		   {
    		   	im++;
    			j++;
    		   }
    		   ScrCls();
    		   Lcdprintf("j = %d",j);
    in the above code value is stored in buffer and is 12345678:123 of length 12. when i am receiving the number, it is not constant.explain me why?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is this code supposed to do and how does it not work? Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Location
    Bangalore
    Posts
    2
    Code:
            while(1)    {
            ret = (PortRecvs(0,buff,sizeof(buff)-1,1000)<0);     // r=registration is the command used in recv         
            if(ret<0)       
            { printf("j = %d",j);
               DelayMs(1000);
             if(j == 12)         
             { if(PortSends(0,DeviceId,sizeof(DeviceId))!=0)// working fine untill here 
                  memset(buff,0,sizeof(buff));
                  while(1)
             {
                 ret = (PortRecvs(0,buff,sizeof(buff)-1,1000)<0);   // receiving number     
                 if(ret<0)       {printf("Receive Data:\n%d\n",buff);
             }
                 for(i=48;i<60;i++)
             {
                          
                  printf("%c",buff[i]);
                  DelayMs(500);
               }
                  im=0;
                  i=48;
                  while(im<12)                                                
             {
                  m2[im]=buff[i]; 
                  i++;
                  im++;
             }
                 ScrCls();
                 printf("Reg no:\n %s",m2);// reg no is not constant keeps on fluctuating
                 DelayMs(2000);
                 break;
            }
            }
                 if(PortSends(0,"Registration Completed",22)!=0)    
                 while(1)
           {
                  ret = (PortRecvs(0,buff,sizeof(buff)-1,1000)<0);             
                  if(ret<0)       
           { 
                   ScrCls();
                   printf("Receive Fail..");
                      
             }
                  else
           {
                     ScrCls();
                for(i=160;i<170;i++)
                  {
                           
                    printf("%c",buff[i]);
                    DelayMs(500);
                 }
                     printf("Receive Close Command :\n%s\n",buff);
                     DelayMs(3000);
                      
                   }
                      break;
                 }
                     im=0;
                     i=160;
                     while(im<5)                                        
                 {
                        m1[im]=buff[i];
                          i++;
                         im++;
                   }
                        im=0;
                        x=0;
                      while(m1[im]==command2[im])
                 {
                       im++;
                       x++;
                 }                                                     
                      ScrCls();
                      printf(" x = %d \n",x);
                      DelayMs(500);
                      while(x==1 || x==5)
               {
                      ScrCls();
                      printf("Close command....");
                      DelayMs(500);
                        break;
                }    
             }
                      PortClose(0);
    I want to register my device using serial port communication between "visual studio c -pos machine- Java fX".
    So ,
    1.receive :i need to get registration command from Java fx
    2.send: I am comparing length , if it is 12 then i am sending device id of length 8
    3.receive : now i need to receive registration number from the java fx .i.e., ex/- 12345678:214.
    4.send: if i get registration number then i am comparing , if length = 12 then i am sending registration completed of length 22.
    5.receive : now , if length =22 then he validates and sends close command to me.

    issue: in step 3 , registration number is not constant , it keeps on overlapping or sometimes i am not getting the data.

    I searched many blogs and did my attempt, but i am not getting what the exact problem is? please explain me.

    why Java fx is used for offline application?
    Last edited by parvathi443; 01-16-2014 at 01:16 AM. Reason: to give clear information

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Unless PortRecvs is unlike all the other comm functions I've seen, it's not going to wait for 1000 bytes to be in the buffer, so you need to be checking (every single time you use it) how much information was actually received; and if you receive partial data (compared to what you were expecting, assuming you know how much you're expecting) then you need to keep Recv'ing until you get it all. (This would appear to be a repeat of the advice you got here, but it doesn't appear to have stuck.)

    Also the fact that you are using character 48 through 60 of a buffer you believe will have 12 characters in it is perhaps worrisome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-09-2012, 11:55 AM
  2. Can someone explain the logic of the program
    By chibi.coder in forum C Programming
    Replies: 5
    Last Post: 12-25-2011, 09:23 AM
  3. Replies: 2
    Last Post: 10-14-2009, 07:45 AM
  4. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  5. Passing Objects to Constructors and Receiving a Logic Error
    By CaptainMorgan in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2006, 06:39 AM

Tags for this Thread