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 Tutorial Additional Hardware Serial Ports
  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:

    Code:
    #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
       
        Serial1.Initialize(
    115200)
        SerialStream.Initialize(Serial1.Stream, 
    "SerialStream_DataReceived""SerialStream_Error")

        
    'Start the IR receiver
        RunNative("enableIRIn"Null)
       
        Timer1.Initialize(
    "Timer1_Tick"100)
        Timer1.Enabled = 
    True
       
    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       
                SerialStream.Write(res)       
    '<------- 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
        
    Log("error")
    End Sub


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

    void enableIRIn(B4R::Object* o) {
       irrecv.enableIRIn();
    }

    B4R::Object* read(B4R::Object* o) {
       ULong result = 0;
       if (irrecv.decode(&results)) {
         irrecv.resume();
         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()

    monki
     
  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:
    Code:
    Dim B(2as Byte
    B(
    0) = Bit.LowByte(Res)
    B(
    1) = Bit.HighByte(Res)
    SerialStream.Write(B)
    '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:
    Code:
    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:

    Code:
    Dim Converter As ByteConverter
    If res > 0 Then               
         SerialStream.Write(Converter.UIntsToBytes(res))      
    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

    Code:
    example:
    SerialStream.Write(Converter.UIntsToBytes(
    array as uint(res))
    Monki
     
  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.
     
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