Share My Creation OBD2

The application was tested on OBD2 Simulator and Citroen C3 reading only current data and fault codes. The bluetooth obd2 plug and USB-OBD2 are from Ebay, it has ELM327 IC ver 2.1 and 1.5 that connects to all OBDII protocols. The computer's bluetooth pairs with the OBD-II plug and connects to the app via COM Port. On windows computer you can find the COM port number in 'device manager' and enter the port to the app. If you use OBD-II USB you can enter the port number of that.
The app takes bytes A and B and calculates the readable data. The app tests only the popular parameters and VID, no fault resets. In here https://en.wikipedia.org/wiki/OBD-II_PIDs#Bitwise_encoded_PIDs you can find the parameter IDs of data request.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private sp As Serial
    Private astream As AsyncStreams
    Private cmb1 As ComboBox
    Private Label1 As Label
    Private text1 As TextArea
    Private btnClear As Button
    Private btnSend As Button
    Private bc As ByteConverter
    
    Private a, b , p=8 As Int
    Private del As Int =500
    Private cn, reply As String
    Private rate As Int = 38400
    Private cmbBaud As ComboBox
    Private txtTest As TextField
    Private btn01, btn02, btn03 As Button
    Private btn04, btn05, btn06 As Button
    Private btn07, btn0b, btn11 As Button
    Private btn0c, btn0e, btn0f As Button
    Private btn13, btn14, btn15 As Button
    Private btn1c, btn20, btn21 As Button
    
    Private text01, text04, text0b As TextField
    Private text05, text06, text07 As TextField
    Private text0c, text0d, text0e, text0f As TextField
    Private text11, text13, text14 As TextField
    Private text15, text20, text21 As TextField
    Private text1c, text02, text03 As TextField
    Private rb1, rb2 As RadioButton
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    sp.Initialize("")
    cmb1.Items.AddAll(sp.ListPorts)

    cmbBaud.Items.AddAll(Array As String("9600", "38400", "115200"))
    End Sub

Private Sub rb1_SelectedChange(Selected As Boolean)
    If rb1.Selected Then p=9
    If rb2.Selected Then p=8
End Sub

Sub cmb1_SelectedIndexChanged(Index As Int, Value As Object)
    Try
        sp.Open(cmb1.Value)
        sp.SetParams(rate,8,1,0)
        astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
        Label1.Text = "Port Opened"
    Catch
        Label1.Text = "Port is busy"
        Log("Port error")
    End Try

End Sub

Sub cmbBaud_SelectedIndexChanged(Index As Int, Value As Object)
    rate=cmbBaud.Value
    cmb1.Enabled=True
End Sub

Sub AStream_NewData (Buffer() As Byte)
    reply = reply & BytesToString(Buffer, 0, Buffer.Length, "UTF8")

End Sub

Sub convert
    Dim aa(), bb() As Byte
    
    Try   
        aa=bc.HexToBytes(reply.SubString2(p+4,p+6))    '12,14
        bb=bc.HexToBytes(reply.SubString2(p+7,p+9))    '15,17
        a=Bit.And(0xFF,aa(0))        'convert byte to unsigned byte
        b=Bit.And(0xFF,bb(0))
        cn = reply.SubString2(p+1,p+3)    '9,11
        text1.Text = text1.Text & reply
    Catch
        text1.Text = text1.Text & "NO DATA"
    End Try
End Sub

Sub btnSend_Click
    Dim str As String = txtTest.Text &  Chr(13) & Chr(10)
    astream.Write(str.GetBytes("UTF8"))
    Sleep(del)
    text1.Text = text1.Text & reply
    reply = ""   
End Sub

Sub btn01_Click
    Dim str1 As String = "0101" &  Chr(13) & Chr(10)
    astream.Write(str1.GetBytes("UTF8"))
    Sleep(del)
    text01.Text = reply.SubString2(p+4,p+16)
    text1.Text = text1.Text & reply
    reply = ""
End Sub

Sub btn04_Click
    Dim str2 As String = "0104" &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = a*100/255
    text04.Text = a & " %  - " & cn
    reply = ""
End Sub

Sub btn05_Click
    Dim str3 As String  = "0105" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = a-40
    text05.Text = a & " °C  - " & cn
    reply = ""
End Sub

Sub btn06_Click
    Dim str3 As String  = "0106" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert
    a=a*100/128 - 100     '(a * 100) / 128) - 100 & " %"
    text06.Text = a & " %  - " & cn
    reply = ""
