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
i try this with no success (using Pin 16,17 on ESP32 txd2,rxd2)
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