Android Question How to use AsyncStreamsText

Laser Tamer

Member
Licensed User
Longtime User
Hello All, First post so please be kind. I used Basic4PPC from Erel a long time ago (only ever used BASIC that once). When I looked for an Android compiler and saw "Erel" on B4A, I knew things would be nice. So, I have been looking at the Chat example for serial comms over Bluetooth. Although Chat works nicely when unchanged, I am wanting to modify this to see if I can send strings from a PC. I have written the PC code, so have full control over how this can be sent. I really have two questions here.

I am using v10.7 of B4A.

1. The IDE complains when I try to change "Private AStream As AsyncStreams" to "Private AStream As AstncstreamsText" saying this is an unknown type, "are you missing a library reference". I also tried to declare this in the Main section but had the same error.

2. If I don't try the Text version, I am unsure of the order of the first 4 bytes when using Prefix mode. I know Big and Little Endian, just not sure when B4A reads these in, does it call the first byte the most significant or the least? For example, if I have 6 bytes being sent, do I send 0,0,0,6 in Big Endian or Little Endian?

At the moment, I am not getting bytes across, but as the tutorials rightly state, there is no guarantee of the whole string being received before the code wants to place whatever has just arrived on a label for example.

I have a terminal program on the Android phone (Samsung Xcover4 with Android v7) to make sure the Bluetooth comms is actually working and that is very happy. I can send lots of text very easily, so the basic system appears to be working OK, as does the Chat program.

My lifelong code choice has been Pascal for 40 years and counting, so the BASIC language is very new to me and also the structure of the Activity/Main pages etc, so just looking for a push in the right direction.

Thank you to everyone in advance and especially Erel for such a nice product once again.

Dr Alec Butterworth (UK)
 

udg

Expert
Licensed User
Longtime User
Welcome!
Have you read this thread?

ps: you're not alone..quite a few of us come from the Pascal language. BTW, B4X is a language on its own. B doesn't stand for Basic (personally I try to make people believe it stands fro Best)
 
Last edited:
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
Hello udg, thank you for the welcome, and so fast!!

I have used many languages over the years, all scientific including HPL from Hewlett Packard and Erel's excellent Basic4PPC, which is why I am persevering with this for the Android phone. I trust Erel!!

Yes, I saw that thread. The first 4 lines show a declaration for AsyncStreamsText at line 2 and that line fails in my compiler. It is as though the type "AsyncStreamsText" simply does not exist. Very strange. What I have not tried, and I will, is to run the example code in that thread and see if anything magically jumps into life. I tried looking for additional libraries but nothing obvious showed itself. The Serial library is already in the project of course, so I am a little bemused at the moment.

In addition to other trials, I have not had success with the Prefix mode. No matter what I send from the PC, with or without the prefixed 4 bytes (big endian / little endian) or no prefix at all, the result in B4A is the exact same disjointed strings.

Since the characters are being transferred without corruption, ie the whole lot does get to the device eventually, I may change the code to look for a chr(10) and then fall out of the routine, but that defeats the object of using the Text version of AsyncStreams.

There is also a slow down of the Android after a few hundred bytes have been sent. I am putting this down to the fact my coding is not yet correct, so that is a secondary problem that may vanish once I am getting better at this coding.

Pascal is my life long love of code. I tried C (eventually C++) but found the syntax diffuclt to remember and thus follow some months later... Pascal can be written as almost plain english, much nicer when coding is only a small fraction of the job description!
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I didn't check the attached file in that thread, but I guess it is the AsyncStreamText class. In that case, just add it to your project.
You may find useful this thread too.

Pascal, my first love. I began in 1984 with MS Pascal (editor,compiler and linker) but soon I luckily found TurboPascal and the world wasn't the same anymore!

ps: since you're new to the B4X world, my advice is to set aside some time to watch Erel's video tutorials and read Klaus' guides. You may find all of it in the Learn section on top of the page on this website. It will take you almost no time to master most of it.
 