End Sub

Sub btn07_Click
    Dim str2 As String = "0107" &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
    Sleep(del)
    convert
    a=a*100/128 - 100
    text07.Text = a & " %  - " & cn
    reply = ""
End Sub

Sub btn0b_Click
    Dim str3 As String  = "010B" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert
    text0b.Text = a & " kPa  - " & cn
    reply = ""
End Sub

Sub btn0c_Click
    Dim c As Int
    Dim str3 As String  = "010C" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert    '(a * 256 + b) / 4 & " RPM"
    c = (a * 256 + b) / 4
    text0c.Text = c & " RPM  - " & cn
    reply = ""
End Sub

Sub btn0d_Click
    Dim str2 As String = "010D" &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
    Sleep(del)
    convert
    text0d.Text = a & " km/h  - " & cn
    reply = ""
End Sub

Sub btn0e_Click
    Dim str3 As String  = "010E" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = (a/2)-64
    text0e.Text = a & " °  - " & cn
    reply = ""
End Sub

Sub btn0f_Click
    Dim str3 As String  = "010F" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = a-40
    text0f.Text = a & " °C  - " & cn
    reply = ""
End Sub

Sub btn11_Click
    Dim str2 As String = "0111" &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = a*100/255
    text11.Text = a & " %  - " & cn
    reply = ""
End Sub

Sub btn13_Click
    Dim str3 As String  = "0113" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    text13.Text = reply.SubString2(p+4,p+6)
    text1.Text = text1.Text & reply
    reply = ""
End Sub

Sub btn14_Click
    Dim str3 As String  = "0114" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = a/200
    b = b*100/128 - 100
    text14.Text = a & " V,  " & b & " %  - " & cn
    reply = ""
End Sub

Sub btn15_Click
    Dim str2 As String = "0115" &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
    Sleep(del)
    convert
    a = a/200
    b = b*100/128 - 100
    text15.Text = a & " V,  " & b & " %  - " & cn
    reply = ""
End Sub

Sub btn1c_Click
    Dim str3 As String  = "011C" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    text1c.Text = reply.SubString2(p+4,p+6)
    text1.Text = text1.Text & reply
    reply = ""
End Sub

Sub btn20_Click
    Dim str3 As String  = "0120" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    text20.Text = reply.SubString2(p+4,p+16)
    text1.Text = text1.Text & reply
    reply = ""
End Sub

Sub btn21_Click
    Dim dis As Int
    Dim str2 As String = "0121" &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
    Sleep(del)
    convert
    dis=a*256+b
    text21.Text = dis & " km  - " & cn
    reply = ""
End Sub

Sub btn03_Click
    Dim dtc As String = ""
    Dim i As Byte
    Dim str3 As String  = "0301" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)
    For i=0 To 3
        dtc = dtc & reply.SubString2(19+i*3,21+i*3)
    Next
    For i=0 To 5
        dtc = dtc & reply.SubString2(36+i*3,38+i*3)
    Next
    text03.Text = dtc
    text1.Text = text1.Text & reply
    reply = ""
End Sub

Sub btn02_Click
    Dim vin As String
    Dim i,c() As Byte
    Dim str3 As String  = "0902" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(del)

    For i=0 To 3
        c=bc.HexToBytes(reply.SubString2(19+i*3,21+i*3))
        vin = vin & BytesToString(c,0,c.Length,"UTF8")
    Next
    For i=0 To 7
        c=bc.HexToBytes(reply.SubString2(36+i*3,38+i*3))
        vin = vin & BytesToString(c,0,c.Length,"UTF8")
    Next
    For i=0 To 6
        c=bc.HexToBytes(reply.SubString2(65+i*3,67+i*3))
        vin = vin & BytesToString(c,0,c.Length,"UTF8")
    Next
    text02.Text = vin
    text1.Text = text1.Text & reply
    reply = ""
End Sub


Sub btnClear_Click
    text1.Text=""
End Sub
 

Attachments

  • obd2-405.gif
    obd2-405.gif
    18.4 KB · Views: 1,857
  • obd2_BT.jpg
    obd2_BT.jpg
    31.2 KB · Views: 301
  • obd2_usb.jpg
    obd2_usb.jpg
    26.9 KB · Views: 285
  • Capture4.gif
    Capture4.gif
    52.9 KB · Views: 299
  • obd2.zip
    5.2 KB · Views: 230
Last edited:
Top