Public Class KWP2000_data_mobitor
'KWP2000 datamonitor for Hayabusa by PetriK
'Copyright (C) 2009, MacMadigan Oy, PetriK
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'GNU General Public License can be found at: http://w...content-available-to-author-only...u.org/licenses/
Public Declare Function FT_SetBreakOn Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
Public Declare Function FT_SetBreakOff Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
Public Declare Function FT_Open Lib "FTD2XX.DLL" (ByVal iDevice As Integer, ByRef lnghandle As Integer) As Integer
Public Declare Function FT_GetNumberOfDevices Lib "FTD2XX.DLL" Alias "FT_ListDevices" (ByRef lngNumberofdevices As Integer, ByVal pvarg2 As String, ByVal lngflags As Integer) As Integer
Public Declare Function FT_ListDevices Lib "FTD2XX.DLL" (ByRef lngNumberofdevices As Integer, ByVal pvarg2 As String, ByVal lngflags As Integer) As Integer
Public Declare Function FT_Close Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
Public Declare Function FT_GetComPortNumber Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByRef portnumber As Integer) As Integer
Public Declare Function FT_SetBaudRate Lib "FTD2XX.DLL" (ByVal lngHandle As Integer, ByVal lngBaudRate As Integer) As Integer
Public Declare Function FT_Write_Bytes Lib "FTD2XX.DLL" Alias "FT_Write" (ByVal lngHandle As Integer, ByRef lpvBuffer As Byte, ByVal lngBufferSize As Integer, ByRef lngBytesWritten As Integer) As Integer
Public Declare Function FT_Read_Bytes Lib "FTD2XX.DLL" Alias "FT_Read" (ByVal lngHandle As Integer, ByRef lpvBuffer As Byte, ByVal lngBufferSize As Integer, ByRef lngBytesReturned As Integer) As Integer
Public Declare Function FT_GetStatus Lib "FTD2XX.DLL" (ByVal lngHandle As Integer, ByRef lngamountInRxQueue As Integer, ByRef lngAmountInTxQueue As Integer, ByRef lngEventStatus As Integer) As Integer
Public lngHandle As Integer
Public comportnum As Integer
Public rxbyte As Byte
Public ticking As Integer
Public FT_status As Integer
Public rxqueue, txqueue, eventstat As Integer
Public rxsptr As Integer
Public rxs(128) As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'
' Initialize the program, input for everything is serialport.portname
'
serialport.PortName = "COM5" ' this will be used for FTDI device handle, important to have correct value here
TextBox1.Text = "Program started... "
comportnum = Val(Mid$(serialport.PortName, 4)) ' com port address
If ((comportnum < 0) Or (comportnum > 8)) Then MsgBox("USB FTDI COMport is non existing or out of range, program may not work")
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "Using comport =" & comportnum
Timer1.Enabled = False
Timer1.Interval = 250
Button1.Enabled = True
ListBox1.SelectedIndex = 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'
' Connect to ecu
'
Dim i, x, y As Integer
Dim txbyte As Byte
Dim bytecount As Integer
'
' Initialize required values
'
Button1.Enabled = False ' do not allow port reopened while monitoring data
'
' Get the FTDI device handle based on com port number
'
FT_status = FT_GetNumberOfDevices(i, 0, &H80000000)
If FT_status <> 0 Then MsgBox("Can not get number of FTDI devices, error:" & FT_status)
TextBox1.Text = "Number of FTDI devices connected =" & i
i = i - 1
For x = 0 To i
FT_status = FT_Open(i, lngHandle) ' only one
If FT_status <> 0 Then MsgBox("Can not open FTDI USB device, error:" & FT_status)
FT_status = FT_GetComPortNumber(lngHandle, y)
If FT_status <> 0 Then MsgBox("Can not get FTDI com port number, error:" & FT_status)
If y = comportnum Then x = i
Next
If FT_status <> 0 Then MsgBox("Can not open FTDI USB device, error:" & FT_status)
FT_status = FT_SetBaudRate(lngHandle, 10400)
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "FTDI USB device opened for 10400 baud, id=" & i & ", port=" & comportnum
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "Initiating 5baud fast initialization"
i = Val(ListBox1.Text)
ticking = 0
'
' 5 Baud Init using FTDI usb driver
'
System.Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.Highest
FT_status = FT_SetBreakOff(lngHandle) ' K-line high
System.Threading.Thread.Sleep(300)
FT_status = FT_SetBreakOn(lngHandle) ' K-line low
System.Threading.Thread.Sleep(i)
FT_status = FT_SetBreakOff(lngHandle) ' K-line high
System.Threading.Thread.Sleep(i)
'
' Capture &H00 from ECU
'
System.Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.Normal
rxbyte = &HFF
bytecount = 0
While (ticking < 100) And (rxbyte <> 0) And (bytecount = 0)
ticking = ticking + 1
FT_status = FT_Read_Bytes(lngHandle, rxbyte, 1, bytecount)
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "Captured &H00 from ECU" & " using " & i & "ms"
i = 25 ' break the for next loop when &H00 found
End While
'
' Send init request to ecu if &H00 read succesfully
'
If ticking < 100 Then
txbyte = &H81
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H12
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &HF1
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H81
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H5
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
'
' Pass control timer to read bytes continously
'
Timer1.Enabled = True
ticking = 0
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "Completed 5baud fast initialization" & Chr(13) & Chr(10)
Else
Timer1.Enabled = False
ticking = 0
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "Failed 5baud initialization" & Chr(13) & Chr(10)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'
' first send close connection to ecu and then close comms port
'
FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
If FT_status = 0 Then
'
' add here code to send close connection to ecu before closing the comport
'
Timer1.Enabled = False
Button1.Enabled = True
ticking = 0
FT_status = FT_Close(lngHandle)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
Dim txbyte As Byte
'
' This timer is used to read any bytes after initialization sequence
'
FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
If FT_status = 0 And rxqueue > 0 Then
For i = 1 To rxqueue
FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
rxs(rxsptr) = rxbyte
rxsptr = rxsptr + 1
TextBox1.Text = TextBox1.Text & Format(rxbyte, "x2") & " "
Next
ticking = 0
End If
ticking = ticking + 1
'
' It is
time ask sensor data from ECU
'
txbyte = &H80
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H12
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &HF1
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H2
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H21
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &H8
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
txbyte = &HAE
FT_Write_Bytes(lngHandle, txbyte, 1, 1)
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10)
rxsptr = 0
For x = 0 To 50
Select Case x
Case 26
L_TPS.Text = Format(rxs(x), "000")
Case 27
L_IAP.Text = Format(rxs(x), "000")
Case 28
L_CLT.Text = Format(rxs(x), "000")
End Select
Next
'
' No response from the line for a while, lets kill the comms
'
If ticking > 100 Then
Timer1.Enabled = False
Button1.Enabled = True
ticking = 0
rxsptr = 0
FT_status = FT_Close(lngHandle)
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & "No traffic, killing listening process"
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = ""
End Sub
End Class