B4R Question Read From RS232

hanyelmehy

Active Member
Licensed User
Longtime User
i use this code in B4J to read from rs232 device ,and it's work fine ,i need to make similar to with B4R to run on ESP32
B4X:
Sub ConnectToRS
    Try
        sp.Initialize("")
        Log(sp.ListPorts)
        sp.Open("COM7")
        sp.SetParams(9600,7,2,2)
        astream.Initialize(sp.GetInputStream,sp.GetOutputStream, "astream")
    Catch
        Log(LastException)
    End Try
End Sub
Private Sub Button2_Click
    Dim SendTxt As String
    SendTxt=Chr(7) & "01RM138" & Chr(3)
    Log(SendTxt)
    astream.Write(SendTxt.GetBytes("ASCII"))
End Sub
Sub AStream_NewData (Buffer() As Byte)
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    Log(s)
   
End Sub

i try this with no success (using Pin 16,17 on ESP32 txd2,rxd2)
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private SerialNative2 As Stream
    Private tmr As Timer
    Private bc As ByteConverter

End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    RunNative("SerialNative2", Null)
    tmr.Initialize("tmr_Tick", 1000)
    tmr.Enabled = True
    AddLooper("read")
End Sub

Sub tmr_tick
    Log("read")
    Dim TS As String
    TS=Chr(7)+"01RM138"+Chr(3)
    Dim buf() As Byte = TS.GetBytes
    SerialNative2.WriteBytes(buf,0,buf.Length)
   
End Sub
Public Sub Chr(asciiCode As Int) As String
    Dim bc1 As ByteConverter
    Dim b() As Byte = Array As Byte(asciiCode)
    Return bc1.StringFromBytes(b)
End Sub
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(bc.StringFromBytes(buffer))
    End If
End Sub


#if C
void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(9600,SERIAL_7E2);
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#End If
 

tigrot

Well-Known Member
Licensed User
Longtime User
I have an app working with 4 uart's
B4X:
Sub SerialNative2_NewData (Buffer() As Byte)
    Log("NewData SerialNative(received ESP32hwserial): ", Buffer)
End Sub
Take off looper.
Do you have a TTL to EIA converter?

Ciao
Mauro
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
I have an app working with 4 uart's
B4X:
Sub SerialNative2_NewData (Buffer() As Byte)
    Log("NewData SerialNative(received ESP32hwserial): ", Buffer)
End Sub
Take off looper.
Do you have a TTL to EIA converter?

Ciao
Mauro
i already try NewData event ,the same result
Do you have a TTL to EIA converter? i use RS232 to TTL
As far as I understand device must be polled to xmit ?yes
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Here is everything you need. Whatever you need, call me
Thank you for your help
first code now work fine
issue was
B4X:
Chr(7) & "01RM138" & Chr(3)
in b4J not like
B4X:
Chr(7)+"01RM138"+Chr(3)
in B4R
so the device was not send any thing because order not send correctly
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
Thanks for feedback. I didn't notice + for concatenation of strings, which is not present in B4R. Send separate strings. You can use SerialNative.write(array as byte(10)) to send a LF. To send chr(7) use array as byte(7).
 
Last edited:
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
Thank you for your help
first code now work fine
issue was
B4X:
Chr(7) & "01RM138" & Chr(3)
in b4J not like
B4X:
Chr(7)+"01RM138"+Chr(3)
in B4R
so the device was not send any thing because order not send correctly
B4X:
SerialNative.write(array as byte(7))
SerialNative.write("01RM138")
SerialNative.write(array as byte(3))
No need for chr functions
 
Upvote 0
Top