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 support@ccsinfo.com

Bluetooth not receiving data on UART
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Billy9689



Joined: 01 Dec 2017
Posts: 24

View user's profile Send private message

PostPosted: Sat Dec 16, 2017 10:54 pm     Reply with quote

OK a quick question. Is that possible that I send a string of data that contain a self defined terminator, for example a "!" or 0x21 in hex (since I think the Bluetooth module is transmitting in hex format). This will tell the pic that my data is end here and I just read from the ":" to "!" (excluding the "!" of course). The actual function I want the pic to receive data via Bluetooth is for it to initiate some function, for example when I send "1" from my smartphone it will tell my sensor to start collect data and when "0" is send the pic will tell the sensor to stop collecting data.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Dec 17, 2017 1:09 am     Reply with quote

Yes.

Means you 'lose' one number that can be sent.

For what you describe, with a very limited range of commands needed, would work well.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Dec 17, 2017 7:51 am     Reply with quote

A clever (aka 'old school' ) programmer can get 240 commands from 15 bytes though it takes a 'bit' of coding..... Wink
Billy9689



Joined: 01 Dec 2017
Posts: 24

View user's profile Send private message

PostPosted: Mon Dec 18, 2017 2:49 am     Reply with quote

Hi guys I come back with more question Sad

As I mentioned previously I wanted to try send my string which ia sandwiched between a self defined header and terminator. Then I can use strtok function to cut my string out from the header and terminator and I can obtain my string back. Seems legit right?

So I just add in strtok function into my code, after bkbhit read my input.
The code are listed below:
Code:
#include <33EP256MU806.h>
#device ADC=12
#use delay(crystal=8Mhz, clock=120Mhz, LOCK)
#fuses NOWDT, NOPROTECT

#pin_select U1TX=PIN_F4
#pin_select U1RX=PIN_F3
#use rs232(baud=19200, UART1, bits=8, parity=N, errors) //enable interrupt receive buffer

#define pixels 5324
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BUFFER_SIZE 64
byte buffer[BUFFER_SIZE];
byte next_in = 0;
byte next_out = 0;
int buf = 0;

#int_rda
void serial_isr() {
   static int1 have_data=FALSE;
   int t;
   t =getc();
   if (t==0) //we have received a break
   {
      if (have_data)
      {
          buffer[next_in]=0; //terminate buffer
          have_data=FALSE; //stop receiving
      }
      else
          break; //throw if no data
   }
   else
   {
       have_data=TRUE;
       buffer[next_in]=t;
   }
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

byte bgetc() {
   byte c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}

void mtbuf(void){  // clear all recived chars in buffer
 while ( bkbhit) { bgetc();};
}

void BT_wake (void)
{
   output_low(PIN_F1); //disable sleep on Bluetooth module
   while (input(PIN_G6)==0)
       ; //wait if chip is in a hardware reset
   output_low(PIN_G6); //set low to mode pin on Bluetooth module
   delay_ms(1);
   output_float(PIN_G6); //release line
   while (input(PIN_G6)==0)
       ; //wait for the chip to finish resetting
}

void main(void)
{
int i = 0;
char out [100] = "TXDT "//header for bluetooth module to send data
char outt [100] = "TXDT "//header for bluetooth module to send data
char input_str [100]; //an array to store the input
char term [3] = "#!"; //indicator for stktok to cut
char *ptr;
BT_wake(); //wake the chip
   delay_ms (20000); //delay 1/3 minutes to establish connection
   //output some introductory message after startup
   puts("TXDT BA"); //Output BA
   putc('\r');
   putc('\n');
   delay_ms (1000);
   puts("TXDT B1");  //Output B1
   putc('\r');
   putc('\n');
   delay_ms (1000);
   //fill_buffer (); //fill the buffer
   enable_interrupts(int_rda);
   enable_interrupts(GLOBAL); //start the receive buffer
while (true)
   {
   mtbuf ();
delay_ms (10000); //delay 10 sec for user input
while(bkbhit)
   {
   input_str [i] = bgetc() ; //read input value
          if (input_str[i]==0) //data finished
             break; //exit
          i++;
   }
input_str [i] = '\0';//add a null terminator
ptr = strtok (input_str, term);
ptr = strtok (0, term);
strncat (outt, ptr, 100); // combine ptr with bluetooth header
strncat (out, input_str, 32); //combine the original input with bluetooth header
puts (out); //send via bluetooth (original input)
putc('\r'); //CR
putc('\n'); //LF
delay_ms (1000); // some delay for human eye to see the changes
puts (outt); //send via bluetooth (cut out string)
putc('\r'); //CR
putc('\n'); //LF
input_str = "0"; //clear the string
   input = "0";
   out = "TXDT "; //reset the header
   outt = "TXDT ";
   i = 0; //reset the value for next loop
}
}

So here I'm using b233121c as my input, where 23 and 21 are hex value of # and !.
However, I can only get the input string displayed, which are 00 00 00 00 AB 23 31 21 but not the cut out part.
I also tried to change the "term" array from "#!" to "bc" and try again with input of b3142c00. Similar results is obtained. I get only 00 00 00 00 AB 31 42 C0 00, but not the cut out string.
I feel something fishy here so I try to copy the whole input string and display the original and the copy version by the following code
Code:
 //parts above this are the same as previous
while(bkbhit)
   {
   input_str [i] = bgetc() ; //read input value
          if (input_str[i]==0) //data finished
             break; //exit
          i++;
   }
   input_str [i] = '\0';//add a null terminator
   strcpy (input, input_str);
   puts ("TXDT 01"); //the first display (original array, where the input from smartphone is stored)
   putc('\r');
   putc('\n');
   strncat (out, input_str, 32); //display the original array
   delay_ms (1000);
   puts (out);
   putc('\r');
   putc('\n');
   delay_ms (1000);
   puts ("TXDT 02"); //the second display (copy version)
   putc('\r');
   putc('\n');
   strncat (outt, input, 32); //display the copied array
   delay_ms (1000);
   input_str = "0"; //clear the string
   input = "0";
   out = "TXDT "; //reset the header
   outt = "TXDT ";
   i = 0; //reset the value for next loop
}
}

