mnew
Posts: 4
Joined: Tue Oct 14, 2008 8:30 pm
Location: USA

connect service, Bond, pnicode

Tue Oct 14, 2008 8:37 pm

Hi, Got two questions for you:

1. After the device is discoved and services are found, I do

m_Service = (RemoteService)services[0];
m_Stream = m_Service.Stream;

and it throws an exception of failure to connect,
why? Can it retry without throwing an exception?

2. How to use RemoteDevice.Bond? Where to locate the pincode?
No examples show Bond, Unbond, multi-threading?

Thank you,


MY

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

RE: connect service, Bond, pnicode

Tue Oct 14, 2008 9:28 pm

1. You catch the exception and then you can try again. The why can be lots of things, perhaps you are not bonded, perhaps the device is not present (turned off/out of range). Depends on the exception too :)

2. The pincode is either known (in the manual of the device, or arbitrary (your choice). Depends on the situation.
There are no samples of Bond/Unbond since it's simply Bond("1234") to bond with a pincode of 1234. Unbond is even simpler: Unbond().
There are no pure multi-threading samples, that's correct.

Regards,
Jonas

mnew
Posts: 4
Joined: Tue Oct 14, 2008 8:30 pm
Location: USA

RE: connect service, Bond, pnicode

Wed Oct 15, 2008 12:18 am

About Stream and ServiceStream, with reference to your FindAndConnect example, I have in my application the following code:

Stream m_Stream = null;
remote_device.DiscoverServices(ServiceType.SerialPort);
Service[] services = remote_device.Services;

if (services.Length > 0)
{
m_Service = (RemoteService)services[0];
m_Stream = m_Service.Stream;
}

When I do "Add Watch" here on m_Stream, I see "ReadTimeout" and "WriteTimeout" throw an exception of type 'System.InvalidOperationException'

Questions:
1. I know your ServiceStream does not support "ReadTimeout" and "WriteTimeout", so how I get rid of these two exceptions?

2. After m_Stream got connected with the above code,
I do read and write with the following code:

m_Stream.Write(bytCommand, 0, 1);
m_ReceivedDataBytes = m_Stream.Read(buffer, 0, buffer.Length);

The program is responding after m_Stream.Read, and I have no way to find out where "it" is.
One time I caught 'System.InvalidOperationException', another time I got "Stream not open" and all the other time nothing, the program just hung.
What's the problem?







MY

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

RE: connect service, Bond, pnicode

Wed Oct 15, 2008 12:24 am

1. You shouldn't get any exception like that. I guess it happens because you're casting the ServiceStream to a Stream or something. Try making m_Stream a real ServiceStream, but I really don't know if that will help.

2. I have no idea. I've never heard of this issue before. Stream not open indicates that the connection was dropped between your application and the remote device.

Regards,
Jonas

mnew
Posts: 4
Joined: Tue Oct 14, 2008 8:30 pm
Location: USA

RE: connect service, Bond, pnicode

Wed Oct 15, 2008 12:36 am

For your reply:

1. I didn't do any casting.
You FindAndConnect example declared
private Stream currentStream = null;

and then set
currentStream = currentService.Stream;

If you do a "Add Watch" you should see the exceptions.

2. So, where does the application execution go after m_Stream.Read?
How do I handle the problem?



MY

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

RE: connect service, Bond, pnicode

Wed Oct 15, 2008 1:01 am

You have declared it as Stream m_Stream so you are doing an explicit cast to a Stream instead of a ServiceStream. Though ServiceStream is inheriting from Stream, so maybe this doesn't matter.

What version of Visual Studio are you using? 2005 or 2008?


The program is responding after m_Stream.Read, and I have no way to find out where "it" is.

You mean it's NOT responding after m_Stream.Read? Because otherwise you just step to the next line in the code of course. On the other hand, if it's not responding you are stuck on the Read(..) of course. Maybe there is nothing to read in the stream? Then Read(...) would block until it has read as many bytes as you have specified (m_buffer.Length)

Regards,
Jonas

Return to “BlueTools”