B4R Question Write Uint value to Serial Port

Discussion in 'B4R Questions' started by Jakes72, Jul 5, 2019.

Similar threads

B4R Tutorial HC 05 Classic Bluetooth
B4R Tutorial Arduino + Raspberry Pi
B4R Tutorial Using PROGMEM to save memory
B4R Tutorial Inline C / C++
B4R Code Snippet Base64 decoding
  1. Jakes72

    Jakes72 Member Licensed User

    Hi Guys,

    I am trying to write a Uint value (Read from a IR Receiver) to the Serial port so I can monitor it, but I do not know how to convert the returned Uint value into a Byte Array that the Asyncstream function 'Write' requires. Also can I use a timer or must I use a AddLooper to poll the IR? Please can someone help? My code is below:

    #Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
    #End Region

    Sub Process_Globals

    Public Serial1 As Serial
    Private SerialStream As AsyncStreams
    Private Timer1 As Timer
    End Sub

    Private Sub AppStart

    'Start the IR receiver
        Timer1.Enabled = 
    End Sub

    Sub Timer1_Tick
    'Poll the IR Receiver...
            Dim res As UInt = RunNative("read"Null)
    'If a IR Signal was received....
            If res > 0 Then       
    '<------- Don't know what to do here.
            End If
    End Sub

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

    Sub SerialStream_Error
    End Sub

    #if C
    #include "IRremote.h"
    //Signal pin
    IRrecv irrecv(11); 
    decode_results results;
    B4R::Object res;

    void enableIRIn(B4R::Object* o) {

    B4R::Object* read(B4R::Object* o) {
       ULong result = 0;
       if (irrecv.decode(&results)) {
         b4r_main::_lastread = millis();
         result = results.value;
       return res.wrapNumber(result);
    #end if
  2. monki

    monki Active Member Licensed User

    You can Use Byteconverter for this
    Ex. UIntsToBytes (UInts() As UInt) As Byte()

  3. emexes

    emexes Well-Known Member Licensed User

    Or since Uint in B4R is 16 bits rather than 32, you could use:

    Dim I As Uint = 0x1234

    Bit.LowByte(I) should return 0x34
    Bit.HighByte(I) should return 0x12
  4. emexes

    emexes Well-Known Member Licensed User

    Sorry, should have used your code outline...

    Slow but clear way:
    Dim B(2as Byte
    0) = Bit.LowByte(Res)
    1) = Bit.HighByte(Res)
    'but now we have have two bytes wasted/unused for the remainder of this Sub
    Concise but less-clear way, constructing the temporary byte array within the Write call:
    SerialStream.Write(Array As Byte(Bit.LowByte(Res), Bit.HighByte(Res)))
    'the temporary two-byte array should only exist for the duration of the call
  5. Jakes72

    Jakes72 Member Licensed User

    Thanks for the reply. I tried to do this as you suggested:

    Dim Converter As ByteConverter
    If res > 0 Then               
    End If
    But I get a 'Cannot cast type' error (see screenshot). Please advise?

    Attached Files:

  6. Jakes72

    Jakes72 Member Licensed User

    Awesome thank you very much emexes, I will give this a go!
  7. emexes

    emexes Well-Known Member Licensed User

    One thing to think about is: what happens if a byte goes missing or gets corrupted on its travel over the serial line to the receiving computer? Next thing you know, you'll be receiving the low byte but think it's the high byte, and vice versa.
    Last edited: Jul 5, 2019
  8. monki

    monki Active Member Licensed User

    The parameter for UintstoByte is a array of uints

    array as uint(res))
  9. Jakes72

    Jakes72 Member Licensed User

    Thank you so much your answers have been extremely helpful. Some of it a bit over my head, but I am learning! Thank you again!
  10. emexes

    emexes Well-Known Member Licensed User

    Yeah, I was thinking later that perhaps I might have run a bit far with that ball, should have just stopped after the heads-up about the possibility of the serial sender and receiver getting out of sync.

    If yours is a short-term project, you might not even encounter the problem. And if you do, well, apparently experience is an excellent teacher :)

    I have tidied up my posts to remove the distraction.
  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