B4R Question Write Uint value to Serial Port

Jakes72

Active Member
Licensed User
Longtime 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:

B4X:
#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
 

emexes

Expert
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
 
Upvote 0

emexes

Expert
Licensed User
Sorry, should have used your code outline...

Slow but clear way:
B4X:
Dim B(2) as 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:
B4X:
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
 
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
You can Use Byteconverter for this
Ex. UIntsToBytes (UInts() As UInt) As Byte()

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

B4X:
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?
 

Attachments

  • Image1.jpg
    24.1 KB · Views: 372
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
Awesome thank you very much emexes, I will give this a go!
 
Upvote 0

emexes

Expert
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:
Upvote 0

monki

Active Member
Licensed User
Longtime User
The parameter for UintstoByte is a array of uints

B4X:
example:
SerialStream.Write(Converter.UIntsToBytes(array as uint(res))

Monki
 
Upvote 0

Jakes72

Active Member
Licensed User
Longtime 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!
 
Upvote 0

emexes

Expert
Licensed User
Some of it a bit over my head
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.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…