Last edited:
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
Hello udg, My first Pascal compiler was ProPascal in DOS 2.1 (with floppy disc functionality!) which pre-dated Turbo by several years. They were different implementations of the language but ProPascal adhered to the standard whereas Turbo didn't. Sadly Prospero stopped the compiler after a fairly weak 32 bit version for early windows, so I had to jump ship to Delphi. That had good and bad versions over the years, but is still better than the free pascal compiler with Lazarus that I looked at and thought "no thanks".

Ah, now, yes, the class. Maybe there is a missing piece of the jigsaw that I am overlooking.

Yes, I saw the video link but the video didn't play on my PC. I have other flavours of PC, so will give that another go. The text in that link I thought would be sufficient, but I do seem to have backed myself into a corner. Revisit and Retry!!!

Thank you again for your patience.
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
follow this link
and include the attachment from the first post in your project file.

Video tutorials are also on YouTube:
 
Last edited:
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
Hello thetahsk. Thank you taking the time to try and help me out.

Yes, I saw the tutorial (and another about the AsyncStreams specifically) but none of them mention the use of AsyncStreamsText.

As I alluded to earlier in the posting, the line in the example "Private ast As AsyncStreamsText" causes an error in the IDE so will not compile. It claims that "AsyncStreamsText" does not exist. I am looking at any way to get this class included in the Chat example at the moment. I do not know yet which library that will reside in. Once that is done, I suspect all my problems will evaporate.

It may be worth adding that I am using Bluetooth for this transfer of characters and not a network. Where I intend using this code, when it works, forbids me to connect to any local WiFi arrangement. Thus I must be as self sufficient as possible between my own PC hardware and the Android phone.

I have taken the Chat example from Erel as my base, as that is already very close to what I need. I would like to change the transfer of characters to the Text version of AsyncStreams so that I can use the chr(10) or chr(13) as the natural terminator. I have tried using the Prefix mode, but that does not seem to change anything at all, so I am missing something fairly fundamental, just cannot pick it out.

I have full control of how to send characters at the PC end of things as that is also my own code. That side appears to work OK as I have a terminal program on the Android phone that show the comms to work, so, I am trying to straighten out the B4A code.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
I am looking at any way to get this class included in the Chat example at the moment.
The code from the thread is a class. You add it to your program (Chat example) using Project/Add existing modules.
 
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
Thank you udg. I added the code in "AsyncStreamsText.bas" as a module into Chat and now I can change the AsyncStreams to AsyncStreamsText without errors.

So, having solved one problem, I now create a few more. The initialisation is now different, so I am looking at how to remove what is not needed in Chat and use the new routines that have been added inside the AsyncStreamsText.bas code.

There is a declaration of Astreams in the AsyncStreamsText code instead of the Astream that is in the original Chat code. I assume I need to use the new variables in AsyncStreamsText instead of those in the original Chat page.

For the moment, I appear to have taken one step forward then two steps back, but I think I only need to get the initialisation right with the correct Astream and all should fall into place. Sounds easy!

Not having an intimate knowledge of the B4A language, structure and IDE is not really helping me!! For the moment, this is quite an alien landscape.

I am determined though, so hope you don't mind my simple questions. You have been most helpful in pointing me in various directions.

I have been looking through the help pages but have not come across a language definition for every function (Klaus has written some good guides). For example, the initialisation (Targetmodule as Object) is not explained as fully as I need.

I will keep trying though....it cannot ignore me forever!
 
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
Hello Erel. Very nice to hear from you and do hope you have been well all these years. As you can see, serial comms again, just as in the basic4ppc days!!

OK, to recap, I have written the PC code at the sending end of things, so can change that around to send whatever characters are needed. All I need at the moment is the Android to receive a string and place it on a label. Simple, no??

I would prefer AsyncStreamsText simply because that looked simple and a single LF or CR could be used to terminate the string at the PC sending end as well as the Android receiving end. This is also the way all machine tool controllers work so I am familiar with this method of transmission.

