B4R Tutorial [tool] External Serial Connector

Discussion in 'B4R Tutorials' started by Erel, Apr 12, 2016.

Similar threads

B4R Tutorial Intel Arduino 101
B4R Tutorial Controlling an RGB Led from the PC
B4R Tutorial LCD Clock example
B4R Tutorial HC 05 Classic Bluetooth
B4R Tutorial Arduino + Raspberry Pi
  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    This is a small B4J program that uses jSerial library to connect to an Arduino board.

    To see it working start with this simple B4R program:
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private astream As AsyncStreams
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       astream.Initialize(Serial1.Stream, 
    "Astream_NewData""Astream_Error")
    End Sub

    Sub Astream_NewData (Buffer() As Byte)
       
    Log("Received: ", Buffer)
    End Sub

    Sub AStream_Error
       
    Log("error")
    End Sub
    Starting with v1.10 of the serial connector and B4J v1.00 beta 8, the IDE can control the serial connector.

    [​IMG]

    In this mode the IDE will send a message to the serial connector to close the serial port during compilation and to open it afterward.

    You can modify the code to send binary data instead of strings.

    Serial connector requires jNetwork library v1.20+: https://www.b4x.com/android/forum/threads/updates-to-internal-libaries.48274/#post-418884
    jReflection library: https://www.b4x.com/android/forum/posts/210841/
     

    Attached Files:

    Last edited: Jun 11, 2016
    Peter Simpson, Wal and Beja like this.
  2. Cableguy

    Cableguy Expert Licensed User

    With this we can use an arduino as a USB device and comunicate with a B4j app, right?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
    Cableguy likes this.
  4. Moch Firmansyah

    Moch Firmansyah Member Licensed User

    is like hyperterminal for direct serial communication, gateway only or virtual communication port as VSPD(virtual serial port driver)?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    It is similar to a hyper-terminal.
     
  6. Beja

    Beja Expert Licensed User

    Thanks Erel, but sorry not following.. I see only one B4J app in which it sends eg4g from one editbox and receives it in another editbox in the same B4J app (unless I am missing something)
    Can we have a block diagram of the system so we can see the different components?
    Thanks.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    There are two components:

    1. The B4R program that uses AsyncStreams to read and write to the serial port.
    2. The B4J desktop program that allows sending data over the serial port.

    Try it. Run the B4R program and the B4J program.
     
    Beja likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    Version 1.10 was uploaded to the first post. Starting from B4R v1.00 beta 8 the IDE can control the serial connector so it is no longer required to stop the serial connector before compilation.
     
    rwblinn likes this.
  9. jarda

    jarda Member Licensed User

    What run this
    External Serial Connector
    on B4J 4.20

    reflector - not found
    and
    udpListener.Initialize2 - not found
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    See the two links at the end of the first post.
     
  11. positrom2

    positrom2 Active Member Licensed User

    Edit: Solved:
    I had copied j... library files to the B4R libraries. Unchecking them in the library manager the program compiles.
    By the way, I have several j... lib files in the B4R folder. Must they all be removed?

    Original post:
    Error message when compiling the code from first post. I have followed the two links at the end of the first post:
    B4R version: 1.00
    Parsing code. (0.00s)
    Compiling code. Error
    Error compiling program.
    Error description: Cannot cast type: {Type=Stream,Rank=0, RemoteObject=True} to: {Type=java.io.InputStream,Rank=0, RemoteObject=True}
    Occurred on line: 10
    astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
    Word: stream
     
    Last edited: Jun 11, 2016
  12. Erel

    Erel Administrator Staff Member Licensed User

    All of B4R libraries start with r. Any other XML file should be deleted.
     
  13. jarda

    jarda Member Licensed User

    Hi
    reflector - is ok


    udpListener.Initialize2 - not found (only Initialize)

    see image
     

    Attached Files:

  14. Erel

    Erel Administrator Staff Member Licensed User

    See the instructions in the first post.
     
  15. jarda

    jarda Member Licensed User

    ??????

    this is result
     
  16. Erel

    Erel Administrator Staff Member Licensed User

  17. jarda

    jarda Member Licensed User

    Yes.. standard was 1.10... now 1.20 is ok...
     
  18. mrred128

    mrred128 Active Member Licensed User

    I removed all refs to network objects and removed jNetwork. It worked fine. Was this console made for something else initially?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    The UDPSocket allows the IDE to control the serial connector. See the first post.
     
  20. mrred128

    mrred128 Active Member Licensed User

    It wasn't mentioned why. Thanks.
     
Loading...