B4R Question Hardware Serial on ESP32

rodmcm

Active Member
Licensed User
I am using the following (Erel Tutorial) to access the second hardware serial on an ESP 32
It will not compile
In Arduino you have to define a hardware serial as

HardwareSerial Serial2(2); // Second Hardware Port

Is there something else that needs to be done in this snippet

#if C
void SerialNative2(B4R::Object* unused) {
::Serial2.begin(115200); //<--You can change the baud rate
b4r_main::_serialnative1->wrappedStream = &::Serial2;
}
#end if
 

rodmcm

Active Member
Licensed User
Yes tried that already ( along with several variations)
Error codes

b4r_main.cpp:12: error: missing terminating ' character
Hardware Serial2 (2); '
^
b4r_main.cpp:12: error: 'Hardware' does not name a type
C:\RODSFI~1\RODSPL~1\RODTOO~1\B4A\RODSRP~1\Objects\bin\sketch\b4r_main.cpp: In function 'void SerialNative2(B4R::Object*)':
b4r_main.cpp:15: error: '::Serial2' has not been declared
::Serial2.begin(115200);
^
b4r_main.cpp:16: error: '::Serial2' has not been declared
b4r_main::_serialnative2->wrappedStream = &::Serial2;
 
Upvote 0

rodmcm

Active Member
Licensed User
Thanks Erel.. Works fine
Perhaps this fix could be added to your original tutorial on multiple hardware connections
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
I treid your snippet but it doesnt compile:
C:\Users\Stephan\OneDrive\ONEDRI~1\B4R\ESP32\Objects\bin/arduino.ar(HardwareSerial.cpp.o):(.bss.Serial2+0x0): multiple definition of `Serial2'
C:\Users\Stephan\OneDrive\ONEDRI~1\B4R\ESP32\Objects\bin\sketch\b4r_main.cpp.o:(.bss.Serial2+0x0): first defined here

What ive done wrong?
here is my very short test code:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private SerialNative2 As Stream
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    RunNative("SerialNative2", Null)
End Sub

Sub astream_NewData
    
End Sub

Sub astream_Error
    
End Sub

#if C
HardwareSerial Serial2(2); // Second Hardware Port
void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(115200);
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#End If
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

please,
received data from serial2(2) are available in ASTREAM_NEWDATA?

or

I must write:
B4X:
Sub SerialNative2_NewData (Buffer() As Byte)
    Log("NewData SerialNative(received ESP32hwserial): ", Buffer)
End Sub
EDIT: I read in forum, that I can use for this classic ASTREAM

and

Second thing, please for advice:

as wrote Erel:
HardwareSerial Serial2(2); // Second Hardware Port
What happens if you remove the HardwareSerial line?

I have same situation. With this line I can not compile project.I have error:
C:\Users\tvo\B4RWIF~1\Objects\bin\core\core.a(HardwareSerial.cpp.o):(.bss.Serial2+0x0): multiple definition of `Serial2'
C:\Users\tvo\B4RWIF~1\Objects\bin\sketch\b4r_main.cpp.o:(.bss.Serial2+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Without this line I can compile project - It does it mean,that I can use only
serial which is communicating with PC (because after upload code to ESP32
module I see that ESP32 is disconnected (in LOG module)) I am using some
default serial? 0 or 1 ?
I can not use serial 2?

p4ppc
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
A working pair - uno with astream and ESP32 with serial2, but using a looper instead of astream which does not want to load.
The UNO app is:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private softserial1 As SoftwareSerial
    Private astream As AsyncStreams
    Private tmr As Timer
    Private bc As ByteConverter

End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    softserial1.Initialize(57600,6,7)
    astream.Initialize(softserial1.Stream, "astream_newdata","astream_Error")

    tmr.Initialize("tmr_Tick", 3000)
    tmr.Enabled = True
End Sub

Sub tmr_tick
    Log("sending")
    astream.Write("abc def gh".GetBytes)
End Sub


Sub astream_newdata (Buffer() As Byte)
    Log(bc.StringFromBytes(Buffer), " ", Buffer.Length)  ' cannot log !
End Sub

Sub astream_Error
'    Log("astream_Error")
End Sub

The ESP32 app is attached. The pins used for the serial are 16 RX and 17 TX.

I would like to see astream integrated to ESP32 !
 

Attachments

  • esp32_serial.zip
    1 KB · Views: 577
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
DEREZ

THANK YOU VERY MUCH
,
everything solved your code:

B4X:
 Sub read
    Dim x As Int = SerialNative2.BytesAvailable    
    If x> 0 Then
        Log("size ", x)
        Dim buffer(x) As Byte
        SerialNative2.ReadBytes(buffer,0,x)
        Log(byteconvert .StringFromBytes(buffer))
    End If
End Sub

with your code everything is functioned
I worked with astream, it was mistake.....

THANK YOU VERY MUCH ;);););););););)
BEST REGARDS
p4ppc
 
Upvote 0
Top