B4R Question Log problem

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all. I am trying to use B4R instead of the native Arduino Ide, hoping that it will be more comfortable to work with. Therefore I started with a very basic example, just wanting to see the Log. I am using an Arduino Mega, correctly connected etc. I have selected the board and the Serial port (by the way B4R ide finds it automatically). Besides the use of Serial3 in the program, which I was not yet able to test, and is not the topic of this post, I don't see the messages in the Log. The Log just displays "PROGRAM START" and nothing else. This problem has already been pointed out in an old thread, but with another board., and I didn't see the solution. I don't figure out what i am missing.. Any hint?
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial3 As Serial
    
    Dim Command As String="AT+CMGL=""ALL"""
End Sub

Private Sub AppStart

    Dim Answer As String
    Log("AppStart")
    Serial3.Initialize(19200)
    
    Log(Command)
    Serial3.Stream.WriteBytes(Command.GetBytes(),0,Command.Length)
    Serial3.Stream.WriteBytes(CRLF,0,2)
    Serial3.Stream.ReadBytes(Answer,0,128)
    Log(Answer)
    Serial3.Close()
End Sub
Thanks in advance.
 

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks for answer, but it seems not. Of course I could have missed something, because the structure of B4R program is different from the Arduino original programs. Not clear where to initialize. I modified the code as follows, without results:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial3 As Serial
    Public Serial1 As Serial
    Dim Command As String="AT+CMGL=""ALL"""
End Sub

Private Sub AppStart

    Dim Answer As String
    Log("AppStart")
    Serial3.Initialize(19200)
    Serial1.Initialize(9600)
    Log(Command)
    Serial1.Stream.WriteBytes("aaa",0,3)
    Serial3.Stream.WriteBytes(Command.GetBytes(),0,Command.Length)
    Serial3.Stream.WriteBytes(CRLF,0,2)
    Serial3.Stream.ReadBytes(Answer,0,128)
    Log(Answer)
    Serial3.Close()
End Sub
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
You sometimes have to put a delay after the 'Serial1.Initialize' to make it work, try up to 3000mS!

B4X:
   Dim Answer As String
    Serial3.Initialize(19200)
    Serial1.Initialize(9600)
    Delay(3000)
    Log("AppStart")
    Log(Command)
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Also the wrong baud rate can sometimes make the logs unreadable, like gibberish.
Default baud rate is 115000 if not mistaken.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Thanks. It worked. But the Delay is not necessary. My error was another: to have a Log("AppStart") before the serial1.Initialize(). You corrected also this mistake in your suggestion. Then I tried either with Delay or without, and I see that it works also without. Thanks again.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi CableGuy. Yes, by the way I also had a default 57600 in the port parameters of the ide. Therefore, with 9600 in the initialization, things may go wrong. Thanks.
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Hi,
Yes, some boards (e.g. some 8266s & D1s) need the delay (and a low baud rate) others don't.
You might also get gibberish in the start of the logs due, I'm told, to rubbish in the buffer, even though the baud rate is correct.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Seen that you have experience, I have further problems. I am trying to move to B4R from Arduino Ide, guessing it could be more comfortable. But I am getting strange behaviours.. For example, the following code enters only once the timer function (according to the log message list). probably I am missing something .. Nevertheless, if it is necessary to use empirical delays, it is a good way to troubles.. I hope to be wrong, of course. Thanks for attention ..
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region (

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial3 As Serial
    Public Serial1 As Serial
    Private Timer1 As Timer
End Sub

Sub InitModem As Boolean

    Serial3.Initialize(19200)
    Delay(1000)
    Return True
End Sub

Sub CloseModem
    Serial3.Close()
End Sub
Sub ModemListener
    
    Log("Enter")
    If InitModem Then 
      CloseModem
    End If
End Sub

Private Sub AppStart

    Serial1.Initialize(9600)
    Timer1.Initialize("ModemListener", 5000)
    Timer1.Enabled = True
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You are missing the timer's tick event!
Correction;
Your tick event should not be "ModemListener" but rather "ModemListener_tick"
 
Last edited:
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I was too sure to miss something.. but, unluckily, no... the behaviour is the same.. what is wrong now?
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial3 As Serial
    Public Serial1 As Serial
    Private Timer1 As Timer
End Sub

Sub InitModem As Boolean

    Serial3.Initialize(19200)
 
    Return True
End Sub

Sub CloseModem
    Serial3.Close()
End Sub

Sub ModemListener_tick
    
    Log("Enter")
    If InitModem Then 
      CloseModem
    End If
End Sub

Private Sub AppStart

    Serial1.Initialize(9600)
    Timer1.Initialize("ModemListener_tick", 5000)
    Timer1.Enabled = True
End Sub
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
What is interesting to observe is a new fact: I closed B4R; the program is now loaded in the Arduino; Every time that I connect on the Serial port, even with another program managing Serial port messages (I am using CoolTerm in this case), the message "Enter" is displayed. It is just like the program on Arduino is restarted at any Serial port connection.., or I don't know what else...
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Now closed CoolTerm, opened Arduino Ide, launched Serial monitor.. and, after 5 seconds, the message Enter appeared.. of course just once .. that is the problem: the timer seems not to work ..
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
When you initialize your timer:

Timer1.Initialize(EventHandler, Duration)

Where EventHandler is a string of your choice (usually the same as the control name, but can be anything) and in your case, you had chosen "ModemListener"

_Tick is the EVENT that the timer fires at the given duration.

SO, you code should be:

Timer1.Initialize("ModemListener", 5000)

AND the Tick event is :

Sub ModemListener_Tick
....
End Sub
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Giovanni,
I've never used serial ports except for logging so may be wrong but it looks like the two that you are defining are overwriting each other.
If I change the code to the following it logs OK:
B4X:
Sub InitModem As Boolean

    Serial3.Initialize(115200) 'the same as Serial1
 
    Return True
End Sub

Sub CloseModem
    Serial3.Close()
End Sub

Sub ModemListener_tick
    
    Log("Enter")
    'If InitModem Then     'disable this as it stops serial1
      'CloseModem
   ' End If
End Sub

Private Sub AppStart

    Serial1.Initialize(115200) 'testing on an esp32, won't work at 9600
    Timer1.Initialize("ModemListener_tick", 5000)
    Timer1.Enabled = True
End Sub
Changing the serial3 rate seems to change Serial1 and hence the logging stops
Enabling Init/ Close Serial3 also stops the logging. Presumably it is closing Serial1
If the Mega has two hardware serial ports then presumable you have to identify the second one somehow, or use software serial?

Cableguy,
I think in B4R, unlike B4A, you have to put the full sub name in the init, i.e.:
Timer1.Initialize("ModemListener_tick", 5000) ?
 
Upvote 0
Top