Author Topic: Read without using OnDataEvent  (Read 2085 times)

Offline jayburt

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Read without using OnDataEvent
« on: June 29, 2011, 09:04:21 PM »
Is there a way or can it be easily be added to have a read like a normal stream instead of using the OnData event?

You have a write function but no read function and have to use the OnData event. It would be nice to have a read function as well.

I am sending a command and waiting for a response, and I can do this either via serial port, InTheHand bluetooth library (for mobile), and this library (for pc). When waiting for a response, I read from the buffers and see if my response is there and if not I try again but eventually will timeout and then drop out of my function. I would like to do the same using this library and tried to use a StringBuilder to hold the incoming data via the OnData event but it never gets called because its on the same thread and if I use an Thread.Sleep it blocks the event from being called as well. Once my timeout has hit and I exit my function, I then get the OnData event which is obviously late.

Any ideas?

Example of what I am doing using this library that blocks the OnData Event:
Code: [Select]
// grab our starting and end points
                        end = DateTime.Now.AddSeconds(QuadCorderStreamProperties.Timeout);

                        // loop till we get our correct answer or our timeout is hit
                        while (DateTime.Now < end)
                        {
                            System.Threading.Thread.Sleep(0);
                            lock (DataBufferLockObject)
                            {
                                if (DataBuffer.Length > 0)
                                {
                                    // compare to our response we want
                                    int idx = DataBuffer.ToString().IndexOf(response);
                                    if (idx != -1)
                                    {
                                        DataBuffer.Remove(0, idx + response.Length);
                                        retVal = true;
                                        break;
                                    }
                                }
                            }

Now what I would like to be able to do and do in the other libraries I use:
Code: [Select]
System.Threading.Thread.Sleep(10);
                            if (stream.DataAvailable)
                            {
                                // try and read a byte
                                int read = stream.Read(buffer, 0, buffer.Length);
                                string decoded = System.Text.Encoding.ASCII.GetString(buffer, 0, read);

                                // compare to our response we want
                                if (decoded.IndexOf(response) != -1)
                                {
                                    retVal = true;
                                    break;
                                }
                            }

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Read without using OnDataEvent
« Reply #1 on: June 29, 2011, 09:34:25 PM »
Hi,

Well, in fact we were thinking about adding something like "wclSyncClient" which will provide synchronouse read operations. But actualy your task can be implemented in much easy and better way (at my point of view as I don't know what you need exactly).

Its easy to add internal buffer and then add data from each OnData event into this internal buffer. Then each OnData event (after data has been added) you can check this internal buffer for the response string and do what you need with data. The only problem, in your case, is timeout. However it can be implemented too with timer.

Of course, if you need synchronouse reading we can add it but it takes time and may cost some money (not too much) as additional custom functions.

Offline jayburt

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Read without using OnDataEvent
« Reply #2 on: June 30, 2011, 02:25:27 PM »
This is exactly what we do now which is the first code block in my original post, where DataBuffer is an internal buffer that is filled on the event OnData. The only problem I have is, using the first code sample block, while in that while loop that tests against a timeout and checks the internal buffer for our response code the OnData event will never fire. It will only fire before or after the while loop. I was hoping to do like a spin and wait type of thing either til the internal buffer got our response code or we timed out. I tried putting a Thread.Sleep in there but it looks as they are on the same thread so the OnData event is blocked either while looping or sleeping.

Any ideas?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Read without using OnDataEvent
« Reply #3 on: June 30, 2011, 02:44:08 PM »
Why are you doing it synchronouse? Do it asynchronouse. Start timer for timeout. And check for response in each OnData event. If it (response) is there then stop timer. If it is still not there - then timeout fires.

 

Sitemap 1 2 3 4 5 6 7 8 9