ced@ottotecnica.com
Posts: 9
Joined: Wed Oct 08, 2008 5:35 pm
Location: Italy
Contact: Website

Read 4 char at a time

Wed Oct 08, 2008 6:10 pm

Hallo
we are evaluating BlueTools for inclusion in a Windows Mobile 5 VB.NET application. The PDA has Microsoft bluetooth stack.
The application connects to an RS232-BT adapter, which in turn is conncted via rs-232 to an embedded microprocessor's serial port.
When the application sends a command to the microprocessor via BT, it responds with a string.
The application works perfectly by using the VB.NET SerialPort class.
We'd like to switch to bluetools because of the ability to enumerate BT devices and their services.
Now to the point: when we send a command to the microprocessor using the BlueTools APIs, only the first 2 to 4 characters of the response string are displayed. If we send another command or even an empty string, the following 4 characters are displayed, then with another command or empty string another 4 chars come up, etc., until the end of the string.
At that point, no further characters are displayed because the commands are not sent anymore. We are sure about this because some commands have visible effects (i.e. turn on leds, motors, ecc.), and nothing happens.

Version information:
Manager.BlueToolsVersion.ToString = 1.2.1.848
Windows obile 5.0 with Featur Pack, Operating system 5.1.476 (Build 15704.3.5.0)
Bluetools module versions: all report 1.7.1.9804

Thanks in advance for your help.

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Read 4 char at a time

Wed Oct 08, 2008 7:50 pm

Hard to say without seeing your code, but usually these kinds of problems stems from usage of BeginWrite/BeginRead. You are not guaranteed that all the data in the buffer is written/read before you enter the callback routine. Inside of it you are supposed to check if there are more data to be written/read and make an additional call to BeginWrite/BeginRead to complete the operation before calling EndWrite/EndRead.

Regards,
Jonas

ced@ottotecnica.com
Posts: 9
Joined: Wed Oct 08, 2008 5:35 pm
Location: Italy
Contact: Website

RE: Read 4 char at a time

Wed Oct 08, 2008 8:14 pm

Originally posted by Jonas

Hard to say without seeing your code, but usually these kinds of problems stems from usage of BeginWrite/BeginRead. You are not guaranteed that all the data in the buffer is written/read before you enter the callback routine. Inside of it you are supposed to check if there are more data to be written/read and make an additional call to BeginWrite/BeginRead to complete the operation before calling EndWrite/EndRead.

Regards,
Jonas


The code is copied from the Find and Connect sample.
Inside the ReadCallback function, EndRead is called, the buffer appended in a textbox and then a new BeginRead is fired.

We have added a "send" button which basically fires up a BeginWrite. In the Write callback function an EndWrite is called and no further BeginWrite is called.

I can post the code if needed.

Maybe I should add that every command sent from the PDA and every response from the microcontroller are terminated with vbCrLf...

Thank you.

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Read 4 char at a time

Wed Oct 08, 2008 8:39 pm

Try using Write instead of BeginWrite to determine that this is in fact the problem.

Regards,
Jonas

ced@ottotecnica.com
Posts: 9
Joined: Wed Oct 08, 2008 5:35 pm
Location: Italy
Contact: Website

RE: Read 4 char at a time

Wed Oct 08, 2008 8:54 pm

Originally posted by Jonas

Try using Write instead of BeginWrite to determine that this is in fact the problem.

Regards,
Jonas


No, using Write instead of BeginWrite/EndWrite doesn't change the situation.

What makes a difference is opening the "old" application, which connects to the bluetooth adapter via a virtual COM port, close it (thus ending the bluetooth connection), then launch the BlueTools sample app. Sending a command with this sample app now works again (i.e. with the 4 chars problem).
In other words, opening and closing the bluetooth connection via the virtual com port seems to reset something under the hood.
Maybe there's an operating system buffer involved that's not properly cleared ?

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Read 4 char at a time

Wed Oct 08, 2008 11:05 pm

I think I am misunderstanding something.

You are writing four bytes with Write. Are those bytes reaching the destination?
How are you reading the bytes off the Stream at the other end?
You are getting a response or something? Have you made sure you reading the entire response in that case? Try using Read here too, just to be sure you are not leaving any bytes in the Stream.

Regards,
Jonas

ced@ottotecnica.com
Posts: 9
Joined: Wed Oct 08, 2008 5:35 pm
Location: Italy
Contact: Website

RE: Read 4 char at a time

Fri Oct 10, 2008 1:03 pm

Originally posted by Jonas

I think I am misunderstanding something.

You are writing four bytes with Write. Are those bytes reaching the destination?
How are you reading the bytes off the Stream at the other end?
You are getting a response or something? Have you made sure you reading the entire response in that case? Try using Read here too, just to be sure you are not leaving any bytes in the Stream.

Regards,
Jonas


1) I write some bytes with Write.
2) Those bytes reach the other end.
3) The other end sends me a response, which is a string of about 15 bytes.
I'm sure about this because I wrote the firmware on the other end :-) and also because with hyperterminal and a bluetooth dongle on the PC it works.
4) On the txtData text field only the first 4 chars of the response appear.
5) I write some other bytes with Write.
6) Those bytes never reach the other end, but on the text field txtData other 4 chars of the previous response appear.
7) I call Write again.
8) The bytes don't reach the other end, but other 4 chars of the previous response appear.
9) I call Write until all of the previous response has appeared, but then nothing more arrives, because the other end is not seeing any more commands, so it doesn't send me anything.

10) I close the application
11) I try to deploy the application again, but Visual Studio tells me "device connectivity error". This is the kind of error you get when you try to deploy an app that is still running on the PDA.
12) I wait a minute or two
13) The deploy is successfull.

Based on these facts, I belive that a thread created by the library becomes "stuck" after the first write. This thread then exits after some sort of timeout.
Of course, I might be very wrong, since I can't see the source code :-)

All of this applies to the version that uses Write and BeginRead/EndRead. I've also written a version that uses a Read right after the Write, but the results are exactly the same.

I can post the entire code if you want.

Thank you.

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Read 4 char at a time

Fri Oct 10, 2008 8:46 pm

Please upload your code somewhere and give me a link so I can look at it.

Regards,
Jonas

ced@ottotecnica.com
Posts: 9
Joined: Wed Oct 08, 2008 5:35 pm
Location: Italy
Contact: Website

RE: Read 4 char at a time

Thu Oct 23, 2008 11:29 am

After extensive tests we found that there was a race condition in microcontroller firmware, which caused it to actually send 4 chars at a time.

I think the bug never showed itself before because the timing of the virtual com port layer were different than those of the bluetools library.

Now it seems the bug is resolved, so we're about to buy a license!

Thanks for your time.

Return to “BlueTools”