B4J Question AStream_NewData (Buffer() As Byte) Fill Buffer with data

Discussion in 'B4J Questions' started by ElliotHC, Aug 7, 2019.

  1. ElliotHC

    ElliotHC Active Member Licensed User

    I have a project where I'm reading data coming in from the serial port but I don't have the hardware at home, I need to debug my project end to end including the data in routines.

    Is there a way to fill the buffer with some data? I'm going to set up some buttons on my project so that each one will fill with different data. My project will then think it has data coming in from the serial port.
     
  2. emexes

    emexes Well-Known Member Licensed User

    Just use a timer to simulate the serial data received events with randomish data. If you can vary the split point between packets, so that some of the packet is received on one event, and the rest of the packet is received on the next event, that'll be a more realistic scenario.
     
    Last edited: Aug 7, 2019
  3. ElliotHC

    ElliotHC Active Member Licensed User

    Yes, that's what I am hoping to do. How can I load bytes in to the buffer?
     
  4. ElliotHC

    ElliotHC Active Member Licensed User

  5. emexes

    emexes Well-Known Member Licensed User

    Something like:
    Code:
    Globals:
        
    Dim PretendSerialTimer as Timer
        
    Dim PretendSerialCounter As Int
        
    Dim PretendSerialBuffer As String

    Startup:
        PretendSerialTimer.Initialize(
    "PretendSerialTimer"70)    'increase period to slow down incoming data during development(/debugging ;-)
        PretendSerialTimer.Enabled = True

    Sub PretendSerialTimer_Tick

        
    'construct a simulated packet
        PretendSerialCounter = PretendSerialCounter + 1
        
    Dim SampleSerialPacket As String = "BUTTON PRESS #" & PretendSerialCounter & Chr(13'sample ASCII packet with CR end-of-line
        'Dim SampleSerialPacket As String = Chr(27) & Chr(3) & Chr(0x11) & Chr(Bit.And(PretendSerialCounter, 0xFF)) & Chr(0xFF)    'sample 5 byte packet

        
    'add simulated packet to buffer
        PretendSerialBuffer = PretendSerialBuffer & SampleSerialPacket

        
    'pass some random proportion of the buffer to the serial receive event
        Dim NumToSend As Int = Rnd(0, PretendSerialBuffer.Length)
        AStreams_NewData(PretendSerialBuffer.SubString2(
    0, NumToSend).GetBytes("UTF8"))    'binary packets need something like ISO-8859 (I think - feel free to check)

        
    'and remove it from the buffer
        PretendSerialBuffer = PretendSerialBuffer.SubString(NumToSend)

    End Sub

    'your existing serial data receive event handler (example from https://www.b4x.com/android/forum/threads/b4x-asyncstreams-tutorial.7669/ )
    Sub AStreams_NewData (Buffer() As Byte)
        
    Dim msg As String
        msg = 
    BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        
    ToastMessageShow(msg, False)
    End Sub
     
    Last edited: Aug 7, 2019
  6. emexes

    emexes Well-Known Member Licensed User

    I thought you didn't have the hardware at home, and wanted to instead fill the buffer with some data.
     
  7. emexes

    emexes Well-Known Member Licensed User

    What format are your button press packets (supposed to be) in?
     
  8. ElliotHC

    ElliotHC Active Member Licensed User

    It's a virtual Serial Port loop-back.. So I can use terminal to send data to the app. Sorted! Although it's not answered the thread, I have however achieved my objective.
     
    KMatle likes this.
  9. emexes

    emexes Well-Known Member Licensed User

    I'm so confused. A loop-back and a null-modem cable are two different things. But if you have achieved your objective, then I am happy too :)
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    You shouldn't convert bytes to string unless the bytes actually represent string.

    The correct solution should be based on B4XBytesBuilder from B4XCollections library.
     
    emexes likes this.
  11. KMatle

    KMatle Expert Licensed User

    - Good idea:

    - Use AsyncStreams via BT or WiFi to send and receive the data to your buffer (beneath of the way it communicates, the result is the same and it's easy to develop)
     
  12. emexes

    emexes Well-Known Member Licensed User

    As a general in-the-absence-of-reason-to-do-otherwise rule, I agree, but... I thought I might manage to sneak this instance past you, given that the source data was a string:
    Code:
    Dim SampleSerialPacket As String = "BUTTON PRESS #" & PretendSerialCounter & Chr(13)    'sample ASCII packet with CR end-of-line
    :)
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice