CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problems Receiving Strings using serial port

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
myloudharim



Joined: 13 Nov 2017
Posts: 10
Location: philly

View user's profile Send private message

Problems Receiving Strings using serial port
PostPosted: Fri Nov 17, 2017 10:19 am     Reply with quote

Hi, I'm new here, i would like to post my problem and hope to find help
so that everybody will take advantage from that.

I'm having troubles receiving string using 16f876. I used a usb to serial coverter with MAX3232.
I used a bluetooth HC05 and used voltage converter, but so far still no success.

The idea is to receive a string from a serial port and process it. Here is my code:
Code:
//#include "W:\CCS Projects\OneStringArray.h"
#include <16F876.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//----------------------variables declaration-------------------------------

unsigned int num1, num2, num3, num4, num5, num6, num7, num8;

unsigned char strnum1[2];
unsigned char strnum2[3], strnum3[3],strnum4[3],strnum5[3],strnum6[3],strnum7[3], strnum8[3];

unsigned char delimiter[2] = ".";

unsigned char rcvdata[50];
unsigned char mystring[50] ;

//-------------------------------------------------------------------------

void CheckReceivedData() // function to check the data available //
{   
      if(kbhit()!=0)
      {    // if data is available
 
      mystring[49] = gets(rcvdata);
       
   
printf(" data is : %s\n\n\r ",mystring); // print all array data     

num1 = atoi(  strcpy(strnum1,strtok(mystring,delimiter))  ); 
num2 = atoi(  strcpy(strnum2,strtok(NULL,delimiter))  );
num3 = atoi(  strcpy(strnum3,strtok(NULL,delimiter))  );
num4 = atoi(  strcpy(strnum4,strtok(NULL,delimiter))  );
num5 = atoi(  strcpy(strnum5,strtok(NULL,delimiter))  );
num6 = atoi(  strcpy(strnum6,strtok(NULL,delimiter))  );
num7 = atoi(  strcpy(strnum7,strtok(NULL,delimiter))  );
num8 = atoi(  strcpy(strnum8,strtok(NULL,delimiter))  );

printf("num1 is %d\n\r",num1);
printf("num2 : %d\n\r ",num2);
printf("num3 : %d\n\r ",num3);
printf("num4 : %d\n\r ",num4);
printf("num5 : %d\n\r ",num5);
printf("num6 : %d\n\r ",num6);
printf("num7 : %d\n\r ",num7);
printf("num8 : %d\n\n\r ",num8);


      }
      else return;
}


//  data has to be inserted with this configuration:  0.5.23.10.17.21.45.53   


void main()
{
   // TODO: USER CODE!!
for(;;)
{
printf("Waiting for data...\n\r ");

CheckReceivedData();
delay_ms(1000);

}

}

This code does not work. I tried another code:
Code:

#include <16F876.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//---------------------------------------variables declaration---------------------------------------------

unsigned int num1, num2, num3, num4, num5, num6, num7, num8;

unsigned char strnum1[2];
unsigned char strnum2[3], strnum3[3],strnum4[3],strnum5[3],strnum6[3],strnum7[3], strnum8[3];

unsigned char delimiter[2] = ".";

unsigned char mystring[50] = {0} ;

int i =0;
//---------------------------------------------------------------------------------------------------------

void CheckReceivedData() // function to check the Settings //
{   
      if(kbhit()!=0)
      {    // if data is available
 
       mystring[i]=getc();
       
       while(true){
       if((mystring[i] !='\r' )|( mystring[i]!='\n'))
            break;
            i++;
            }//end while
           mystring[i+1] = '\0';
           i=0;         
   
printf(" data is : %s\n\n\r ",mystring); // print all array data     

num1 = atoi(  strcpy(strnum1,strtok(mystring,delimiter))  ); 
num2 = atoi(  strcpy(strnum2,strtok(NULL,delimiter))  );
num3 = atoi(  strcpy(strnum3,strtok(NULL,delimiter))  );
num4 = atoi(  strcpy(strnum4,strtok(NULL,delimiter))  );
num5 = atoi(  strcpy(strnum5,strtok(NULL,delimiter))  );
num6 = atoi(  strcpy(strnum6,strtok(NULL,delimiter))  );
num7 = atoi(  strcpy(strnum7,strtok(NULL,delimiter))  );
num8 = atoi(  strcpy(strnum8,strtok(NULL,delimiter))  );

printf("num1 is %d\n\r",num1);
printf("num2 : %d\n\r ",num2);
printf("num3 : %d\n\r ",num3);
printf("num4 : %d\n\r ",num4);
printf("num5 : %d\n\r ",num5);
printf("num6 : %d\n\r ",num6);
printf("num7 : %d\n\r ",num7);
printf("num8 : %d\n\n\r ",num8);
      }
      else return;
}

//    0.5.23.10.17.21.45    //

void main()
{
   // TODO: USER CODE!!
for(;;)
{
printf("Waiting for data...\n\r ");

CheckReceivedData();
delay_ms(1000);

}

}


Is there any solution for that? Confused
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 17, 2017 12:19 pm     Reply with quote

Get rid of your code and use the Ex_sisr.c example program. It's in
the Examples folder for the CCS compiler on your PC.
Ttelmah



Joined: 11 Mar 2010
Posts: 19562

View user's profile Send private message

PostPosted: Fri Nov 17, 2017 12:48 pm     Reply with quote

Also, as a general comment you should always have 'errors' in your RS232 declaration.

As it currently stands (assuming the strings are arriving perhaps every second), the UART will become hung as soon as the delay in the main code occurs, if any data arrives during this.
If you look at the 'gets' source code, this can easily be re-written to use the buffered data from ex_sisr.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group