B4R Question Send serial data via USB - string "HALLO" - from B4R and receive � in B4J

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends, please for help,

I am sending simply serial data from B4R (esp) to B4J

1) In B4R:

This is settings:in App start
B4X:
Serial1.Initialize(115200)
astream.Initialize(Serial1.Stream, "astream_newdata", "astream_Error")
In Timer
B4X:
' log("HALLO1")
astream.Write("HALLO2".GetBytes)


2) In B4J:
B4X:
Sub btnopenport
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
End sub
Sub astream_NewData (Buffer() As Byte)
    Dim s As String 
    s = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(s)
End sub

very magical situation for me is - when I upload B4R program to ESP then after this upload I see "Hallo1" and "Hallo2" in B4J, I get right result
but then
I disconnect ESP from USB and then I connect ESP again I get only bad result in B4J - �

In Logs in B4R I see each time everything right = HALLO. Only in B4J I can not get good result.

I have read this, but this threads not solved my situation:
https://www.b4x.com/android/forum/t...-using-asyncstreams-b4r-b4j-and-vb-net.81637/
https://www.b4x.com/android/forum/threads/cannot-receive-serial-data-from-b4r-to-net-program.107386/

Why please I get �? Where I am doing mistake, please?
p4ppc
 
Last edited:

petr4ppc

Well-Known Member
Licensed User
Longtime User
Rbghongade

thank you very much for your advice, it helped me. I dont know why I did this basic mistake - why I forgot to set baudrate. I shame. Its my basic school mistake..

Please do you have experience if is better to send some simple text via USB serial port with LOG("HALLO") or is better to send (from b4r-esp to B4J) simple text with: astream.Write("HALLO2".GetBytes)? Or is this the same?

Thank you very much,
Best regards
p4ppc
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Hi, it is essentially the same.
I know this is an old thread but....never to old to learn something new! Using a joystick and the below B4R and Processing code I can control the "arrows" in the Processing UI by using "Log" statements to send "commands" from the B4R project and Processing then updates the direction of the arrow according to the direction that the joystick (connected to an UNO) is moved. Very interesting indeed.

B4R code:

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 Serial1 As Serial
    
    Dim x, y As Int = 0
    Dim a0, a1 As Pin
    
    Dim TOPMARGIN As Int = 700
    Dim BOTTOMMARGIN As Int = 200
    
End Sub

Private Sub AppStart
    Serial1.Initialize(19200)
    Log("AppStart")
    a0.Initialize(a0.A0, a0.MODE_INPUT)
    a1.Initialize(a1.A1, a1.MODE_INPUT)

    AddLooper("myLooper")
    
End Sub

Sub myLooper
    
    x = a0.AnalogRead
    y = a1.AnalogRead
    
    Delay(100)
        
    If(x > TOPMARGIN And y > TOPMARGIN) Then
        If(x>y) Then
            Log("left")
        Else
            Log("down")
        End If
    else if(x > TOPMARGIN And y < TOPMARGIN) Then
        If(x>y And y>125) Then
            Log("left")
        Else
            Log("up")
        End If
    else if(x < BOTTOMMARGIN And y < BOTTOMMARGIN) Then
        If(x<y And y>125) Then
            Log("right")
        Else
            Log("up")
        End If
    else if(x < BOTTOMMARGIN And y < BOTTOMMARGIN) Then
        If(x>y And x>125) Then
            Log("down")
        Else
            Log("right")
        End If
    else if(x>TOPMARGIN) Then
        Log("left")
    else if(x<BOTTOMMARGIN) Then
        Log("right")
    else if(y>TOPMARGIN) Then
        Log("down")
    else if(y<BOTTOMMARGIN) Then
        Log("up")
    Else
        Log("stop")
    End If 
    

End Sub

Processing code:
B4X:
import processing.serial.*;
Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port

// I know that the first port in the serial list on my mac
// is Serial.list()[1].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
// Execute the project and if don't work in the console you can see the array of the Serial ports and identify the number in the array and choose the correct.

void setup(){
  //Change here the number of the port
  String portName = "COM46";
 
  printArray(Serial.list());
  size(500,500);
  println(portName);
  myPort = new Serial(this, portName, 19200);
  println(portName);
}
 
void draw()
{
  if ( myPort.available() > 0)
  {  // If data is available,
  val = myPort.readStringUntil('\n');
 
    if(val!=null && val.contains("left")){
      PImage img;
      img = loadImage("images/left.png");
      background(img);
      println("left");
    }else if (val!=null && val.contains("right")){
      println("right");
      PImage img;
      img = loadImage("images/right.png");
      background(img);
    }else if (val!=null && val.contains("up")){
      println("up");
      PImage img;
      img = loadImage("images/up.png");
      background(img);
    }else if (val!=null && val.contains("down")){
      PImage img;
      img = loadImage("images/down.png");
      background(img);
      println("down");
    }
    
  }
}

1586792041139.png
 
Upvote 0
Top