AsyncStreams receiving less bytes than expected

antreg

Member
Licensed User
Longtime User
HI,
I've a problem, or maybe a doubt, about the AsyncStreams behaviour: I have to receive an image from a camera (it's a professional industrial camera/sensor with a ARM processor inside), I can ask this camera to give me one image, sending it a 4 bytes command.
So I initially try with a standard approach, but I know that network communications are better handled in Async mode... so I pass to asyncstreams.
The problem is that I don't have any async automatism that told me when all bytes are arrived, and the only way I can use is counting the total bytes received in AStreams_NewData
So if I ask for a 640x480 image plus a header of 15 bytes (total 307215 bytes), after some (and never the same...:) ) cycles of AStreams_NewData I will be arrived at that total amount of bytes received.
This is true, I can say, 6 times on 10...but sometimes this in not true and I received less bytes than expected, and the event AStreams_NewData not fire anymore.
So my question is: it's an Android problem with network (I write a java routine directly in Eclipese... with similar results) or it's my camera that stops to send me bytes?
If someone need code I can paste it, but it's very simple, similar to AsyncStreams Tutorial.
Let me know what you think about this... Thanx and sorry for my English.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that I updated the AsyncStrems tutorial today: AsyncStreams Tutorial
It is worth going over it. AsyncStreams doesn't lose any bytes. Combining AsyncStreams with modal dialogs can cause some problems though I don't believe that this is the cause of this issue.

Are you using AsyncStreams in an activity? Is the activity running all the time?
 
Upvote 0

antreg

Member
Licensed User
Longtime User
Thanx for fast reply.
Yes, I saw the updated tutorial.
And yes the Asyncstreams is used inside an activity, but I was guessing now if could have more sense move it to a service activity... At this time I have to show the image received, and I can process it only I have all bytes and more important I need a way to call again the routine taht ask to the camera another image.
I have noticed that sometimes it takes 100 milliseconds to finish all cycles and sometimes it takes 300-500 milliseconds.
But I'm not sure if it's the camera that sleep... or android that take a coffee...
 
Upvote 0

antreg

Member
Licensed User
Longtime User
Yes, here is the code, I cutoff all code not necessary.
I know that is probabily orrible the:

B4X:
If bStop=False Then creaRichiesta

To create a new image request...
Let me know.
 

Attachments

  • testAsync.zip
    48.3 KB · Views: 208
Upvote 0

antreg

Member
Licensed User
Longtime User
The bitmap size is decided from an interface, and I can can decide different resolutions. In any case, I have a 15 bytes header, if I need info about the resolution, but I'm pretty sure that the images size is always the same.
The problem is probabily the server software inside the camera, even if with some code written in vb.net/C# on Windows I am able to make syncro call to the camera and receive exactly the bytes I know I have to take. In android/java I'm not able to make syncronous call, because after a couple of cycles the routines stop receiving bytes...:-(

About the way to convert bytes, you are right, but I need to stay between 120 millisecond of global operation (download image, convert bytes in int and process image) so for now I choose that way, but in the real application I should save images somewhere...so I will probabily choose the OutputStream way.
Thanx again, I will update this post if find any new info.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
but I need to stay between 120 millisecond of global operation (download image, convert bytes in int and process image) so for now I choose that way, but in the real application I should save images somewhere...so I will probabily choose the OutputStream way.
You can use OutputStream.InitializeToBytesArray. This will allow you to save it in memory.
 
Upvote 0
Top