Serial and Bluetooth examples & OBDII

dsirio75

Member
Licensed User
Longtime User
Hi all,

i'm working on an app that should connect a OBDII device and take some data.
I tried both Serial and Bluetooth examples but i can't get responses.

Serial Example: I can connect and sent commands like this

B4X:
Sub btnSend_Click
   If connected Then
      TextWriter1.WriteLine(txtSend.Text & Chr(13))
      TextWriter1.Flush
      txtSend.Text = ""
   End If
End Sub

Chr(13) is needed so that the device can answer.
The device returns some data but when i read last line and get next line i get an exception and the app crashes. Seems like i wants to read something null.

B4X:
s = TextReader1.ReadLine

Bluetooth Example:

I can connect and send command. When i send command i get a b4a exception and the app exit without any answer.

Please help me, where i'm wrong whit this?

This is my actual bluetooth example code

B4X:
'Activity module
Sub Process_Globals
   Dim AStream As AsyncStreams
End Sub

Sub Globals
   Dim txtInput As EditText
   Dim txtLog As EditText
   Dim btnSend As Button
End Sub

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

Sub AStream_NewData (Buffer() As Byte)
   
   Dim msg As String
    msg = msg & BytesToString(Buffer, 0, Buffer.Length, "UTF8")
'   msg = msg & BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
'   msg = msg & BytesToString(Buffer, 0, Buffer.Length, "ASCII")
'   msg = msg & BytesToString(Buffer, 0, Buffer.Length, "WINDOWS-1252")

'    msg = msg.Replace(Chr(13), Chr(10))
   
   LogMessage("", msg)         
   
    'LAST CHAR THEN RESET
    'If msg.EndsWith(">") Then         
    '   Log("AStream_NewData: " & msg)
   '   LogMessage("", msg)      
'        blnReadySend = True                  'ELM IDLE STATE INDICATOR
'        Timer_Timeout.Enabled = False        'RESET TIMEOUT
    'Else If msg.Contains(">") Then
    '    Log("AStream_NewData: Bad Data " & msg)
'        blnReadySend = True
'        Timer_Timeout.Enabled = False
    '    msg = ""
    '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
   txtInput.Text = txtInput.Text & Chr(13)
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   LogMessage("Me", txtInput.Text)
End Sub

Sub LogMessage(From As String, Msg As String)
   txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
   txtLog.SelectionStart = txtLog.Text.Length
End Sub

Thanks.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…