Asyncstreams receiving issue

PD5DJ

Member
Licensed User
Longtime User
Hi

I have some issues using the Asyncstreams,

Also caused by the fact im very new to this..

But neverteless I cant get my situation working..

I want to receive the following sentences from an embedded device.

Header = $
Stopper = #
Variable = 1234

$1234#

I have made a similar code for my embedded work, and that works fine..

This is what i made with Astreams_newdata.
I looks like I am missing a lot of characters sended in..

What I want is full control over all the characters 1 by 1 coming in, without missing some.

:sign0163:

B4X:
Sub AStreams_NewData (D() As Byte)
  
   Dd = BytesToString(D,0,1,"UTF-8")
   
   Dim Bytecounter As Byte
   Dim Cmaxchar As Byte :Cmaxchar = 20
   Dim Buffer As String
   Dim Dd As String
   
   If Connected Then
   
      If Bytecounter < Cmaxchar Then
   
         Bytecounter = Bytecounter + 1
      
         If Dd = "#" Then
            Buffer = ""
            Bytecounter = 0
            Label1.Text = Buffer
            
         
            Else If Dd = "$" Then
         
               Buffer= ""
               Bytecounter = 0
            Else
         
               Buffer = Buffer & Dd
                              
               If Bytecounter > 19 Then
                  Buffer = ""
                  Bytecounter = 0
               End If
            
         End If
      End If
   End If
   
End Sub

Verstuurd van mijn LT18i met Tapatalk
 

Attachments

  • Serial.zip
    8 KB · Views: 204
Last edited:

mitsusdev

Member
Licensed User
Longtime User
Hi

I have some issues using the Asyncstreams,

Also caused by the fact im very new to this..

But neverteless I cant get my situation working..

I want to receive the following sentences from an embedded device.

Header = $
Stopper = #
Variable = 1234

$1234#

I have made a similar code for my embedded work, and that works fine..

This is what i made with Astreams_newdata.
I looks like I am missing a lot of characters sended in..

What I want is full control over all the characters 1 by 1 coming in, without missing some.

:sign0163:

B4X:
Sub AStreams_NewData (D() As Byte)
  
   Dd = BytesToString(D,0,1,"UTF-8")
   
   Dim Bytecounter As Byte
   Dim Cmaxchar As Byte :Cmaxchar = 20
   Dim Buffer As String
   Dim Dd As String
   
   If Connected Then
   
      If Bytecounter < Cmaxchar Then
   
         Bytecounter = Bytecounter + 1
      
         If Dd = "#" Then
            Buffer = ""
            Bytecounter = 0
            Label1.Text = Buffer
            
         
            Else If Dd = "$" Then
         
               Buffer= ""
               Bytecounter = 0
            Else
         
               Buffer = Buffer & Dd
                              
               If Bytecounter > 19 Then
                  Buffer = ""
                  Bytecounter = 0
               End If
            
         End If
      End If
   End If
   
End Sub

Verstuurd van mijn LT18i met Tapatalk

How to inizialize AStream object? If you use AStream.InitializePrefix you're wrong because it read only 4 bytes at one time.

You must use AStream.Initialize. Is it the error?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The data is not sent one character after another (even if the other application sends it this way, the network communication is based on buffers).

The problem is in this line:
B4X:
Dd = BytesToString(D,0,1,"UTF-8")
You are losing all the other characters.
You should use:
B4X:
BytesToString(D, 0, D.Length, "UTF-8")

If you like you can use a for loop and go over each character.
 
Upvote 0

PD5DJ

Member
Licensed User
Longtime User
Hi Erel

Thanks for the reply.

I was already thinking about that solution..
But what I do not understand and can find, how the Astream principle works.
Does It have a fixed buffer?
D.Length.. I cant seem to know what .length has to do in my situation...
Because "i" expected byte for byte reception.. so length is always 1.

About the For loop, how do I implement it in this case?
Can you share me a little example based on my idea?

Many thanks :)
 
Upvote 0

PD5DJ

Member
Licensed User
Longtime User
Hi,

No the length is variable.

What I want is to catch that string.
The communication is very fast 115200 baud.

On my embedded work I do this byte per byte. So I thought I will do the same here.

Verstuurd van mijn LT18i met Tapatalk
 
Upvote 0

PD5DJ

Member
Licensed User
Longtime User
Hi again,

I have been busy starting all over again, since I have no succes to obtain a reliable and stable reception..

What i am using:

Bluetooth to serial (rs232) adatper configured at 115200baud.

I want to receive some simple strings like $1234567890#

Update interval 100mS to 1second or faster like 50mS

But from here it already goes wrong..

B4X:
Sub AStream_NewData (buffer() As Byte)

RxBuffer = BytesToString(buffer, 0, buffer.Length, "UTF8")

txtLog.Text = RxBuffer 

End Sub

As you see I am outputting the incoming data straight to a textbox..

100% of the time "$" is not shown.. only 1234567890#

And 50% of the time half of the incoming data is shown..

I need a reliable serial transmission, but in this case with Astreams it is not possible.. :sign0163:
 
Last edited:
Upvote 0

PD5DJ

Member
Licensed User
Longtime User
Hi again,

I have been busy starting all over again, since I have no succes to obtain a reliable and stable reception..

What i am using:

Bluetooth to serial (rs232) adatper configured at 115200baud.

I want to receive some simple strings like $1234567890#

But from here it already goes wrong..

B4X:
Sub AStream_NewData (buffer() As Byte)

RxBuffer = BytesToString(buffer, 0, buffer.Length, "UTF8")
txtLog.Text = RxBuffer 

End Sub

As you see I am outputting the incoming data straight to a textbox..

100% of the time "$" is not shown.. only 1234567890#

And 50% of the time half of the incoming data is shown..

I need a reliable serial transmission, but in this case with Astreams it is not possible.. :sign0163:

Complete code:
B4X:
'Activity module
Sub Process_Globals
   Dim AStream As AsyncStreams
End Sub

Sub Globals
   Dim txtInput As EditText
   Dim txtLog As EditText
   Dim btnSend As Button
   Dim RxBuffer As String
   Dim Potmeter As Int
   Dim SeekBar1 As SeekBar
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2")
   If AStream.IsInitialized = False Then
      AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStream")
      'AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
   End If
   txtLog.Width = 100%x
End Sub

Sub AStream_NewData (buffer() As Byte)

   
   RxBuffer = BytesToString(buffer, 0, buffer.Length, "UTF8")
   txtLog.Text = RxBuffer '"RXBUFFER: " & RxBuffer & CRLF & "Buffer length: " & Buffer_Length & CRLF & "Stripped: "& RxBuffer.substring2(1,(Buffer_Length - 1))
         
End Sub

Sub AStream_Error
   ToastMessageShow("Connection is broken.", True)
   btnSend.Enabled = False
   txtInput.Enabled = False
End Sub

Sub AStream_Terminated
   AStream_Error
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
      AStream.Close
   End If
End Sub

Sub txtInput_EnterPressed
   If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   'LogMessage("Me", txtInput.Text)
End Sub
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I need a reliable serial transmission, but in this case with Astreams it is not possible..
The problem is not in AsyncStreams. The problem is in your implementation.

As I explained before you should not assume that the message will arrive as a single complete message. This is how network communication works.

Use Log(RxBuffer) to check the data received.

In your code you can easily miss some of the data as it overwrites txtLog.
 
Upvote 0
Top