As what I expected, I can display the original version but not the copied version.
For example my input is b123456c, and below are what shown on my smartphone screen:
01 00 00 00 00 00 00 00
00 00 00 00 AB 12 34 56
02 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 <---- (same thing as second should output here but no)
Still in operation, waiting for new input, tried to input again with e010203f
01 00 00 00 00 00 00 00
00 00 00 00 AE 01 02 03
02 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 <---- (same thing as second should output here but no, again...)
I wonder whats going wrong here? Why I can display using the input_str (the original array which store the input from smartphone) but not the copied one? By right both should consist of the same thing but it seems like the input_str is just exclusively for display in the smartphone screen but not to extract data from it.
Can somebody please help me ? If I can extract information out from the input string I can end this phase and proceed to next phase so please help me. Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Dec 18, 2017 3:18 am     Reply with quote

There is still this fundamental problem:

while(bkbhit)

This will return 'FALSE', when you have looped round, and _data has not yet_ arrived.

You have to understand that your loop can happen far faster than data itself arrives. You need to only start actually saving data to the output buffer, when you see your 'header', and only exit when either you see your 'terminator', or there is some form of timeout.....

input_str is filling with all this:

00 00 00 00 00 00 00
00 00 00 00

before your data, and:

00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

after your actual data.

You need to be searching _for_ the header, and only write the data when this is seen, and then stop writing when the terminator is seen (or a timeout).

Think about human radio communications. There has to be something to stop the communication or you sit listening to the static. You have the terminator, you need your actual 'transfer to buffer' code to stop at the terminator.
Billy9689



Joined: 01 Dec 2017
Posts: 24

View user's profile Send private message

PostPosted: Mon Dec 18, 2017 5:13 am     Reply with quote

But I thought that it will output 00 00 00 00 00 00 00 00 when bkbhit is false as i make input_str 0 at the end of the infinity while loop?
Billy9689



Joined: 01 Dec 2017
Posts: 24

View user's profile Send private message

PostPosted: Mon Dec 18, 2017 6:21 am     Reply with quote

Somehow I think I catch what you trying to say Ttelmah. Did you mean that the string I displayed is actually "old" string. When while (bkbhit) is false the input_str will be 00 00 00 00 00 00 00 00. So when I carried out operation it will return nothing. Is that right?

If so I have other question. I carried out the string copy function right after the while(bkbhit) finish (copy the whole input_str) and the display of input_str is carried out much later. But how come display of input_str is valid (string similar to what I inputted is displayed) and display of the copied string is not valid (only 00 00 00 00 00 00 00 00 is displayed)?

And in term of searching the header and terminator as you suggested previously, the Bluetooth module is sending and receiving data in hexadecimal format. So I should search for the header and terminator in hexadecimal form (e.g. 0x23 for #, which is my self defined header) or in ASCII form (search directly for #)?
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 18, 2017 7:50 am     Reply with quote

Well I'm confused HOW you're getting what you get ! According to the manual the BT module receives data as follows....
· Received data is output from the first byte up to the 16th byte in comma-separated format.
· For each value, output hexadecimal as ASCII character (0 ~ 9, A ~ F).
Example of received data
00, 0000, 00: 01, 02, 03, 04, 05, 06, 07, 08
00, 0000, 00: 01, 23, 45, 67, 89, AB, CD, EF
00, 0000, 00: 13, 57, 9 B, DF, 02, 46, 8 A, CE
...
The data starts after the colon (Smile WITH a comma (,) between pairs of data yet somehow your data buffer never captures the colon or any of the commas. Something isn't correct.
I'm thinking your cellphone doesn't transmit in the CSV format that this BT module does. It would be nice to see the entire 'buffer' contents.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Dec 18, 2017 9:41 am     Reply with quote

It'd all be far easier if you sat down and added a way to actually monitor what is being seen, and show this.
My thought is that even if the data is packetised, if you stop interpreting at the terminator, they remaining data is still arriving. Your ISR will receive everything that arrives. You need to actually discard (receive and throw away) everything that is not wanted.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
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