|
|
View previous topic :: View next topic |
Author |
Message |
myloudharim
Joined: 13 Nov 2017 Posts: 10 Location: philly
|
Problems Receiving Strings using serial port |
Posted: Fri Nov 17, 2017 10:19 am |
|
|
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? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 17, 2017 12:19 pm |
|
|
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: 19569
|
|
Posted: Fri Nov 17, 2017 12:48 pm |
|
|
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. |
|
|
|
|
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
|