B4R Question astream.write2

derez

Expert
Licensed User
Longtime User
It should be like this:
B4X:
 astream.Write2(ar, 1, 1000)
But you must set ar to have data in it, after the dim declaration it is empty.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
The B4J_connectToEsp8266 reports "Invalid Input" when I try to send the Array by astream1.Write2(ar,1,10) instead of the (commented) Time message.
What's wrong?
B4X:
Sub Timer1_Tick
Dim ar(3000) As Byte
For i=0 To 1000
    ar(i)=1
Next
If server.Socket.Connected Then
astream.Write2(ar,1,10)  
'  astream.Write(ser.ConvertArrayToBytes(Array("Time here is: ", Millis)))
End If
End Sub
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The B4J_connectToEsp8266 reports "Invalid Input" when I try to send the Array by astream1.Write2(ar,1,10) instead of the (commented) Time message.
What's wrong?
B4X:
Sub Timer1_Tick
Dim ar(3000) As Byte
For i=0 To 1000
    ar(i)=1
Next
If server.Socket.Connected Then
astream.Write2(ar,1,10)
'  astream.Write(ser.ConvertArrayToBytes(Array("Time here is: ", Millis)))
End If
End Sub
If you have not changed the b4j "Sub AStream_NewData (Buffer() As Byte)"
then you have a problem by trying to read the output of the serializer, while it was not made by it.
Just check each byte from the received buffer.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Just check each byte from the received buffer.
Instead of using B4J (stopped), I connect a terminal emulator to the ESP8266-01 USB-converter com port.
However with none of the settings in the Board Selector log: (IDE, B4J serial connector, None) I get the output from the astream.write2(ar,1,10) to the terminal emulator.
All what I see is
"Connected to wireless network.
My ip: 192.168.178.20"
I also tried to set up a second asyncstream channel, but that did not work either.
How can I direct astream.write2(ar,1,10) to that com port that also is set in the board selector window?
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

not sure if it helps, but have tested on a Arduino MEGA and WeMOS D1 (ESP8266), below sample code which just uses the serial line.
[EDIT]
1) Checked the serial line output (COM4, COM5) with the B4R IDE, B4R_Serial_Connector, ... each reported AStream Bytes written but the bytes are not displayed.
2) Tested with the Arduino IDE Serial Monitor and the 10 bytes are displayed.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
    Private timer1 As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    astream.Initialize(Serial1.Stream, "astream_NewData", "astream_Error")
    timer1.Initialize("timer1_Tick", 3000)
    timer1.Enabled = True
End Sub

Sub Timer1_Tick
    Dim ar(3000) As Byte
    For i=0 To 1000
        ar(i)=1
    Next
    astream.Write2(ar,1,10)
    Log("AStream Bytes written")
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Log("AStream Received:", Buffer)
End Sub

Sub AStream_Error
  Log("AStream Error")
End Sub
 
Last edited:
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
rwblinn, thanks you, that code worked, sending ten x01 Bytes.

Now trying to understand Erel's example:
https://www.b4x.com/android/forum/threads/esp8266-getting-started.68740/
Why does it not matter which of the options in the Board selector screen is checked (checking "None" works).
Should'nt it be an example for the B4J serializator?

Now, trying to send output to a terminal emulator by opening a second astream channel:
B4X:
Private Sub AppStart
Serial1.Initialize(115200)
astream1.Initialize(Serial1.Stream, "astream_newdata", Null)
Log("AppStart")
....
If server.Socket.Connected Then
astream1.Write2(ar,1,10)   
astream.Write(ser.ConvertArrayToBytes(Array("Time here is: ", Millis)))
End If
I do not get output to a terminal emulator although I can open the com port.
 
Upvote 0
Top