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 2010Posts: 13674

Posted: Sun Dec 17, 2017 1:09 am

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 2010Posts: 6290Location: Greensville,Ontario

Posted: Sun Dec 17, 2017 7:51 am

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

Billy9689

Joined: 01 Dec 2017Posts: 24

Posted: Mon Dec 18, 2017 2:49 am

Hi guys I come back with more question

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:

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

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 2010Posts: 13674

Posted: Mon Dec 18, 2017 3:18 am

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 2017Posts: 24

Posted: Mon Dec 18, 2017 5:13 am

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 2017Posts: 24

Posted: Mon Dec 18, 2017 6:21 am

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 2010Posts: 6290Location: Greensville,Ontario

Posted: Mon Dec 18, 2017 7:50 am

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 ( 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 2010Posts: 13674

Posted: Mon Dec 18, 2017 9:41 am

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.