I tried modifying Chat leaving it in prefix mode, but whatever I did with a prefix set of 4 bytes, nothing changed. The "messages" were still split up when I am trying to place the whole string on a label for display on the Android phone.

In Prefix mode, I understand that the first 4 bytes must be an equivalent 32 bit integer. So, as an example, I would send chr(0), chr(0), chr(0), chr(6) before the 6 bytes/characters of the message. No matter how I do this, or even omit the first 4 bytes, nothing changes at the Android end, the message is always split up.

So, I tried once again to get the AsyncStreamsText working and found that if I added the sample AsycStreamsText.bas code into another tab in the IDE, the class was then added to the project and I could then declare the AsyncStreamsText type instead of the AsnycStreams type. Yippee, I thought. However, I am now trying to work out how to get at the byte buffer that this newly added AsyncStreamsText offers, as now, I do not receive anything. I think I am looking at the original Astream instead of this new one (Astreams). I am also struggling with the initialisation line especially the first parameter which is some sort of object. The rest of the initialise function is OK. I would eventually like this to be a send or recieve stream, so have tried to initialise both as you did in Chat.

I need to learn a lot about the structure of the pages/activities etc as well as the syntax of the various functions/subs that are on these pages. It will all fall into place, but sadly, I do not have lots of time to devote to this. After tomorrow, I will be busy once again and will be forced to leave this alone for quite a while.

I thought it may be easy to get the Chat program tweaked to suit what I am doing, but that has not really happened just yet. I will persevere though and once working, a donation to your cause will follow, just as I did with basic4ppc. That worked really well, so I have high hopes with B4A as well.

I have been looking through all the booklets but nothing in there mentions AsyncStreamsText. The Serial reference only lists the basics and although AsyncStreamsText is mentioned, it is not shown how to implement it. Other projects use a network as an example but I cannot use a network where I will be, so I am tied to using Bluetooth.

Thank you for taking the time to look through this post and hope you have some golden pointers for me to follow!
 
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
[Update] - I have abandoned AsyncStreamsText and concentrated on getting the Prefix mode working.

Almost there....however....

In the Chat example, the receive stream stalls randomly. The characters arrive perfectly for a few seconds, then the receiving stops with no error reported. If I disconnect the Bluetooth but do not stop the Chat app, when I re-connect, the receive starts again, but then stalls just as before. Very curious. I am using only 9600 baud at the PC sending end of things. I even put a delay in between bursts of transmissions but nothing seems to prevent this stalling. I believe this is the receive part of the program and not something to do with the update of the text boxes on the display part as both the label and the textbox stop at the same time. Am I over-running a buffer here somewhere?

Any help gratefully received!
 
Upvote 0

Laser Tamer

Member
Licensed User
Longtime User
[SOLVED] - In trying to cure the stalling, I managed to cause a disconnect (as others have found) every time I sent a group of strings. This is from a test program of mine on a remote PC using Delphi Pascal and a COM port driver from Winsoft (Erik Salej). This COM port driver is very reliable and has never caused problems. However, this may also apply to other "Connection is broken" errors. Initially, my code was closing the PC port after I sent a number of strings and I thought that was the reason why I had the "connection is broken" error. Not quite the case. I was also using a routine called PurgeOutput in the Delphi code to make sure all characters have actually been sent from the local buffer. That turned out to be the main culprit.

It is my belief that this was somehow upsetting AsyncStreams or at least telling it everything has been sent in such a way that AsyncStreams decides to also close itself down. Once I removed the PurgeOutput rountine, the connection was stable and I could send batches of strings without further broken connections.

I have seen others here with this same problem in other situations and different hardware, so my experience would suggest, inspect your remote code very, very carefully, and make sure no unneccessary commands are being directed at the remote COM port as you may well be surprised, just as I was, that they could have an effect at the Android end. Once again, Erel has produced some very nice and stable code for serial comms, I just needed to feed it the correct strings!
 
Upvote 0
Top