B4A syntax vs Perl syntax

mitsusdev

Member
Licensed User
Longtime User
Hi,
i've a question. I'm develop a new app that use and IT to BT converter for data read from an IR device linked to the phone over bluetooth.

So i'm able to link the IT-to-BT adapter (after Erel explanation See thread: http://www.b4x.com/forum/basic4android-updates-questions/17492-bluetooth-ir-converter.html#post100166)

So now my question is that using this converter with my linux workstation, i'm able to talk with remote IR device (using my IR-to-Bluetooth converter) using a little perl script.

B4X:
  $out = pack(C,hex(B)); #Convert hex(B) into and unsigned char
  $foo = $meterport->write($out); # Write data over IR
  sleep 0.1;
  $foo = $meterport->input; # Read data over IR from remote device

  $out = pack(C,hex(D)); #Convert hex(D) (CR) into and unsigned char
  $foo = $meterport->write($out); # Write data over IR
  sleep 0.2;
  $bah = $meterport->input;

  print $bah;

..it wotk fine, and the latest "print" is able to see the serial number of remote device.

I'm trying to read data (from B4A) using the same technique, but the remote device is able only of send back commands.
Below my B4A code (symilar to bluetooth chat example):

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("2")
   If AStream.IsInitialized = False Then
      AStream.Initialize(Main.serial1.InputStream, Main.serial1.OutputStream, "AStream")
   End If
   'txtLog.Width = 100%x
End Sub

Sub AStream_NewData (buffer() As Byte)
   Dim tmpGlucose As Int
   'Wait(100)
   i = i + 1
   Log("i: " & i)
   Log("DIm buffer:" & buffer.Length)
   Log("****** Buffer content: " & BConv.HexFromBytes(buffer))
   

   If i = 1 Then
      AStream.Write(BConv.HexToBytes(0x0D))
      Log("SEND : 0x0D")
      Wait(20)
      
   End If
      
End Sub

Sub AStream_Error
   ToastMessageShow("Connection is broken.", True)
   btnSend.Enabled = False
   'txtInput.Enabled = False
End Sub

Sub AStream_Terminated
   AStream_Error
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
      AStream.Close
   End If
End Sub

Sub txtInput_EnterPressed
   If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
   Dim checkTV As Byte
   
   i = 0
   
   checkTV = 0x0B ' Hex init data <CAN>
   
   Log("SEND <CAN>: " & checkTV) 
   AStream.Write(BConv.HexToBytes(checkTV))
   Wait(10)
End Sub

Sub Wait(Sekunden As Int)
   Dim Ti As Long
   Ti = DateTime.Now + Sekunden
   Log("Wait for " & Sekunden & "msec")
   Do While DateTime.Now < Ti
      DoEvents
   Loop
End Sub

Where is my errors?

Thanks and Best Regards
 

mitsusdev

Member
Licensed User
Longtime User
You can remove the wait method it is not needed.

If I understand correctly AStream_NewData is not raised, correct?

Are you sure that the message you send is complete?

Hi Erel,
OK, i've removed wait() function.

I'm able to send data over IR to remote device and i'm able to receive its "echo back" using AStream_NewData.

My problem is that after send last data "0x0D" (Carriage Return) i don't read back the correct value (Serial Number of remote device), but another echo value (in thi case 0x0D).

So...my question is that i don't know if i want to send and unsigned char [hex value..in perl pack(C,hex(D))] over serial-bluetooth i must use AStream.Write(HexFromBytes(0x0D) or another.

I've tried to convert hex value to unsigned char using chr(0x0D)...but i don't know ho to send it to remote device.

Can you help me please?

Thanks a lot
 
Last edited:
Upvote 0

mitsusdev

Member
Licensed User
Longtime User
You do not need to use HexFromBytes.
You should just put the values in an array of bytes:
B4X:
Dim b(1) As byte
b(0) = 0x0d
AStream.Write(b)

Erel, now it work fine!!

I'm able to receive data from remote device.

Thanks a lot
 
Upvote 0
Top