/BusaECUeditor/Backup/Backup/Common/Main.vb

https://bitbucket.org/ecueditor/ecueditor/ · Visual Basic · 4727 lines · 3553 code · 483 blank · 691 comment · 21 complexity · c6c5c3e96cca120790bef2292cd025e3 MD5 · raw file

  1. '
  2. ' This file is part of BusaECUeditor - Hayabusa ECUeditor
  3. '
  4. ' Hayabusa ECUeditor is free software: you can redistribute it and/or modify
  5. ' it under the terms of the GNU General Public License as published by
  6. ' the Free Software Foundation, either version 3 of the License, or
  7. ' (at your option) any later version.
  8. '
  9. ' Hayabusa ECUeditor is distributed in the hope that it will be useful,
  10. ' but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ' GNU General Public License for more details.
  13. '
  14. ' You should have received a copy of the GNU General Public License
  15. ' along with Hayabusa ECUeditor. If not, see <http://www.gnu.org/licenses/>.
  16. '
  17. ' Notice: Please note that under GPL if you use this program or parts of it
  18. ' you are obliged to distribute your software including source code
  19. ' under this same license for free. For more information see paragraph 5
  20. ' of the GNU licence.
  21. '
  22. Imports System
  23. Imports System.Threading
  24. Imports System.IO
  25. Imports System.Text
  26. Imports System.Net.Mail
  27. Imports System.Deployment.Application
  28. Public Class main
  29. #Region "Declare Functions"
  30. Public Declare Function FT_GetModemStatus Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByRef modstat As Integer) As Integer
  31. Public Declare Function FT_SetBreakOn Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
  32. Public Declare Function FT_SetBreakOff Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
  33. Public Declare Function FT_Open Lib "FTD2XX.DLL" (ByVal iDevice As Integer, ByRef lnghandle As Integer) As Integer
  34. Public Declare Function FT_Purge Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
  35. Public Declare Function FT_SetRts Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
  36. Public Declare Function FT_ClrRts Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
  37. Public Declare Function FT_SetDtr Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer 'new for Interface V1.1 ***************************************
  38. Public Declare Function FT_ClrDtr Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer 'new for Interface V1.1 ***************************************
  39. Public Declare Function FT_SetLatencyTimer Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByVal uTime As Byte) As Integer
  40. Public Declare Function FT_SetUSBParameters Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByVal dwInTransferSize As Integer, ByVal dwOutTransferSize As Integer) As Integer
  41. Public Declare Function FT_ResetDevice Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByVal iMask As Integer) As Integer
  42. 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
  43. Public Declare Function FT_ListDevices Lib "FTD2XX.DLL" (ByRef lngNumberofdevices As Integer, ByVal pvarg2 As String, ByVal lngflags As Integer) As Integer
  44. Public Declare Function FT_Close Lib "FTD2XX.DLL" (ByVal lnghandle As Integer) As Integer
  45. Public Declare Function FT_GetComPortNumber Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByRef portnumber As Integer) As Integer
  46. Public Declare Function FT_SetBaudRate Lib "FTD2XX.DLL" (ByVal lngHandle As Integer, ByVal lngBaudRate As Integer) As Integer
  47. Public Declare Function FT_SetDataCharacteristics Lib "FTD2XX.DLL" (ByVal lnghandle As Integer, ByVal uWordLength As Byte, ByVal uStopBits As Byte, ByVal uParity As Byte) As Integer
  48. 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
  49. 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
  50. 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
  51. Public Declare Function FT_SetTimeouts Lib "FTD2XX.DLL" (ByVal lngHandle As Integer, ByVal rxTimeout As Integer, ByVal txTimeout As Integer) As Integer
  52. Public Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Integer) As Integer
  53. Public Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Integer) As Integer
  54. Public Declare Function FT_Write Lib "FTD2XX.DLL" (ByVal ftHandle As Integer, ByVal lpBuffer() As Byte, ByVal nBufferSize As Integer, ByRef lpBytesWritten As Integer) As Integer
  55. Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As Long) As Short 'new HighResTimer*******************************
  56. Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef X As Long) As Short ' new HighResTimer************************
  57. #End Region
  58. #Region "Variables"
  59. Dim path As String = ""
  60. Dim comparepath As String = ""
  61. Dim fs As FileStream
  62. #End Region
  63. #Region "Form Events"
  64. Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  65. Me.Visible = False
  66. '
  67. ' COmmented out for VBonXP
  68. '
  69. 'My.Application.SaveMySettingsOnExit = True
  70. '
  71. ' Win XP error handler
  72. '
  73. On Error GoTo skip_update
  74. Dim deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
  75. Dim update As UpdateCheckInfo = deploy.CheckForDetailedUpdate()
  76. If (deploy.CheckForUpdate()) Then
  77. If (Updateavail.ShowDialog() = Windows.Forms.DialogResult.OK) Then
  78. deploy.Update()
  79. Application.Restart()
  80. End If
  81. End If
  82. skip_update:
  83. ' initialize global variables, just in case
  84. DisableButtons()
  85. path = My.Settings.Item("path")
  86. comparepath = My.Settings.Item("comparepath")
  87. ECUVersion = ""
  88. BlockPgm = True ' just initializing
  89. 'MsgBox("Important note: This is a prerelease of Hayabusa ECUeditor v2.0 for testing new functionality.")
  90. If My.Settings.Item("ComPort") = "" Then MsgBox("Com port not set up. Please click 'Connect for enginedata' and set up the correct com port.")
  91. LoginForm.Show()
  92. LoginForm.Select()
  93. End Sub
  94. #End Region
  95. #Region "Control Events"
  96. Private Sub B_FuelMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_FuelMap.Click
  97. Select Case ECUversion
  98. Case "gen1"
  99. FuelMap.Show()
  100. FuelMap.Select()
  101. Case "gen2"
  102. K8Fuelmap.Show()
  103. K8Fuelmap.Select()
  104. Case "bking"
  105. BKingFuelMap.Show()
  106. BKingFuelMap.Select()
  107. Case "gixxer"
  108. GixxerFuelmap.Show()
  109. GixxerFuelmap.Select()
  110. Case Else
  111. MsgBox("Feature not yet implemented")
  112. End Select
  113. End Sub
  114. Private Sub B_IgnitionMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_IgnitionMap.Click
  115. Select Case ECUversion
  116. Case "gen1"
  117. Ignitionmap.Show()
  118. Ignitionmap.Select()
  119. Case "gen2"
  120. K8Ignitionmap.Show()
  121. K8Ignitionmap.Select()
  122. Case "gixxer"
  123. GixxerIgnitionmap.Show()
  124. GixxerIgnitionmap.Select()
  125. Case "bking"
  126. BKingIgnitionMap.Show()
  127. BKingIgnitionMap.Select()
  128. Case Else
  129. MsgBox("Feature not yet implemented")
  130. End Select
  131. End Sub
  132. Private Sub B_Limiters_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Limiters.Click
  133. Select Case ECUversion
  134. Case "gen1"
  135. Limiters.Show()
  136. Limiters.Select()
  137. Case "gen2"
  138. K8Limiters.Show()
  139. K8Limiters.Select()
  140. Case "bking"
  141. BKingLimiters.Show()
  142. BKingLimiters.Select()
  143. Case "gixxer"
  144. GixxerLimiters.Show()
  145. GixxerLimiters.Select()
  146. Case Else
  147. MsgBox("feature not yet supported")
  148. End Select
  149. End Sub
  150. Private Sub B_FlashECU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_FlashECU.Click
  151. FlashTheECU()
  152. End Sub
  153. Private Sub B_EngineData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_EngineData.Click
  154. Select Case ECUversion
  155. Case "gen1"
  156. Datastream.Show()
  157. Datastream.Select()
  158. Case "gen2"
  159. K8Datastream.Show()
  160. K8Datastream.Select()
  161. Case "bking"
  162. K8Datastream.Show()
  163. K8Datastream.Select()
  164. Case "gixxer"
  165. K8Datastream.Show()
  166. K8Datastream.Select()
  167. Case "GixxerK5"
  168. K8Datastream.Show()
  169. K8Datastream.Select()
  170. Case Else
  171. MsgBox("Feature not yet implemented")
  172. End Select
  173. End Sub
  174. Private Sub B_AdvancedSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_AdvancedSettings.Click
  175. Select Case ECUVersion
  176. Case "gen1"
  177. AdvSettings.Show()
  178. AdvSettings.Select()
  179. Case "gen2"
  180. K8Advsettings.Show()
  181. K8Advsettings.Select()
  182. Case "bking"
  183. BKingAdvSettings.Show()
  184. BKingAdvSettings.Select()
  185. Case Else
  186. MsgBox("Feature not yet implemented")
  187. End Select
  188. End Sub
  189. Private Sub B_Shifter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Shifter.Click
  190. Select Case ECUVersion
  191. Case "gen1"
  192. Shifter.Show()
  193. Shifter.Select()
  194. Case "gen2"
  195. K8shifter.Show()
  196. K8shifter.Select()
  197. Case "bking"
  198. BKingShifter.Show()
  199. BKingShifter.Select()
  200. Case Else
  201. MsgBox("Feature not yet implemented")
  202. End Select
  203. End Sub
  204. Private Sub G1NewBaseMapUSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles G1NewBaseMapUSToolStripMenuItem.Click
  205. 'JaSa 01.July.2010
  206. 'G1 US base map selected --> reading of bin will hapen on G1ReadMap procedure
  207. Dim defpath As String ' this is for this subroutine only
  208. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\G1BusaUSdefault.bin"
  209. G1ReadMap(defpath)
  210. End Sub
  211. Private Sub G1NewBaseMapEUToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles G1NewBaseMapEUToolStripMenuItem.Click
  212. 'JaSa 01.July.2010
  213. 'G1 EU base map selected --> reading of bin will hapen on G1ReadMap procedure
  214. Dim defpath As String ' this is for this subroutine only
  215. ' OK, so the file is found, now lets start processing it
  216. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\G1BusaEUdefault.bin"
  217. G1ReadMap(defpath)
  218. End Sub
  219. Private Sub NewK8ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewK8ToolStripMenuItem.Click
  220. Dim defpath As String ' this is for this subroutine only
  221. CloseChildWindows()
  222. ' OK, so the file is found, now lets start processing it
  223. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\k8.bin"
  224. L_File.Text = ""
  225. L_Comparefile.Text = ""
  226. DisableButtons()
  227. ' Open the stream and read it to global variable "Flash".
  228. fs = File.OpenRead(defpath)
  229. Dim b(1) As Byte
  230. Dim i As Integer
  231. i = 0
  232. Do While fs.Read(b, 0, 1) > 0
  233. Flash(i) = b(0)
  234. FlashCopy(i) = b(0)
  235. i = i + 1
  236. Loop
  237. fs.Close()
  238. ' Check that the binary lenght matches expected ecu
  239. If i <> (262144 * 4) Then
  240. ECUNotSupported.ShowDialog()
  241. End If
  242. ECUVersion = "gen2"
  243. '
  244. ' Make sure the ECU id is supported type
  245. '
  246. i = 0
  247. ECUID.Text = ""
  248. Do While i < 8
  249. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  250. i = i + 1
  251. Loop
  252. ' check the ecu id bytes and validate that the ecu flash image is supported
  253. If Mid(ECUID.Text, 1, 6) <> "DJ18SE" Then
  254. ECUNotSupported.ShowDialog()
  255. Else
  256. SetECUType()
  257. End If
  258. ' enable controls, otherwise at form load an event will occur
  259. Limiters.C_RPM.Enabled = True
  260. SaveToolStripMenuItem.Enabled = True
  261. B_FlashECU.Enabled = True
  262. B_Limiters.Enabled = True
  263. B_EngineData.Enabled = True
  264. B_Shifter.Enabled = True
  265. B_FuelMap.Enabled = True
  266. B_IgnitionMap.Enabled = True
  267. B_AdvancedSettings.Enabled = True
  268. B_DataLogging.Enabled = True
  269. K8Fuelmap.Close()
  270. GixxerIgnitionmap.Close()
  271. MsgBox("A new gen2 basemap is generated", MsgBoxStyle.Information)
  272. BlockPgm = True
  273. End Sub
  274. Private Sub NewStockBkingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewStockBkingToolStripMenuItem.Click
  275. Dim defpath As String ' this is for this subroutine only
  276. CloseChildWindows()
  277. ' OK, so the file is found, now lets start processing it
  278. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\bking.bin"
  279. L_File.Text = ""
  280. L_Comparefile.Text = ""
  281. DisableButtons()
  282. ' Open the stream and read it to global variable "Flash".
  283. fs = File.OpenRead(defpath)
  284. Dim b(1) As Byte
  285. Dim i As Integer
  286. i = 0
  287. Do While fs.Read(b, 0, 1) > 0
  288. Flash(i) = b(0)
  289. FlashCopy(i) = b(0)
  290. i = i + 1
  291. Loop
  292. fs.Close()
  293. ' Check that the binary lenght matches expected ecu
  294. If i <> (262144 * 4) Then
  295. ECUNotSupported.ShowDialog()
  296. End If
  297. ECUVersion = "bking"
  298. '
  299. ' Make sure the ECU id is supported type
  300. '
  301. i = 0
  302. ECUID.Text = ""
  303. Do While i < 8
  304. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  305. i = i + 1
  306. Loop
  307. ' check the ecu id bytes and validate that the ecu flash image is supported
  308. If Mid(ECUID.Text, 1, 6) <> "DJ47SE" Then
  309. ECUNotSupported.ShowDialog()
  310. Else
  311. SetECUType()
  312. End If
  313. ' enable controls, otherwise at form load an event will occur
  314. Limiters.C_RPM.Enabled = True
  315. SaveToolStripMenuItem.Enabled = True
  316. B_FlashECU.Enabled = True
  317. B_Limiters.Enabled = True
  318. B_EngineData.Enabled = True
  319. B_Shifter.Enabled = True
  320. B_FuelMap.Enabled = True
  321. B_IgnitionMap.Enabled = True
  322. B_AdvancedSettings.Enabled = True
  323. B_DataLogging.Enabled = True
  324. K8Fuelmap.Close()
  325. GixxerIgnitionmap.Close()
  326. MsgBox("A new Bking basemap is generated", MsgBoxStyle.Information)
  327. BlockPgm = True
  328. End Sub
  329. Private Sub NewStockBkingUSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewStockBkingUSToolStripMenuItem.Click
  330. Dim defpath As String ' this is for this subroutine only
  331. CloseChildWindows()
  332. ' OK, so the file is found, now lets start processing it
  333. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\BkingUS.bin"
  334. L_File.Text = ""
  335. L_Comparefile.Text = ""
  336. DisableButtons()
  337. ' Open the stream and read it to global variable "Flash".
  338. fs = File.OpenRead(defpath)
  339. Dim b(1) As Byte
  340. Dim i As Integer
  341. i = 0
  342. Do While fs.Read(b, 0, 1) > 0
  343. Flash(i) = b(0)
  344. FlashCopy(i) = b(0)
  345. i = i + 1
  346. Loop
  347. fs.Close()
  348. ' Check that the binary lenght matches expected ecu
  349. If i <> (262144 * 4) Then
  350. ECUNotSupported.ShowDialog()
  351. End If
  352. ECUVersion = "bking"
  353. '
  354. ' Make sure the ECU id is supported type
  355. '
  356. i = 0
  357. ECUID.Text = ""
  358. Do While i < 8
  359. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  360. i = i + 1
  361. Loop
  362. ' check the ecu id bytes and validate that the ecu flash image is supported
  363. If Mid(ECUID.Text, 1, 6) <> "DJ47SE" Then
  364. ECUNotSupported.ShowDialog()
  365. Else
  366. SetECUType()
  367. End If
  368. ' enable controls, otherwise at form load an event will occur
  369. Limiters.C_RPM.Enabled = True
  370. SaveToolStripMenuItem.Enabled = True
  371. B_FlashECU.Enabled = True
  372. B_Limiters.Enabled = True
  373. B_EngineData.Enabled = True
  374. B_Shifter.Enabled = True
  375. B_FuelMap.Enabled = True
  376. B_IgnitionMap.Enabled = True
  377. B_AdvancedSettings.Enabled = True
  378. B_DataLogging.Enabled = True
  379. K8Fuelmap.Close()
  380. GixxerIgnitionmap.Close()
  381. MsgBox("A new Bking basemap is generated", MsgBoxStyle.Information)
  382. BlockPgm = True
  383. End Sub
  384. Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
  385. ' Lets use OpenFileDialog to open a new flash image file
  386. Dim fdlg As OpenFileDialog = New OpenFileDialog()
  387. Dim fs As FileStream
  388. fdlg.InitialDirectory = path 'My.Application.Info.DirectoryPath
  389. fdlg.Title = "Open ECU .bin file"
  390. fdlg.Filter = "ECU definitions (*.bin)|*.bin"
  391. fdlg.FilterIndex = 1
  392. fdlg.RestoreDirectory = True
  393. fdlg.FileName = path
  394. CloseChildWindows()
  395. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  396. ' OK, so the file is found, now lets start processing it
  397. path = fdlg.FileName
  398. If path.Length > 15 Then
  399. L_Comparefile.Text = "..." & path.Substring(path.Length - 15)
  400. L_File.Text = L_Comparefile.Text
  401. Else
  402. L_Comparefile.Text = path
  403. L_File.Text = L_Comparefile.Text
  404. End If
  405. ' Open the stream and read it to global variable "Flash".
  406. fs = File.OpenRead(path)
  407. Dim b(1) As Byte
  408. Dim i As Integer
  409. i = 0
  410. Do While fs.Read(b, 0, 1) > 0
  411. Flash(i) = b(0)
  412. FlashCopy(i) = b(0)
  413. i = i + 1
  414. Loop
  415. fs.Close()
  416. ' Check that the binary lenght matches expected ecu
  417. ' and initialize variables and stuff as needed
  418. '
  419. ' Remove v1.5 protection if exists
  420. '
  421. If Flash(&H2) = 0 Then
  422. Flash(&H2) = 4
  423. MsgBox("ECUeditor v1.5 protection detected and removed")
  424. End If
  425. Select Case i
  426. Case (262144 * 4)
  427. ECUVersion = "gen2"
  428. '
  429. ' Make sure the ECU id is supported type
  430. '
  431. i = 0
  432. ECUID.Text = ""
  433. Do While i < 8
  434. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  435. i = i + 1
  436. Loop
  437. ' check the ecu id bytes and validate that the ecu flash image is supported
  438. If (Mid(ECUID.Text, 1, 6) <> "DJ18SE") And (Mid(ECUID.Text, 1, 6) <> "DJ47SE") And (Mid(ECUID.Text, 1, 4) <> "DJ0H") And (Mid(ECUID.Text, 1, 4) <> "DT0H") And (Mid(ECUID.Text, 1, 4) <> "DJ21") Then
  439. ECUNotSupported.ShowDialog()
  440. Else
  441. SetECUType()
  442. End If
  443. BlockPgm = True
  444. CloseChildWindows()
  445. Case (262144)
  446. ECUVersion = "gen1"
  447. FlashToolStripMenuItem.Visible = False
  448. ' Make sure the ECU id is supported type
  449. i = 0
  450. ECUID.Text = ""
  451. Do While i < 8
  452. ECUID.Text = ECUID.Text & Chr(Flash(&H3FFF0 + i))
  453. i = i + 1
  454. Loop
  455. ' check the ecu id bytes and validate that the ecu flash image is supported
  456. If Mid(ECUID.Text, 1, 6) <> "BB34BB" Then
  457. ECUNotSupported.ShowDialog()
  458. Else
  459. Hayabusa.Visible = True
  460. G1BinFileVersion(ECUID.Text)
  461. 'Select Case Mid(ECUID.Text, 1, 8)
  462. ' Case "BB34BB51"
  463. 'Hayabusa.Text = "Hayabusa EU "
  464. 'Metric = True
  465. 'ECUVersion = "gen1"
  466. ' Case "BB34BB35"
  467. 'Hayabusa.Text = "Hayabusa USA"
  468. 'Metric = False
  469. 'ECUVersion = "gen1"
  470. ' Case Else
  471. 'Hayabusa.Text = "Unknown model"
  472. 'Metric = True
  473. 'ECUVersion = ""
  474. 'End Select
  475. End If
  476. Case Else
  477. ECUVersion = ""
  478. ECUNotSupported.ShowDialog()
  479. End Select
  480. My.Settings.Item("path") = path
  481. My.Settings.Item("comparepath") = comparepath
  482. ' enable controls, otherwise at form load an event will occur
  483. Limiters.C_RPM.Enabled = True
  484. Select Case ECUVersion
  485. Case "gen1"
  486. B_EngineData.Enabled = True
  487. FuelMap.Close()
  488. IgnitionMap.Close()
  489. FlashToolStripMenuItem.Visible = False
  490. B_DataLogging.Enabled = False
  491. SaveToolStripMenuItem.Enabled = True
  492. B_FlashECU.Enabled = True
  493. B_Limiters.Enabled = True
  494. B_Shifter.Enabled = True
  495. B_FuelMap.Enabled = True
  496. B_IgnitionMap.Enabled = True
  497. B_AdvancedSettings.Enabled = True
  498. Case "gen2"
  499. B_EngineData.Enabled = True
  500. GixxerIgnitionmap.Close()
  501. K8Fuelmap.Close()
  502. FlashToolStripMenuItem.Visible = Enabled
  503. B_DataLogging.Enabled = True
  504. SaveToolStripMenuItem.Enabled = True
  505. B_FlashECU.Enabled = True
  506. B_Limiters.Enabled = True
  507. B_Shifter.Enabled = True
  508. B_FuelMap.Enabled = True
  509. B_IgnitionMap.Enabled = True
  510. B_AdvancedSettings.Enabled = True
  511. Case "bking"
  512. B_EngineData.Enabled = True
  513. BKingIgnitionMap.Close()
  514. BKingFuelMap.Close()
  515. FlashToolStripMenuItem.Visible = Enabled
  516. B_DataLogging.Enabled = True
  517. SaveToolStripMenuItem.Enabled = True
  518. B_FlashECU.Enabled = True
  519. B_Limiters.Enabled = True
  520. B_Shifter.Enabled = True
  521. B_FuelMap.Enabled = True
  522. B_IgnitionMap.Enabled = True
  523. B_AdvancedSettings.Enabled = True
  524. Case "gixxer"
  525. B_EngineData.Enabled = True
  526. GixxerIgnitionmap.Close()
  527. GixxerFuelmap.Close()
  528. FlashToolStripMenuItem.Visible = Enabled
  529. B_DataLogging.Enabled = False
  530. SaveToolStripMenuItem.Enabled = True
  531. B_FlashECU.Enabled = True
  532. B_Limiters.Enabled = True
  533. B_Shifter.Enabled = False
  534. B_FuelMap.Enabled = True
  535. B_IgnitionMap.Enabled = True
  536. B_AdvancedSettings.Enabled = False
  537. Case Else
  538. MsgBox("feature not yet implemented")
  539. End Select
  540. End If
  541. End Sub
  542. Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
  543. ' lets use SaveFileDialog
  544. Dim fs As FileStream
  545. Dim fdlg As SaveFileDialog = New SaveFileDialog()
  546. fdlg.Title = "Save ECU .bin file"
  547. fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
  548. fdlg.FilterIndex = 2
  549. fdlg.RestoreDirectory = True
  550. fdlg.FileName = path
  551. Select Case ECUVersion
  552. Case "gen1"
  553. ' First, lets show a warning dialogue about dangers of updating the ecu
  554. 'If filesavenotice.ShowDialog = Windows.Forms.DialogResult.OK Then
  555. ' and now lets start saving the file using savefiledialog
  556. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  557. path = fdlg.FileName
  558. If Not path.Contains(".bin") Then path = path & ".bin"
  559. ' if the file exists then lets make a backup copy of it just in case...
  560. If File.Exists(path) = True Then
  561. If File.Exists(path & ".bak") Then
  562. File.Delete(path & ".bak")
  563. End If
  564. File.Copy(path, (path & ".bak"))
  565. File.Delete(path)
  566. End If
  567. ' save the file
  568. fs = File.Open(path, FileMode.CreateNew)
  569. fs.Write(Flash, 0, 262144)
  570. fs.Close()
  571. If path.Length > 15 Then
  572. L_File.Text = "..." & path.Substring(path.Length - 15)
  573. Else
  574. L_File.Text = path
  575. End If
  576. End If
  577. 'End If
  578. Case "gen2"
  579. ' First, lets show a warning dialogue about dangers of updating the ecu
  580. 'If filesavenotice.ShowDialog = Windows.Forms.DialogResult.OK Then
  581. ' and now lets start saving the file using savefiledialog
  582. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  583. path = fdlg.FileName
  584. If Not path.Contains(".bin") Then path = path & ".bin"
  585. ' if the file exists then lets make a backup copy of it just in case...
  586. If File.Exists(path) = True Then
  587. If File.Exists(path & ".bak") Then
  588. File.Delete(path & ".bak")
  589. End If
  590. File.Copy(path, (path & ".bak"))
  591. File.Delete(path)
  592. End If
  593. ' save the file
  594. fs = File.Open(path, FileMode.CreateNew)
  595. fs.Write(Flash, 0, (262144 * 4))
  596. fs.Close()
  597. If path.Length > 15 Then
  598. L_File.Text = "..." & path.Substring(path.Length - 15)
  599. Else
  600. L_File.Text = path
  601. End If
  602. End If
  603. Case "bking"
  604. ' First, lets show a warning dialogue about dangers of updating the ecu
  605. 'If filesavenotice.ShowDialog = Windows.Forms.DialogResult.OK Then
  606. ' and now lets start saving the file using savefiledialog
  607. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  608. path = fdlg.FileName
  609. If Not path.Contains(".bin") Then path = path & ".bin"
  610. ' if the file exists then lets make a backup copy of it just in case...
  611. If File.Exists(path) = True Then
  612. If File.Exists(path & ".bak") Then
  613. File.Delete(path & ".bak")
  614. End If
  615. File.Copy(path, (path & ".bak"))
  616. File.Delete(path)
  617. End If
  618. ' save the file
  619. fs = File.Open(path, FileMode.CreateNew)
  620. fs.Write(Flash, 0, (262144 * 4))
  621. fs.Close()
  622. If path.Length > 15 Then
  623. L_File.Text = "..." & path.Substring(path.Length - 15)
  624. Else
  625. L_File.Text = path
  626. End If
  627. End If
  628. Case "gixxer"
  629. ' First, lets show a warning dialogue about dangers of updating the ecu
  630. 'If filesavenotice.ShowDialog = Windows.Forms.DialogResult.OK Then
  631. ' and now lets start saving the file using savefiledialog
  632. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  633. path = fdlg.FileName
  634. If Not path.Contains(".bin") Then path = path & ".bin"
  635. ' if the file exists then lets make a backup copy of it just in case...
  636. If File.Exists(path) = True Then
  637. If File.Exists(path & ".bak") Then
  638. File.Delete(path & ".bak")
  639. End If
  640. File.Copy(path, (path & ".bak"))
  641. File.Delete(path)
  642. End If
  643. ' save the file
  644. fs = File.Open(path, FileMode.CreateNew)
  645. fs.Write(Flash, 0, (262144 * 4))
  646. fs.Close()
  647. If path.Length > 15 Then
  648. L_File.Text = "..." & path.Substring(path.Length - 15)
  649. Else
  650. L_File.Text = path
  651. End If
  652. End If
  653. Case Else
  654. MsgBox("Somehow trying to save a .bin which is not gen1 or gen2. Report this as an error!")
  655. End Select
  656. End Sub
  657. Private Sub OpenComparemapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenComparemapToolStripMenuItem.Click
  658. ' Lets use OpenFileDialog to open a a flash image file
  659. ' this function is used for fuelmap and ignitionmap compare function to compare maps
  660. ' to the previous maps
  661. Dim fdlg As OpenFileDialog = New OpenFileDialog()
  662. Dim fs As FileStream
  663. CloseChildWindows()
  664. Select Case ECUVersion
  665. Case "gen1"
  666. fdlg.InitialDirectory = comparepath 'My.Application.Info.DirectoryPath
  667. fdlg.Title = "Open ECU .bin file"
  668. fdlg.Filter = "ECU definitions (*.bin)|*.bin"
  669. fdlg.FilterIndex = 1
  670. fdlg.RestoreDirectory = True
  671. fdlg.FileName = comparepath
  672. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  673. ' OK, so the file is found, now lets start processing it
  674. comparepath = fdlg.FileName
  675. If comparepath.Length > 15 Then
  676. L_Comparefile.Text = "..." & comparepath.Substring(comparepath.Length - 15)
  677. Else
  678. L_Comparefile.Text = comparepath
  679. End If
  680. ' Open the stream and read it to global variable "Flash".
  681. fs = File.OpenRead(comparepath)
  682. Dim b(1) As Byte
  683. Dim i As Integer
  684. i = 0
  685. Do While fs.Read(b, 0, 1) > 0
  686. FlashCopy(i) = b(0)
  687. i = i + 1
  688. Loop
  689. fs.Close()
  690. ' Check that the binary lenght matches expected ecu
  691. If i <> 262144 Then
  692. ECUNotSupported.ShowDialog()
  693. End If
  694. End If
  695. '
  696. ' Lets write this value into the memory so that its easier for the user to know
  697. '
  698. My.Settings.Item("comparepath") = comparepath
  699. FuelMap.Close()
  700. IgnitionMap.Close()
  701. Case "gen2"
  702. fdlg.InitialDirectory = comparepath 'My.Application.Info.DirectoryPath
  703. fdlg.Title = "Open ECU .bin file"
  704. fdlg.Filter = "ECU definitions (*.bin)|*.bin"
  705. fdlg.FilterIndex = 1
  706. fdlg.RestoreDirectory = True
  707. fdlg.FileName = comparepath
  708. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  709. ' OK, so the file is found, now lets start processing it
  710. comparepath = fdlg.FileName
  711. If comparepath.Length > 15 Then
  712. L_Comparefile.Text = "..." & comparepath.Substring(comparepath.Length - 15)
  713. Else
  714. L_Comparefile.Text = comparepath
  715. End If
  716. ' Open the stream and read it to global variable "Flash".
  717. fs = File.OpenRead(comparepath)
  718. Dim b(1) As Byte
  719. Dim i As Integer
  720. i = 0
  721. Do While fs.Read(b, 0, 1) > 0
  722. FlashCopy(i) = b(0)
  723. i = i + 1
  724. Loop
  725. fs.Close()
  726. ' Check that the binary lenght matches expected ecu
  727. If i <> (262144 * 4) Then
  728. ECUNotSupported.ShowDialog()
  729. End If
  730. End If
  731. '
  732. ' Lets write this value into the memory so that its easier for the user to know
  733. '
  734. My.Settings.Item("comparepath") = comparepath
  735. FuelMap.Close()
  736. IgnitionMap.Close()
  737. Case "bking"
  738. fdlg.InitialDirectory = comparepath 'My.Application.Info.DirectoryPath
  739. fdlg.Title = "Open ECU .bin file"
  740. fdlg.Filter = "ECU definitions (*.bin)|*.bin"
  741. fdlg.FilterIndex = 1
  742. fdlg.RestoreDirectory = True
  743. fdlg.FileName = comparepath
  744. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  745. ' OK, so the file is found, now lets start processing it
  746. comparepath = fdlg.FileName
  747. If comparepath.Length > 15 Then
  748. L_Comparefile.Text = "..." & comparepath.Substring(comparepath.Length - 15)
  749. Else
  750. L_Comparefile.Text = comparepath
  751. End If
  752. ' Open the stream and read it to global variable "Flash".
  753. fs = File.OpenRead(comparepath)
  754. Dim b(1) As Byte
  755. Dim i As Integer
  756. i = 0
  757. Do While fs.Read(b, 0, 1) > 0
  758. FlashCopy(i) = b(0)
  759. i = i + 1
  760. Loop
  761. fs.Close()
  762. ' Check that the binary lenght matches expected ecu
  763. If i <> (262144 * 4) Then
  764. ECUNotSupported.ShowDialog()
  765. End If
  766. End If
  767. '
  768. ' Lets write this value into the memory so that its easier for the user to know
  769. '
  770. My.Settings.Item("comparepath") = comparepath
  771. FuelMap.Close()
  772. IgnitionMap.Close()
  773. End Select
  774. End Sub
  775. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
  776. End
  777. End Sub
  778. Private Sub ProgramInfoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgramInfoToolStripMenuItem.Click
  779. AboutBox.Show()
  780. AboutBox.Select()
  781. End Sub
  782. Private Sub VersionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VersionToolStripMenuItem.Click
  783. Dim strVersion As String
  784. Dim strPublish As String
  785. Dim strTitle As String
  786. strVersion = "DisplayVersion"
  787. strPublish = "Publisher"
  788. strTitle = "DisplayName"
  789. Dim assembly As System.Reflection.Assembly
  790. assembly = System.Reflection.Assembly.GetExecutingAssembly()
  791. If (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) Then
  792. Dim currentVersion As System.Version
  793. currentVersion = My.Application.Deployment.CurrentVersion
  794. strVersion = currentVersion.ToString()
  795. Else
  796. strVersion = assembly.GetName.Version.Major & "." & assembly.GetName.Version.Minor & "." & assembly.GetName.Version.Build & "." & assembly.GetName.Version.Revision
  797. End If
  798. Dim company As System.Reflection.AssemblyCompanyAttribute
  799. company = assembly.GetCustomAttributes(GetType(System.Reflection.AssemblyCompanyAttribute), False)(0)
  800. strPublish = company.Company
  801. Dim title As System.Reflection.AssemblyTitleAttribute
  802. title = assembly.GetCustomAttributes(GetType(System.Reflection.AssemblyTitleAttribute), False)(0)
  803. strTitle = title.Title
  804. Dim description As System.Reflection.AssemblyDescriptionAttribute
  805. description = assembly.GetCustomAttributes(GetType(System.Reflection.AssemblyDescriptionAttribute), False)(0)
  806. strTitle = strTitle & " " & description.Description
  807. MsgBox(vbTab & vbTab & "ECUeditor Version: " & vbTab & strVersion & vbCr _
  808. & vbCr & vbTab & vbTab & " Publisher: " & strPublish & vbCr & vbTab & vbTab & " Version info displayed thanks to Eric. " & vbCr, 0, strTitle)
  809. End Sub
  810. 'Private Sub VersionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VersionToolStripMenuItem.Click
  811. ' ' Option Explicit
  812. ' Dim objShell
  813. ' Dim strEditor, strVersion, strPublish, strTitle
  814. ' strVersion = "DisplayVersion"
  815. ' strPublish = "Publisher"
  816. ' strTitle = "DisplayName"
  817. ' strEditor = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\dde589b887ecb332\"
  818. ' objShell = CreateObject("WScript.Shell")
  819. ' strVersion = objShell.RegRead(strEditor & strVersion)
  820. ' strPublish = objShell.RegRead(strEditor & strPublish)
  821. ' strTitle = objShell.RegRead(strEditor & strTitle)
  822. ' 'Wscript.Echo "ECUeditor Version: " & vbTab & strVersion & vbCr _
  823. ' MsgBox(vbTab & vbTab & "ECUeditor Version: " & vbTab & strVersion & vbCr _
  824. ' & vbCr & vbTab & vbTab & " Publisher: " & strPublish & vbCr & vbTab & vbTab & " Version info displayed thanks to Eric. " & vbCr, 0, strTitle)
  825. 'End Sub
  826. Private Sub ProgramUpdateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  827. ProgramUpdate.Show()
  828. ProgramUpdate.Select()
  829. End Sub
  830. Private Sub InstallFTDIDriversToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstallFTDIDriversToolStripMenuItem.Click
  831. Dim s As String
  832. s = Application.StartupPath
  833. If Not File.Exists(Application.StartupPath & "\common\FTDI_CDM_2.04.16.exe") Then
  834. MsgBox("FTDI driver is missing, can not be installed")
  835. Return
  836. Else
  837. If MsgBox("Install/reinstall the FTDI USB COM port drivers, press OK or Cancel", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
  838. Shell(Application.StartupPath & "\common\FTDI_CDM_2.04.16.exe", AppWinStyle.NormalFocus, True, -1)
  839. MsgBox("When drivers are installed, now reboot your computer")
  840. Else
  841. MsgBox("Drivers not installed")
  842. End If
  843. End If
  844. End Sub
  845. Private Sub SetupCOMPortToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetupCOMPortToolStripMenuItem.Click
  846. K8setupcomport.Show()
  847. End Sub
  848. Private Sub VerifyChecksumToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerifyChecksumToolStripMenuItem.Click
  849. Select Case ECUVersion
  850. Case "gen1"
  851. MsgBox("Command not supported with gen1 ecu.")
  852. Case "gen2"
  853. TestCheckSum()
  854. Case "gixxer"
  855. TestCheckSum()
  856. Case "bking"
  857. TestCheckSum()
  858. Case Else
  859. MsgBox("Unknown ecu type, command not supported.")
  860. End Select
  861. End Sub
  862. Private Sub VerifyECUToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerifyECUToolStripMenuItem.Click
  863. Select Case ECUVersion
  864. Case "gen1"
  865. MsgBox("Command not supported with gen1 ecu.")
  866. Case "gen2"
  867. ReadECU()
  868. Case "bking"
  869. ReadECU()
  870. Case "gixxer"
  871. ReadECU()
  872. Case Else
  873. MsgBox("Unknown ecu type, command not supported.")
  874. End Select
  875. End Sub
  876. Private Sub FullEraseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FullEraseToolStripMenuItem.Click
  877. Select Case ECUVersion
  878. Case "gen1"
  879. MsgBox("Command not supported with gen1 ecu.")
  880. Case "gen2"
  881. EraseECU()
  882. Case "bking"
  883. EraseECU()
  884. Case Else
  885. MsgBox("Unknown ecu type, command not supported.")
  886. End Select
  887. End Sub
  888. Private Sub FlashTheECUToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlashTheECUToolStripMenuItem.Click
  889. Select Case ECUVersion
  890. Case "gen1"
  891. MsgBox("Command not supported with gen1 ecu.")
  892. Case "gen2"
  893. FlashTheECU()
  894. Case "bking"
  895. FlashTheECU()
  896. Case "gixxer"
  897. FlashTheECU()
  898. Case Else
  899. MsgBox("Unknown ecu type, command not supported.")
  900. End Select
  901. End Sub
  902. Private Sub HomepageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  903. End Sub
  904. Private Sub L_ProgramHomepage_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles L_ProgramHomepage.LinkClicked
  905. System.Diagnostics.Process.Start("http://www.ecueditor.com")
  906. End Sub
  907. #End Region
  908. #Region "Functions"
  909. Private Sub G1BinFileVersion(ByVal ecuid As String)
  910. Select Case Mid(ecuid, 1, 8)
  911. Case "BB34BB51"
  912. Hayabusa.Text = "Hayabusa EU 32920-24FGO"
  913. Metric = True
  914. ECUVersion = "gen1"
  915. Case "BB34BB35"
  916. Hayabusa.Text = "Hayabusa USA 32920-24FKO"
  917. Metric = False
  918. ECUVersion = "gen1"
  919. Case Else
  920. Hayabusa.Text = "Unknown model"
  921. Metric = True
  922. ECUVersion = ""
  923. End Select
  924. End Sub
  925. Private Sub G1ReadMap(ByVal defpath)
  926. 'this procedure reads base map from the location given as parameter
  927. 'this procedure is for G1 only
  928. 'JaSa 01.July.2010
  929. L_File.Text = ""
  930. L_Comparefile.Text = ""
  931. ' Open the stream and read it to global variable "Flash".
  932. fs = File.OpenRead(defpath)
  933. Dim b(1) As Byte
  934. Dim i As Integer
  935. i = 0
  936. Do While fs.Read(b, 0, 1) > 0
  937. Flash(i) = b(0)
  938. FlashCopy(i) = b(0)
  939. i = i + 1
  940. Loop
  941. fs.Close()
  942. ' Check that the binary lenght matches expected ecu
  943. If i <> 262144 Then
  944. ECUNotSupported.ShowDialog()
  945. End If
  946. '
  947. ' Make sure the ECU id is supported type
  948. '
  949. i = 0
  950. ECUID.Text = ""
  951. Do While i < 8
  952. ECUID.Text = ECUID.Text & Chr(Flash(&H3FFF0 + i))
  953. i = i + 1
  954. Loop
  955. ' check the ecu id bytes and validate that the ecu flash image is supported
  956. If Mid(ECUID.Text, 1, 6) <> "BB34BB" Then
  957. ECUNotSupported.ShowDialog()
  958. Else
  959. Hayabusa.Visible = True
  960. FlashToolStripMenuItem.Visible = False
  961. G1BinFileVersion(ECUID.Text)
  962. 'Select Case Mid(ECUID.Text, 1, 8)
  963. ' Case "BB34BB51"
  964. 'Hayabusa.Text = "Hayabusa EU 32920-24FGO"
  965. 'Metric = True
  966. 'ECUVersion = "gen1"
  967. ' Case "BB34BB35"
  968. 'Hayabusa.Text = "Hayabusa USA 32920-24FKO"
  969. 'Metric = False
  970. 'ECUVersion = "gen1"
  971. ' Case Else
  972. 'Hayabusa.Text = "Unknown model"
  973. 'Metric = True
  974. 'ECUVersion = ""
  975. 'End Select
  976. End If
  977. ' enable controls, otherwise at form load an event will occur
  978. Limiters.C_RPM.Enabled = True
  979. SaveToolStripMenuItem.Enabled = True
  980. B_FlashECU.Enabled = True
  981. B_Limiters.Enabled = True
  982. B_Shifter.Enabled = True
  983. B_FuelMap.Enabled = True
  984. B_IgnitionMap.Enabled = True
  985. B_AdvancedSettings.Enabled = True
  986. B_EngineData.Enabled = True
  987. B_DataLogging.Enabled = False
  988. ' if the computername does not match to the stored computername, do not use the email address from this map
  989. CloseChildWindows()
  990. MsgBox("A new gen1 " & Hayabusa.Text & " basemap is generated", MsgBoxStyle.Information)
  991. End Sub
  992. Private Sub DisableButtons()
  993. '
  994. ' set the controls enabled or disabled at start or when file is reload.
  995. ' the ecu version testing sets the correct buttons visible or enabled.
  996. '
  997. Hayabusa.Visible = False
  998. OpenToolStripMenuItem.Enabled = True
  999. SaveToolStripMenuItem.Enabled = False
  1000. B_Limiters.Enabled = False
  1001. B_Shifter.Enabled = False
  1002. B_FlashECU.Enabled = False
  1003. B_FuelMap.Enabled = False
  1004. B_IgnitionMap.Enabled = False
  1005. B_AdvancedSettings.Enabled = False
  1006. B_EngineData.Enabled = False
  1007. B_DataLogging.Enabled = False
  1008. ReadProcessOnGoing = False
  1009. FuelMapVisible = False
  1010. IgnitionMapVisible = False
  1011. End Sub
  1012. Private Sub CloseChildWindows()
  1013. '
  1014. ' This sub closes all open windows that are closeable. Did not get MDI thread working properly so using just vb close instead
  1015. '
  1016. 'gen2
  1017. K8Advsettings.Close()
  1018. K8boostfuel.Close()
  1019. K8Fuelmap.Close()
  1020. K8Datastream.Close()
  1021. GixxerIgnitionmap.Close()
  1022. K8shifter.Close()
  1023. K8dragtools.Close()
  1024. K8injectorbalancemap.Close()
  1025. K8dwellignition.Close()
  1026. K8STPmap.Close()
  1027. GixxerLimiters.Close()
  1028. 'gen1
  1029. FuelMap.Close()
  1030. IgnitionMap.Close()
  1031. AdvSettings.Close()
  1032. Shifter.Close()
  1033. Limiters.Close()
  1034. 'BKing
  1035. BKingFuelMap.Close()
  1036. BKingIgnitionMap.Close()
  1037. BKingAdvSettings.Close()
  1038. BKingShifter.Close()
  1039. BKingLimiters.Close()
  1040. 'gixxer
  1041. GixxerFuelmap.Close()
  1042. End Sub
  1043. Private Sub FlashTheECU()
  1044. Select Case ECUVersion
  1045. Case "gen1"
  1046. RenesasFDT()
  1047. Case "gen2"
  1048. FlashSerial()
  1049. Case "bking"
  1050. FlashSerial()
  1051. Case "gixxer"
  1052. FlashSerial()
  1053. Case Else
  1054. MsgBox("ECU programmer not defined for this .bin file")
  1055. End Select
  1056. End Sub
  1057. Private Sub CleanUpFDTDirectory(ByVal FDTPath As String)
  1058. Dim di As New IO.DirectoryInfo(FDTPath)
  1059. Dim diar1 As IO.FileInfo() = di.GetFiles()
  1060. Dim dra As IO.FileInfo
  1061. Dim delcount As Integer
  1062. delcount = 0
  1063. '
  1064. 'Cleanup all BB34*.bin files which are older than -14 days
  1065. '
  1066. For Each dra In diar1
  1067. If dra.CreationTime < (My.Computer.Clock.LocalTime.AddDays(-14)) Then
  1068. If dra.FullName.Contains(".bin") And dra.FullName.Contains("BB34") Then
  1069. File.Delete(dra.FullName)
  1070. delcount = delcount + 1
  1071. End If
  1072. End If
  1073. Next
  1074. End Sub
  1075. Private Function FName(ByVal p As String) As String
  1076. Dim testFile As System.IO.FileInfo
  1077. testFile = My.Computer.FileSystem.GetFileInfo(p)
  1078. Dim folderPath As String = testFile.DirectoryName
  1079. Dim fileName As String = testFile.Name
  1080. Return (fileName)
  1081. End Function
  1082. Public Shared Function StrToByteArray(ByVal str As String) As Byte()
  1083. Dim encoding As New System.Text.ASCIIEncoding()
  1084. Return encoding.GetBytes(str)
  1085. End Function 'StrToByteArray
  1086. Private Sub RenesasFDT()
  1087. Dim path As String
  1088. Dim shpath As String
  1089. Dim curpath As String
  1090. Dim FDTpath As String
  1091. Dim FDTfile As String
  1092. Dim MOTpath As String
  1093. Dim MOTfile As String
  1094. Dim i As Integer
  1095. Dim b() As Byte
  1096. Dim s_record As String
  1097. Dim r0, r1, r2, r3, r4 As Integer
  1098. Dim l As Integer
  1099. Dim cks As Integer
  1100. Dim cb(4) As Byte
  1101. ' program assumes FDT.exe in one of the following directory
  1102. FDTpath = My.Settings.Item("FDTpath")
  1103. If Not File.Exists(FDTpath & "FDT.EXE") Then
  1104. ' check out for the known versions, assume latest known being used if found
  1105. If File.Exists("C:\Program Files\Renesas\FDT3.07\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT3.07\"
  1106. If File.Exists("C:\Program Files\Renesas\FDT4.00\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.00\"
  1107. If File.Exists("C:\Program Files\Renesas\FDT4.01\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.01\"
  1108. If File.Exists("C:\Program Files\Renesas\FDT4.02\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.02\"
  1109. If File.Exists("C:\Program Files\Renesas\FDT4.03\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.03\"
  1110. If File.Exists("C:\Program Files\Renesas\FDT4.04\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.04\"
  1111. If File.Exists("C:\Program Files\Renesas\FDT4.05\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.05\"
  1112. If File.Exists("C:\Program Files\Renesas\FDT4.06\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.06\"
  1113. If File.Exists("C:\Program Files\Renesas\FDT4.1\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.1\"
  1114. If File.Exists("C:\Program Files\Renesas\FDT4.10\" & "FDT.EXE") Then FDTpath = "C:\Program Files\Renesas\FDT4.10\"
  1115. My.Settings.Item("FDTpath") = FDTpath ' store the latest know as the default_owner
  1116. End If
  1117. If Not File.Exists(FDTpath & "FDT.EXE") Then
  1118. MsgBox("FDT.exe not found, please install from www.renesas.com/fdt. Only versions 3.07, 4.00, 4.01, 4.02 tested.", MsgBoxStyle.Information)
  1119. Else
  1120. ' FDT FOUND, flashing is possible
  1121. ' first close the comport if its open
  1122. Try
  1123. If Datastream.SerialPort1.IsOpen Then
  1124. Datastream.SerialPort1.Close()
  1125. Datastream.Close()
  1126. End If
  1127. Catch ex As Exception
  1128. Datastream.TextBox1.Text = ex.Message
  1129. End Try
  1130. '
  1131. ' generate the bin to be flashed
  1132. '
  1133. FDTfile = "ecuflash.bin"
  1134. path = FDTpath & FDTfile
  1135. ' if the temporary file exists for any reason, delete it
  1136. If File.Exists(path) = True Then
  1137. File.Delete(path)
  1138. End If
  1139. ' create the temporary file from current memory
  1140. fs = File.Open(path, FileMode.CreateNew)
  1141. fs.Write(Flash, 0, 262144)
  1142. fs.Close()
  1143. '
  1144. ' create also a .mot s-record file from current memory and use that instead of .bin
  1145. '
  1146. MOTfile = "ecuflash.mot"
  1147. MOTpath = FDTpath & MOTfile
  1148. ' if the .mot file exists for any reason, delete it
  1149. If File.Exists(MOTpath) = True Then
  1150. File.Delete(MOTpath)
  1151. End If
  1152. ' create the temporary file from current memory and calculate checksum (ones complement)
  1153. fs = File.OpenWrite(MOTpath)
  1154. For i = 0 To (262144 / 4) - 1
  1155. ' read the flashword and set address
  1156. r0 = i * 4
  1157. r1 = ReadFlashByte((i * 4) + 0)
  1158. r2 = ReadFlashByte((i * 4) + 1)
  1159. r3 = ReadFlashByte((i * 4) + 2)
  1160. r4 = ReadFlashByte((i * 4) + 3)
  1161. ' calculate checksum
  1162. cks = 9
  1163. cks = cks + Val("&H" & Mid$(r0.ToString("x8"), 1, 2))
  1164. If cks > 256 Then cks = cks - 256
  1165. cks = cks + Val("&H" & Mid$(r0.ToString("x8"), 3, 2))
  1166. If cks > 256 Then cks = cks - 256
  1167. cks = cks + Val("&H" & Mid$(r0.ToString("x8"), 5, 2))
  1168. If cks > 256 Then cks = cks - 256
  1169. cks = cks + Val("&H" & Mid$(r0.ToString("x8"), 7, 2))
  1170. If cks > 256 Then cks = cks - 256
  1171. cks = cks + r1
  1172. If cks > 256 Then cks = cks - 256
  1173. cks = cks + r2
  1174. If cks > 256 Then cks = cks - 256
  1175. cks = cks + r3
  1176. If cks > 256 Then cks = cks - 256
  1177. cks = cks + r4
  1178. If cks > 256 Then cks = cks - 256
  1179. If cks <> 0 Then
  1180. cks = 255 - cks
  1181. If cks = &HFFFFFFFF Then cks = &HFF ' This will fix VB2008 bug
  1182. End If
  1183. ' generate and write the record
  1184. s_record = "S309" & r0.ToString("x8") & r1.ToString("x2") & r2.ToString("x2") & r3.ToString("x2") & r4.ToString("x2") & cks.ToString("x2") & "cr"
  1185. b = StrToByteArray(s_record)
  1186. l = Len(s_record)
  1187. ' add crlf
  1188. b(l - 2) = 13
  1189. b(l - 1) = 10
  1190. ' write to file
  1191. fs.Write(b, 0, l)
  1192. Next
  1193. fs.Close()
  1194. ' start the programmer, first startup may require setting up the program
  1195. ' just change first directory to FDT directory
  1196. path = FDTpath & FDTfile
  1197. curpath = Application.StartupPath
  1198. ChDir(FDTpath)
  1199. shpath = "FDT.exe /DISCRETESTARTUP ""SimpleInterfaceMode /r /u " & MOTfile ' use either MOTfile or FDTfile
  1200. Shell(shpath, AppWinStyle.NormalFocus, True, -1)
  1201. ChDir(curpath)
  1202. ' dont delete the temporary file, in case system crashes it remains there for returnging it
  1203. 'If File.Exists(path) = True Then
  1204. 'File.Delete(path)
  1205. 'End If
  1206. '
  1207. ' Generate a backup copy of the flashed file just flashed
  1208. '
  1209. FDTfile = ECUID.Text & "-"
  1210. FDTfile = FDTfile & My.Computer.Clock.LocalTime.Date.Day & "-"
  1211. FDTfile = FDTfile & My.Computer.Clock.LocalTime.Date.Month & "-"
  1212. FDTfile = FDTfile & My.Computer.Clock.LocalTime.Date.Year & "-"
  1213. FDTfile = FDTfile & My.Computer.Clock.LocalTime.Hour & "-"
  1214. FDTfile = FDTfile & My.Computer.Clock.LocalTime.Minute
  1215. FDTfile = FDTfile & ".bin"
  1216. path = FDTpath & FDTfile
  1217. ' if the temporary file exists for any reason, delete it
  1218. If File.Exists(path) = True Then
  1219. File.Delete(path)
  1220. End If
  1221. ' create the temporary file from curren memory
  1222. fs = File.Open(path, FileMode.CreateNew)
  1223. fs.Write(Flash, 0, 262144)
  1224. fs.Close()
  1225. CleanUpFDTDirectory(FDTpath)
  1226. End If
  1227. End Sub
  1228. Private Sub FlashSerial()
  1229. Dim path As String
  1230. Dim flashfile As String
  1231. Dim binfile As String
  1232. Dim binpath As String
  1233. Dim cb(4) As Byte
  1234. Dim FT_status As Long
  1235. Dim lngHandle As Long
  1236. Dim rxbyte, txbyte As Byte
  1237. Dim rxqueue, txqueue, eventstat As Integer
  1238. Dim i, x, y, comportnum As Integer
  1239. Dim ACK As Integer = &H6
  1240. Dim NAK As Integer = &H15
  1241. Dim block As Integer
  1242. Dim cp As Integer
  1243. Dim buff(&HFF) As Byte
  1244. Dim j As Integer
  1245. Dim k As Integer
  1246. Dim blk5 As Boolean
  1247. Dim startaddr As Integer
  1248. Dim modemstat As Integer
  1249. Dim im, chksumflash, chksum As Long
  1250. Dim blkF As Boolean
  1251. Dim loopuntilack As Boolean
  1252. Dim loopcount As Integer
  1253. Dim do_once As Boolean
  1254. Dim starttime As Date
  1255. Dim endtime As Date
  1256. Dim totaltime As TimeSpan
  1257. endtime = Date.Now
  1258. starttime = Date.Now
  1259. totaltime = endtime.Subtract(starttime)
  1260. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  1261. K8FlashStatus.Show()
  1262. K8FlashStatus.Progressbar_Flashstatus.Maximum = &HFF
  1263. K8FlashStatus.Progressbar_Flashstatus.Value = 1
  1264. K8FlashStatus.fmode.ForeColor = Color.DarkGray
  1265. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  1266. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  1267. K8FlashStatus.Refresh()
  1268. System.Windows.Forms.Application.DoEvents()
  1269. '
  1270. ' Lets calculate checksum for the bin
  1271. '
  1272. chksum = ReadFlashWord(&HFFFF8) 'old checksum
  1273. WriteFlashWord(&HFFFF8, 0)
  1274. For im = 0 To &HFFFFF
  1275. If k = 0 Then
  1276. chksumflash = chksumflash + (Flash(im) * &H100)
  1277. k = 1
  1278. Else
  1279. k = 0
  1280. chksumflash = chksumflash + Flash(im)
  1281. End If
  1282. If chksumflash > &HFFFF Then
  1283. chksumflash = chksumflash - &H10000
  1284. End If
  1285. Next
  1286. chksumflash = (&H5AA5 - chksumflash) And &HFFFF
  1287. WriteFlashWord(&HFFFF8, chksumflash) 'new checksum to written to .bin
  1288. If K8Datastream.Visible() Then
  1289. K8Datastream.closeenginedatacomms()
  1290. End If
  1291. K8EngineDataViewer.Close()
  1292. K8EngineDataLogger.Close()
  1293. '
  1294. ' Get the FTDI device handle based on com port number and leave that port open
  1295. '
  1296. B_FlashECU.Enabled = False ' can not restart while flashing active
  1297. timeBeginPeriod(1)
  1298. comportnum = Val(Mid$(My.Settings.Item("ComPort"), 4))
  1299. FT_status = FT_GetNumberOfDevices(i, 0, &H80000000)
  1300. i = i - 1
  1301. For x = 0 To i
  1302. FT_status = FT_Open(x, lngHandle) ' only one
  1303. FT_status = FT_GetComPortNumber(lngHandle, y)
  1304. If y = comportnum Then
  1305. cp = x
  1306. x = i
  1307. End If
  1308. FT_status = FT_Close(lngHandle)
  1309. Next
  1310. If FT_status <> 0 Then
  1311. MsgBox("Could not open com port, please set correct port on K8 enginedata screen. Programming aborted, reset ecu and reprogram")
  1312. B_FlashECU.Enabled = True
  1313. FT_status = FT_Close(lngHandle)
  1314. K8FlashStatus.Close()
  1315. Return
  1316. End If
  1317. '
  1318. ' Open, Reset, set timeouts and set baud rate
  1319. '
  1320. FT_status = FT_Open(cp, lngHandle)
  1321. FT_status = FT_ResetDevice(lngHandle, 3) 'set device to default status
  1322. FT_status = FT_status + FT_Purge(lngHandle) 'clear rx and tx buffers
  1323. FT_status = FT_status + FT_SetBaudRate(lngHandle, 57600) 'set speed 57600
  1324. FT_status = FT_status + FT_SetDataCharacteristics(lngHandle, 8, 1, 0) ' 8bits ,1 stop, parity none
  1325. FT_status = FT_status + FT_SetTimeouts(lngHandle, 50, 50) 'rx and tx timeouts ms
  1326. FT_status = FT_status + FT_SetLatencyTimer(lngHandle, 8) 'ms
  1327. FT_status = FT_status + FT_SetUSBParameters(lngHandle, 4096, 4096) 'only rx is active by FTDI
  1328. If FT_status <> 0 Then
  1329. MsgBox("Could not set Com port parameters. Programming aborted, set correct com port for the interface using data monitoring screen")
  1330. B_FlashECU.Enabled = True
  1331. K8FlashStatus.Close()
  1332. FT_status = FT_Close(lngHandle)
  1333. Return
  1334. End If
  1335. '
  1336. ' Lets test that the interface is in the programming mode
  1337. '
  1338. '***************************************************************************************************************************
  1339. FT_status = FT_SetDtr(lngHandle) 'new for Interface V1.1
  1340. System.Threading.Thread.Sleep(100)
  1341. '****************************************************************************************************************************
  1342. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  1343. If FT_status <> 0 Then
  1344. MsgBox("Set the correct Com port for the interface using data monitoring screen")
  1345. K8FlashStatus.Close()
  1346. B_FlashECU.Enabled = True
  1347. K8FlashStatus.Close()
  1348. Return
  1349. End If
  1350. If Not ((modemstat = &H6000) Or (modemstat = &H6200)) Then
  1351. MsgBox("Interface is not on or it is not in programming mode, set programming switch to programming mode and retry")
  1352. K8FlashStatus.Close()
  1353. B_FlashECU.Enabled = True
  1354. '***************************************************************************************************************************
  1355. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1356. '****************************************************************************************************************************
  1357. FT_status = FT_Close(lngHandle)
  1358. Return
  1359. Else
  1360. '
  1361. ' Reset ecu
  1362. '
  1363. FT_status = FT_SetRts(lngHandle)
  1364. System.Threading.Thread.Sleep(300)
  1365. FT_status = FT_ClrRts(lngHandle)
  1366. System.Threading.Thread.Sleep(300)
  1367. End If
  1368. i = 0
  1369. rxqueue = 0
  1370. '
  1371. ' Sync baud rate with ecu 18 x 0x00, get ack as a reply
  1372. '
  1373. x = 18 'default is 18
  1374. For i = 1 To x
  1375. txbyte = &H0
  1376. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1377. System.Threading.Thread.Sleep(40)
  1378. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1379. If rxqueue <> 0 Then i = x
  1380. Next
  1381. System.Threading.Thread.Sleep(2)
  1382. For x = 1 To rxqueue
  1383. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1384. Next
  1385. If (rxbyte <> ACK) Then
  1386. MsgBox("Unexpected or missing ECU response during intialization. Programming aborted, reset ecu and reprogram." & Hex(rxqueue) & " " & Hex(rxbyte))
  1387. K8FlashStatus.Close()
  1388. B_FlashECU.Enabled = True
  1389. '***************************************************************************************************************************
  1390. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1391. '****************************************************************************************************************************
  1392. FT_status = FT_Close(lngHandle)
  1393. Return
  1394. End If
  1395. '
  1396. ' check key status and send key if necessary
  1397. '
  1398. rxbyte = 0
  1399. i = 0
  1400. rxqueue = 0
  1401. While (rxqueue = 0) And (i < 10)
  1402. txbyte = &H70
  1403. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1404. System.Threading.Thread.Sleep(40)
  1405. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1406. i = i + 1
  1407. End While
  1408. For x = 1 To rxqueue
  1409. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1410. Next
  1411. If rxbyte <> &H8C Then
  1412. txbyte = &HF5
  1413. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1414. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1415. txbyte = &H84
  1416. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1417. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1418. txbyte = &H0
  1419. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1420. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1421. txbyte = &H0
  1422. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1423. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1424. txbyte = &HC
  1425. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1426. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1427. txbyte = &H53
  1428. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1429. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1430. txbyte = &H55
  1431. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1432. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1433. txbyte = &H45
  1434. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1435. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1436. txbyte = &H46
  1437. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1438. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1439. txbyte = &H49
  1440. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1441. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1442. txbyte = &H4D
  1443. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1444. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1445. txbyte = &HFF
  1446. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1447. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1448. txbyte = &HFF
  1449. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1450. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1451. txbyte = &HFF
  1452. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1453. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1454. txbyte = &HFF
  1455. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1456. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1457. txbyte = &H56
  1458. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1459. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1460. txbyte = &H30
  1461. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1462. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1463. '
  1464. ' Receive ACK if unlock code succesfull
  1465. '
  1466. System.Threading.Thread.Sleep(100)
  1467. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1468. For i = 1 To rxqueue
  1469. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1470. Next
  1471. If rxbyte <> ACK Then
  1472. MsgBox("No ACK received after sending unlock code. Programming aborted, reset ecu and reprogram")
  1473. K8FlashStatus.Close()
  1474. B_FlashECU.Enabled = True
  1475. '***************************************************************************************************************************
  1476. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1477. '****************************************************************************************************************************
  1478. FT_status = FT_Close(lngHandle)
  1479. Return
  1480. End If
  1481. End If
  1482. '
  1483. ' Check status after unlock code
  1484. '
  1485. txqueue = 0
  1486. i = 0
  1487. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1488. System.Threading.Thread.Sleep(50)
  1489. While rxqueue = 0 And i < 10
  1490. txbyte = &H70
  1491. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1492. System.Threading.Thread.Sleep(40)
  1493. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1494. i = i + 1
  1495. End While
  1496. If (i >= 10) Or (rxqueue = 0) Then
  1497. MsgBox("Error in validating the unlock code from ECU. Programming aborted, reset ecu and reprogram")
  1498. K8FlashStatus.Close()
  1499. B_FlashECU.Enabled = True
  1500. '***************************************************************************************************************************
  1501. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1502. '****************************************************************************************************************************
  1503. FT_status = FT_Close(lngHandle)
  1504. Return
  1505. Else
  1506. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '128
  1507. System.Threading.Thread.Sleep(50)
  1508. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1509. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '140
  1510. System.Threading.Thread.Sleep(50)
  1511. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1512. End If
  1513. If (rxbyte <> &H8C) Or (FT_status <> 0) Then
  1514. MsgBox("Was not able to set the ecu key. Programming aborted, reset ecu and reprogram")
  1515. K8FlashStatus.Close()
  1516. B_FlashECU.Enabled = True
  1517. '***************************************************************************************************************************
  1518. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1519. '****************************************************************************************************************************
  1520. FT_status = FT_Close(lngHandle)
  1521. Return
  1522. End If
  1523. '
  1524. ' Clear status register just in case
  1525. '
  1526. txbyte = &H50
  1527. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1528. System.Threading.Thread.Sleep(50)
  1529. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1530. For i = 1 To rxqueue
  1531. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1532. Next
  1533. If rxbyte <> ACK Then
  1534. MsgBox("Status query error 1. Programming aborted, reset ecu and reprogram")
  1535. K8FlashStatus.Close()
  1536. B_FlashECU.Enabled = True
  1537. '***************************************************************************************************************************
  1538. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1539. '****************************************************************************************************************************
  1540. FT_status = FT_Close(lngHandle)
  1541. Return
  1542. End If
  1543. '
  1544. ' New command added, to be tested
  1545. '
  1546. 'timeBeginPeriod(0)
  1547. '
  1548. ' Lets verify that this really is suzuki hayabusa ecu, flashing any other ecu type may damage the ecu and the bike
  1549. '
  1550. System.Threading.Thread.Sleep(100)
  1551. txbyte = &HFF
  1552. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1553. txbyte = &HFF
  1554. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1555. txbyte = &HF
  1556. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1557. System.Threading.Thread.Sleep(100)
  1558. k = 0
  1559. Dim s As String
  1560. s = ""
  1561. For j = 0 To &HFF
  1562. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1563. For i = 1 To rxqueue
  1564. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1565. '
  1566. ' Lets test that the ecu id matches close enought to Hayabusa ecu
  1567. ' if rxbyte = &HFF then its likely that the ecu has been fully erased and can be reflashed
  1568. '
  1569. If (k >= &HF0) And (k <= &HF5) Then
  1570. If (rxbyte <> ReadFlashByte(&HFFF00 + k)) And (rxbyte <> &HFF) Then
  1571. If MsgBox("Not same ECU ID in memory and inside the ecu. You can stop the flashing by pressing cancel.", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
  1572. K8FlashStatus.Close()
  1573. B_FlashECU.Enabled = True
  1574. '***************************************************************************************************************************
  1575. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1576. '****************************************************************************************************************************
  1577. FT_status = FT_Close(lngHandle)
  1578. End If
  1579. End If
  1580. End If
  1581. '
  1582. ' Commented out 7.1.2011, new ecutypes added - this check is no more validl
  1583. '
  1584. 'If (k >= &HF6) And (k <= &HF7) Then
  1585. ' If (rxbyte <> &H30) And (rxbyte <> &H31) And (rxbyte <> &H32) And (rxbyte <> &H35) And (rxbyte <> &HFF) Then
  1586. ' If MsgBox("Not a Hayabusa 15H00, 15H10, 15H20 or 15Hxx Generic ecu. Programming stopped to avoid damage to ecu or bike. Press cancel to stop, ok to continue", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
  1587. ' K8FlashStatus.Close()
  1588. ' B_FlashECU.Enabled = True
  1589. ' '***************************************************************************************************************************
  1590. ' FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1591. ' '****************************************************************************************************************************
  1592. ' FT_status = FT_Close(lngHandle)
  1593. ' End If
  1594. ' End If
  1595. ' End If
  1596. k = k + 1
  1597. Next
  1598. Next
  1599. '
  1600. ' Lets read what is the flashingmode in ecu if memory is set to fast flashmode
  1601. ' if fastflash then...
  1602. '
  1603. If ReadFlashLongWord(&H51F10) = &H536C4 Then
  1604. System.Threading.Thread.Sleep(100)
  1605. txbyte = &HFF
  1606. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1607. txbyte = &H1F
  1608. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1609. txbyte = &H5
  1610. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1611. System.Threading.Thread.Sleep(100)
  1612. k = 0
  1613. For j = 0 To &HFF
  1614. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1615. For i = 1 To rxqueue
  1616. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1617. If k = 19 Then
  1618. i = ReadFlashByte(&H51F10 + 3)
  1619. Select Case rxbyte
  1620. Case &H18 ' stock setting not fastmode, reflash block 5 is memory in fast mode
  1621. If ReadFlashLongWord(&H51F10) = &H536C4 Then
  1622. blk5 = True
  1623. Else
  1624. blk5 = False
  1625. End If
  1626. Case &HC4 ' ecu already in fast mode, no reflashing is needed
  1627. blk5 = False
  1628. Case &HFF ' block5 is empty, may be reflashing error. reflash block 5
  1629. blk5 = True
  1630. Case Else
  1631. MsgBox("Error in reading flashingmode from ECU, programming aborted. Please reboot ecu and reflash")
  1632. BlockPgm = True
  1633. K8FlashStatus.Close()
  1634. B_FlashECU.Enabled = True
  1635. '***************************************************************************************************************************
  1636. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1637. '****************************************************************************************************************************
  1638. FT_status = FT_Close(lngHandle)
  1639. End Select
  1640. End If
  1641. k = k + 1
  1642. Next
  1643. Next
  1644. End If
  1645. timeBeginPeriod(1)
  1646. System.Threading.Thread.Sleep(300)
  1647. '
  1648. ' For a reason or another block 0 requires full erase
  1649. '
  1650. If BlockChanged(0) = True Then
  1651. BlockPgm = True
  1652. End If
  1653. blkF = False ' this is just used for check sum testing
  1654. '
  1655. ' Here is an erase for the full ecu
  1656. '
  1657. If BlockPgm Then
  1658. endtime = Date.Now
  1659. totaltime = endtime.Subtract(starttime)
  1660. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  1661. K8FlashStatus.fmode.Text = "Performing full erase, please wait"
  1662. K8FlashStatus.fmode.ForeColor = Color.Gray
  1663. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  1664. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  1665. K8FlashStatus.Refresh()
  1666. System.Windows.Forms.Application.DoEvents()
  1667. '
  1668. ' Send Erase full memory command
  1669. '
  1670. txbyte = &HA7
  1671. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1672. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1673. txbyte = &HD0
  1674. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1675. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1676. '
  1677. ' ECU confirms a succesfull erase by sending ACK
  1678. '
  1679. loopcount = 0
  1680. loopuntilack = False
  1681. While Not loopuntilack
  1682. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1683. While (rxqueue = 0) And (i < 100)
  1684. System.Threading.Thread.Sleep(50)
  1685. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1686. i = i + 1
  1687. End While
  1688. For i = 1 To rxqueue
  1689. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1690. If rxbyte = ACK Then
  1691. loopuntilack = True
  1692. End If
  1693. Next
  1694. If loopcount > 10 Then
  1695. '
  1696. ' Clear program lock bit and status register
  1697. '
  1698. txbyte = &H75
  1699. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1700. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1701. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1702. txbyte = &H50
  1703. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1704. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1705. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1706. End If
  1707. If loopcount > 20 Then
  1708. MsgBox("No ACK after full erase, Programming aborted, reset ecu and reprogram.")
  1709. K8FlashStatus.Close()
  1710. B_FlashECU.Enabled = True
  1711. '***************************************************************************************************************************
  1712. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1713. '****************************************************************************************************************************
  1714. FT_status = FT_Close(lngHandle)
  1715. BlockPgm = True
  1716. Return
  1717. End If
  1718. loopcount = loopcount + 1
  1719. End While
  1720. End If
  1721. '
  1722. ' Now programmings starts
  1723. '
  1724. j = ReadFlashLongWord(&H51F10)
  1725. If ReadFlashLongWord(&H51F10) <> &H536C4 Then
  1726. K8FlashStatus.fmode.Text = "Normal flash "
  1727. Else
  1728. K8FlashStatus.fmode.Text = "Fast flash "
  1729. End If
  1730. K8FlashStatus.fmode.ForeColor = Color.Black
  1731. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  1732. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  1733. K8FlashStatus.Refresh()
  1734. System.Windows.Forms.Application.DoEvents()
  1735. startaddr = 0
  1736. For block = startaddr To &HF
  1737. ' BlockChanged returns true if there has been any changes to that block
  1738. ' BlockPgm is a global variable that forces all blocks to be written
  1739. If BlockChanged(block) Or BlockPgm Then
  1740. endtime = Date.Now
  1741. totaltime = endtime.Subtract(starttime)
  1742. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  1743. K8FlashStatus.fmode.Text = K8FlashStatus.fmode.Text & Hex(block)
  1744. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  1745. K8FlashStatus.Refresh()
  1746. System.Windows.Forms.Application.DoEvents()
  1747. If block = &HF Then blkF = True
  1748. '
  1749. ' Erase block
  1750. '
  1751. txbyte = &H20
  1752. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1753. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1754. txbyte = &H0
  1755. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1756. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1757. txbyte = block
  1758. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1759. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1760. txbyte = &HD0
  1761. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1762. '
  1763. ' ECU confirms a succesfull erase by sending ACK
  1764. '
  1765. loopcount = 0
  1766. loopuntilack = False
  1767. While Not loopuntilack
  1768. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1769. i = 0
  1770. While (rxqueue = 0) And (i < 100)
  1771. System.Threading.Thread.Sleep(50)
  1772. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1773. i = i + 1
  1774. End While
  1775. For i = 1 To rxqueue
  1776. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1777. If rxbyte = ACK Then loopuntilack = True
  1778. Next
  1779. If rxbyte = NAK Then
  1780. '
  1781. ' Lets inform the user that something is wrong
  1782. '
  1783. K8FlashStatus.fmode.ForeColor = Color.Orange
  1784. '
  1785. ' Clear status register
  1786. '
  1787. System.Threading.Thread.Sleep(200)
  1788. txbyte = &H50
  1789. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1790. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1791. i = 0
  1792. While (rxqueue = 0) And (i < 10)
  1793. System.Threading.Thread.Sleep(50)
  1794. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1795. i = i + 1
  1796. End While
  1797. For i = 1 To rxqueue
  1798. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1799. Next
  1800. '
  1801. ' Disable lock bit
  1802. '
  1803. System.Threading.Thread.Sleep(200)
  1804. txbyte = &H75
  1805. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1806. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1807. i = 0
  1808. While (rxqueue = 0) And (i < 10)
  1809. System.Threading.Thread.Sleep(50)
  1810. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1811. i = i + 1
  1812. End While
  1813. For i = 1 To rxqueue
  1814. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1815. Next
  1816. '
  1817. ' Erase block again
  1818. '
  1819. System.Threading.Thread.Sleep(200)
  1820. txbyte = &H20
  1821. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1822. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1823. txbyte = &H0
  1824. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1825. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1826. txbyte = block
  1827. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1828. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1829. txbyte = &HD0
  1830. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1831. System.Threading.Thread.Sleep(200)
  1832. End If
  1833. If loopcount > 100 Then
  1834. MsgBox("No ACK after erasing a block=" & Str(block) & " Programming aborted, reset ecu and reprogram")
  1835. K8FlashStatus.Close()
  1836. B_FlashECU.Enabled = True
  1837. '***************************************************************************************************************************
  1838. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1839. '****************************************************************************************************************************
  1840. FT_status = FT_Close(lngHandle)
  1841. BlockPgm = True
  1842. Return
  1843. End If
  1844. endtime = Date.Now
  1845. totaltime = endtime.Subtract(starttime)
  1846. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  1847. K8FlashStatus.L_elapsedtime.Text = TimeOfDay
  1848. K8FlashStatus.Progressbar_Flashstatus.Value = loopcount
  1849. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  1850. K8FlashStatus.Refresh()
  1851. System.Windows.Forms.Application.DoEvents()
  1852. loopcount = loopcount + 1
  1853. End While
  1854. rxqueue = 0
  1855. i = 0
  1856. '
  1857. ' Write block using page write
  1858. '
  1859. K8FlashStatus.fmode.ForeColor = Color.Black
  1860. Dim page As Integer
  1861. For page = 0 To &HFF
  1862. '
  1863. ' Write one page at time
  1864. '
  1865. endtime = Date.Now
  1866. totaltime = endtime.Subtract(starttime)
  1867. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  1868. K8FlashStatus.Progressbar_Flashstatus.Value = page
  1869. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  1870. K8FlashStatus.Refresh()
  1871. System.Windows.Forms.Application.DoEvents()
  1872. '
  1873. ' Check if the page is filled with 0xFF, no need to program
  1874. '
  1875. i = 0
  1876. For y = 0 To &HFF
  1877. buff(y) = ReadFlashByte((block * &H10000) + (page * &H100) + y)
  1878. If buff(y) <> &HFF Then
  1879. i = i + 1
  1880. End If
  1881. Next
  1882. If i > 0 Then ' there is something in the page that is not 0xFF
  1883. loopcount = 0
  1884. loopuntilack = False
  1885. While Not loopuntilack
  1886. '
  1887. ' write a page
  1888. '
  1889. txbyte = &H41
  1890. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1891. txbyte = page
  1892. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1893. txbyte = block
  1894. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1895. For y = 0 To &HFF
  1896. txbyte = buff(y)
  1897. 'FT_status = FT_Write_Bytes(lngHandle, txbyte, 1, txqueue)
  1898. Next
  1899. FT_status = FT_Write(lngHandle, buff, &HFF + 1, &HFF + 1)
  1900. '
  1901. ' this should be ack from page write
  1902. '
  1903. rxbyte = 0
  1904. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1905. i = 0
  1906. While (rxqueue = 0) And (i < 30)
  1907. System.Threading.Thread.Sleep(25)
  1908. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1909. If rxqueue > 0 Then i = 30
  1910. i = i + 1
  1911. End While
  1912. For i = 1 To rxqueue
  1913. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1914. If rxbyte = ACK Then loopuntilack = True
  1915. Next
  1916. If loopcount > 5 Then
  1917. '
  1918. ' Clear program lock bit and status register
  1919. '
  1920. K8FlashStatus.fmode.ForeColor = Color.Orange
  1921. K8FlashStatus.Refresh()
  1922. System.Windows.Forms.Application.DoEvents()
  1923. System.Threading.Thread.Sleep(100)
  1924. txbyte = &H75
  1925. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1926. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1927. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1928. System.Threading.Thread.Sleep(100)
  1929. rxbyte = 0
  1930. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1931. i = 0
  1932. While (rxqueue = 0) And (i < 30)
  1933. System.Threading.Thread.Sleep(25)
  1934. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1935. If rxqueue > 0 Then i = 30
  1936. i = i + 1
  1937. End While
  1938. For i = 1 To rxqueue
  1939. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1940. Next
  1941. txbyte = &H50
  1942. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1943. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1944. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1945. System.Threading.Thread.Sleep(100)
  1946. rxbyte = 0
  1947. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1948. i = 0
  1949. While (rxqueue = 0) And (i < 30)
  1950. System.Threading.Thread.Sleep(25)
  1951. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1952. If rxqueue > 0 Then i = 30
  1953. i = i + 1
  1954. End While
  1955. For i = 1 To rxqueue
  1956. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1957. Next
  1958. End If
  1959. If loopcount > 10 Then
  1960. MsgBox("No ACK after writing a block=" & Str(block) & " page=" & Str(page) & ". Programming aborted, reset ecu and reprogram")
  1961. K8FlashStatus.Close()
  1962. B_FlashECU.Enabled = True
  1963. '***************************************************************************************************************************
  1964. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  1965. '****************************************************************************************************************************
  1966. FT_status = FT_Close(lngHandle)
  1967. BlockPgm = True
  1968. Return
  1969. End If
  1970. loopcount = loopcount + 1
  1971. End While
  1972. End If
  1973. Next
  1974. End If
  1975. Next
  1976. '
  1977. ' Acquire sum value and compare checksum. This will be done every time as flashing always
  1978. ' starts by writing a new checksum to the image in computer memory
  1979. '
  1980. txbyte = &HE1
  1981. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1982. txbyte = 0
  1983. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1984. txbyte = 0
  1985. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1986. txbyte = &HFF
  1987. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1988. txbyte = &HF
  1989. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  1990. System.Threading.Thread.Sleep(200)
  1991. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  1992. If rxqueue <> 2 Then MsgBox("Error in reading checksum from ecu")
  1993. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1994. k = rxbyte
  1995. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  1996. k = (k) + (rxbyte * &H100)
  1997. '
  1998. ' Flashing is finished, wait until switch is flipped back and then close com and activate enginedata if visible
  1999. '
  2000. do_once = True
  2001. timeEndPeriod(1)
  2002. If k <> &H5AA5 Then
  2003. MsgBox("Checksum error when validating the flash, please reflash your ecu before using it.")
  2004. K8FlashStatus.fmode.Text = "Checksum error, please reprogram"
  2005. ResetBlocks()
  2006. BlockPgm = True
  2007. Else
  2008. K8FlashStatus.fmode.Text = "Flash OK, turn switch to enginedata"
  2009. ResetBlocks()
  2010. BlockPgm = False
  2011. End If
  2012. endtime = Date.Now
  2013. totaltime = endtime.Subtract(starttime)
  2014. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  2015. K8FlashStatus.Refresh()
  2016. System.Windows.Forms.Application.DoEvents()
  2017. '***************************************************************************************************************************
  2018. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  2019. System.Threading.Thread.Sleep(100)
  2020. '****************************************************************************************************************************
  2021. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2022. While ((modemstat = &H6000) Or (modemstat = &H6200))
  2023. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2024. System.Threading.Thread.Sleep(200)
  2025. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2026. End While
  2027. K8FlashStatus.Close()
  2028. B_FlashECU.Enabled = True
  2029. 'FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  2030. FT_status = FT_Close(lngHandle)
  2031. If (FT_status = 0) Then
  2032. If K8Datastream.Visible() Then
  2033. K8Datastream.startenginedatacomms()
  2034. End If
  2035. Else
  2036. MsgBox("Can not close com port, please save the bin and reboot your computer and reflash just in case.")
  2037. End If
  2038. '
  2039. ' generate the bin that was flashed as a backup file
  2040. '
  2041. flashfile = "ecuflash.bin"
  2042. binpath = Application.StartupPath
  2043. binfile = "ecuflash.bin"
  2044. path = binpath & flashfile
  2045. ' if the temporary file exists for any reason, delete it
  2046. If File.Exists(path) = True Then
  2047. File.Delete(path)
  2048. End If
  2049. ' create the temporary file from current memory
  2050. fs = File.Open(path, FileMode.CreateNew)
  2051. fs.Write(Flash, 0, (262144 * 4))
  2052. fs.Close()
  2053. '
  2054. ' Generate a backup copy of the flashed file just flashed
  2055. '
  2056. binfile = ECUID.Text & "-"
  2057. binfile = binfile & My.Computer.Clock.LocalTime.Date.Day & "-"
  2058. binfile = binfile & My.Computer.Clock.LocalTime.Date.Month & "-"
  2059. binfile = binfile & My.Computer.Clock.LocalTime.Date.Year & "-"
  2060. binfile = binfile & My.Computer.Clock.LocalTime.Hour & "-"
  2061. binfile = binfile & My.Computer.Clock.LocalTime.Minute
  2062. binfile = binfile & ".bin"
  2063. path = binpath & binfile
  2064. ' if the temporary file exists for any reason, delete it
  2065. If File.Exists(path) = True Then
  2066. File.Delete(path)
  2067. End If
  2068. ' create the temporary file from curren memory
  2069. fs = File.Open(path, FileMode.CreateNew)
  2070. fs.Write(Flash, 0, (262144 * 4))
  2071. fs.Close()
  2072. ' cleanup of old backup files
  2073. CleanUpFDTDirectory(binpath)
  2074. End Sub
  2075. Private Sub BlockOK(ByVal b As Integer)
  2076. Select Case b
  2077. Case b = 0 : Block0 = False
  2078. Case b = 1 : Block1 = False
  2079. Case b = 2 : Block2 = False
  2080. Case b = 3 : Block3 = False
  2081. Case b = 4 : Block4 = False
  2082. Case b = 5 : Block5 = False
  2083. Case b = 6 : Block6 = False
  2084. Case b = 7 : Block7 = False
  2085. Case b = 8 : Block8 = False
  2086. Case b = 9 : Block9 = False
  2087. Case b = 10 : BlockA = False
  2088. Case b = 11 : BlockB = False
  2089. Case b = 12 : BlockC = False
  2090. Case b = 13 : BlockD = False
  2091. Case b = 14 : BlockE = False
  2092. Case b = 15 : BlockF = False
  2093. End Select
  2094. End Sub
  2095. Sub ReadECU()
  2096. '
  2097. ' This subroutine reads the ecu contents into memory
  2098. '
  2099. Dim cb(4) As Byte
  2100. Dim FT_status As Long
  2101. Dim lngHandle As Long
  2102. Dim rxbyte, txbyte As Byte
  2103. Dim rxqueue, txqueue, eventstat As Integer
  2104. Dim i, x, y, comportnum As Integer
  2105. Dim ACK As Integer = &H6
  2106. Dim NAK As Integer = &H15
  2107. Dim cp As Integer
  2108. Dim buff(&HFF) As Byte
  2109. Dim j As Integer
  2110. Dim k As Integer
  2111. Dim modemstat As Integer
  2112. Dim midorder, highorder As Integer
  2113. Dim imagelength, im As Long
  2114. Dim imageidentical As Boolean
  2115. Dim diffstr As String
  2116. Dim chksum As Integer
  2117. Dim chksumflash As Long
  2118. '
  2119. ' Get the FTDI device handle based on com port number and leave that port open
  2120. '
  2121. timeBeginPeriod(1)
  2122. comportnum = Val(Mid$(My.Settings.Item("ComPort"), 4))
  2123. FT_status = FT_GetNumberOfDevices(i, 0, &H80000000)
  2124. i = i - 1
  2125. For x = 0 To i
  2126. FT_status = FT_Open(x, lngHandle) ' only one
  2127. FT_status = FT_GetComPortNumber(lngHandle, y)
  2128. If y = comportnum Then
  2129. cp = x
  2130. x = i
  2131. End If
  2132. FT_status = FT_Close(lngHandle)
  2133. Next
  2134. If FT_status <> 0 Then
  2135. MsgBox("Could not open com port, please set correct port on K8 enginedata screen. Verfiy aborted")
  2136. FT_status = FT_Close(lngHandle)
  2137. Return
  2138. End If
  2139. '
  2140. ' Open, Reset, set timeouts and set baud rate
  2141. '
  2142. FT_status = FT_Open(cp, lngHandle)
  2143. FT_status = FT_ResetDevice(lngHandle, 3) 'set device to default status
  2144. FT_status = FT_status + FT_Purge(lngHandle) 'clear rx and tx buffers
  2145. FT_status = FT_status + FT_SetBaudRate(lngHandle, 57600) 'set speed 57600
  2146. FT_status = FT_status + FT_SetDataCharacteristics(lngHandle, 8, 1, 0) ' 8bits ,1 stop, parity none
  2147. FT_status = FT_status + FT_SetTimeouts(lngHandle, 5, 5) 'rx and tx timeouts ms
  2148. If FT_status <> 0 Then
  2149. MsgBox("Could not set Com port parameters. Verify aborted, set correct com port for the interface using data monitoring screen")
  2150. FT_status = FT_Close(lngHandle)
  2151. Return
  2152. End If
  2153. '
  2154. ' Lets test that the interface is in the programming mode
  2155. '
  2156. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2157. If FT_status <> 0 Then
  2158. MsgBox("Set the correct Com port for the interface using data monitoring screen")
  2159. Return
  2160. End If
  2161. If Not ((modemstat = &H6000) Or (modemstat = &H6200)) Then
  2162. MsgBox("Interface is not on or it is not in programming mode, set programming switch to programming mode and retry")
  2163. FT_status = FT_Close(lngHandle)
  2164. Return
  2165. Else
  2166. '
  2167. ' Reset ecu
  2168. '
  2169. FT_status = FT_SetRts(lngHandle)
  2170. System.Threading.Thread.Sleep(300)
  2171. FT_status = FT_ClrRts(lngHandle)
  2172. System.Threading.Thread.Sleep(300)
  2173. End If
  2174. i = 0
  2175. rxqueue = 0
  2176. '
  2177. ' Sync baud rate with ecu 18 x 0x00, get ack as a reply
  2178. '
  2179. x = 18 'default is 18
  2180. For i = 1 To x
  2181. txbyte = &H0
  2182. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2183. System.Threading.Thread.Sleep(40)
  2184. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2185. If rxqueue <> 0 Then i = x
  2186. Next
  2187. System.Threading.Thread.Sleep(2)
  2188. For x = 1 To rxqueue
  2189. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2190. Next
  2191. If (rxbyte <> ACK) Then
  2192. MsgBox("Unexpected or missing ECU response during intialization. Verify aborted, reset ecu and retry." & Hex(rxqueue) & " " & Hex(rxbyte))
  2193. FT_status = FT_Close(lngHandle)
  2194. Return
  2195. End If
  2196. '
  2197. ' check key status and send key if necessary
  2198. '
  2199. rxbyte = 0
  2200. i = 0
  2201. rxqueue = 0
  2202. While (rxqueue = 0) And (i < 10)
  2203. txbyte = &H70
  2204. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2205. System.Threading.Thread.Sleep(40)
  2206. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2207. i = i + 1
  2208. End While
  2209. For x = 1 To rxqueue
  2210. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2211. Next
  2212. If rxbyte <> &H8C Then
  2213. txbyte = &HF5
  2214. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2215. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2216. txbyte = &H84
  2217. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2218. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2219. txbyte = &H0
  2220. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2221. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2222. txbyte = &H0
  2223. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2224. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2225. txbyte = &HC
  2226. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2227. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2228. txbyte = &H53
  2229. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2230. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2231. txbyte = &H55
  2232. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2233. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2234. txbyte = &H45
  2235. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2236. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2237. txbyte = &H46
  2238. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2239. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2240. txbyte = &H49
  2241. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2242. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2243. txbyte = &H4D
  2244. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2245. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2246. txbyte = &HFF
  2247. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2248. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2249. txbyte = &HFF
  2250. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2251. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2252. txbyte = &HFF
  2253. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2254. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2255. txbyte = &HFF
  2256. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2257. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2258. txbyte = &H56
  2259. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2260. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2261. txbyte = &H30
  2262. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2263. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2264. '
  2265. ' Receive ACK if unlock code succesfull
  2266. '
  2267. System.Threading.Thread.Sleep(100)
  2268. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2269. For i = 1 To rxqueue
  2270. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2271. Next
  2272. If rxbyte <> ACK Then
  2273. MsgBox("No ACK received after sending unlock code. Verify aborted, reset ecu and reprogram")
  2274. FT_status = FT_Close(lngHandle)
  2275. Return
  2276. End If
  2277. End If
  2278. '
  2279. ' Check status after unlock code
  2280. '
  2281. txqueue = 0
  2282. i = 0
  2283. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2284. System.Threading.Thread.Sleep(50)
  2285. While rxqueue = 0 And i < 10
  2286. txbyte = &H70
  2287. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2288. System.Threading.Thread.Sleep(40)
  2289. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2290. i = i + 1
  2291. End While
  2292. If (i >= 10) Or (rxqueue = 0) Then
  2293. MsgBox("Error in validating the unlock code from ECU. Verify aborted, reset ecu and reprogram")
  2294. FT_status = FT_Close(lngHandle)
  2295. Return
  2296. Else
  2297. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '128
  2298. System.Threading.Thread.Sleep(50)
  2299. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2300. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '140
  2301. System.Threading.Thread.Sleep(50)
  2302. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2303. End If
  2304. If (rxbyte <> &H8C) Or (FT_status <> 0) Then
  2305. MsgBox("Was not able to set the ecu key. Verify aborted, reset ecu and reprogram")
  2306. FT_status = FT_Close(lngHandle)
  2307. Return
  2308. End If
  2309. timeEndPeriod(1)
  2310. '
  2311. ' Lets read the ecu, &HFF, midorder, highorder -> 256 bytes of data in readbuffer
  2312. ' if the checksum of read bytes and ecu command matches the read page will be then compared
  2313. ' against whats in the memory. Variable flash(i) contains the information read from ecu
  2314. ' the variable flashcopy(i) is the one that the verification against is made to.
  2315. '
  2316. VerifyInProgress.ProgressBar_Verify.Value = 50
  2317. VerifyInProgress.Show()
  2318. imageidentical = True
  2319. k = 0
  2320. imagelength = 0
  2321. diffstr = ""
  2322. 'FT_SetTimeouts(lngHandle, 100, 200)
  2323. For highorder = 0 To &HF
  2324. For midorder = 0 To &HFF
  2325. '
  2326. ' Acquire sum value
  2327. '
  2328. txbyte = &HE1
  2329. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2330. txbyte = midorder
  2331. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2332. txbyte = highorder
  2333. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2334. txbyte = midorder
  2335. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2336. txbyte = highorder
  2337. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2338. System.Threading.Thread.Sleep(100)
  2339. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2340. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2341. k = rxbyte
  2342. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2343. chksum = (k) + (rxbyte * &H100)
  2344. '
  2345. ' Read Page until checksum match
  2346. '
  2347. chksumflash = -1
  2348. im = imagelength
  2349. While chksumflash <> chksum
  2350. '
  2351. ' Read page
  2352. '
  2353. txbyte = &HFF
  2354. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2355. txbyte = midorder
  2356. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2357. txbyte = highorder
  2358. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2359. Dim s As String
  2360. s = ""
  2361. j = 0
  2362. k = 0
  2363. chksumflash = 0
  2364. System.Threading.Thread.Sleep(100)
  2365. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2366. For i = 1 To rxqueue
  2367. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2368. Flash(imagelength) = rxbyte
  2369. If k = 0 Then
  2370. chksumflash = chksumflash + (rxbyte * &H100)
  2371. k = 1
  2372. Else
  2373. k = 0
  2374. chksumflash = chksumflash + rxbyte
  2375. End If
  2376. If chksumflash > &HFFFF Then
  2377. chksumflash = chksumflash - &H10000
  2378. End If
  2379. imagelength = imagelength + 1
  2380. Next
  2381. If chksumflash <> chksum Then
  2382. '
  2383. ' Block is being reread, lets make a note of it and also return the memory counter back to previous state
  2384. '
  2385. If Len(diffstr) < 100 Then diffstr = diffstr & "R"
  2386. imagelength = im
  2387. System.Threading.Thread.Sleep(100)
  2388. End If
  2389. End While
  2390. '
  2391. ' Now block is read and we can check if the block contents is identical in flashcopy as read from ecu
  2392. '
  2393. For j = 0 To &HFF
  2394. If Flash((midorder * &H100) + (highorder * &H10000) + j) <> FlashCopy((midorder * &H100) + (highorder * &H10000) + j) Then
  2395. imageidentical = False
  2396. If Len(diffstr) < 100 Then diffstr = diffstr & " " & Hex((midorder * &H100) + (highorder * &H10000) + j)
  2397. End If
  2398. Next
  2399. VerifyInProgress.Select()
  2400. VerifyInProgress.L_Txt.Text = Hex(highorder) & " " & Hex(midorder) & " - " & imageidentical & " " & diffstr & " "
  2401. VerifyInProgress.ProgressBar_Verify.Value = Int(((highorder * &HF) + Int(midorder / &HF)) / 2.56)
  2402. VerifyInProgress.Refresh()
  2403. System.Windows.Forms.Application.DoEvents()
  2404. Next
  2405. Next
  2406. VerifyInProgress.Close()
  2407. '
  2408. ' All done, close comms
  2409. '
  2410. B_FlashECU.Enabled = True
  2411. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2412. If FT_status = 0 Then
  2413. FT_status = FT_Close(lngHandle)
  2414. Else
  2415. MsgBox("Can not close com port, please reboot your ecu and computer and reread just in case.")
  2416. End If
  2417. '
  2418. ' Here add the filename etc stuff to reflect that new bin has been loaded
  2419. '
  2420. L_File.Text = ""
  2421. L_Comparefile.Text = ""
  2422. DisableButtons()
  2423. If imageidentical Then
  2424. MsgBox("Ecu verify complete, image same as comparemap. Ecu image is in ecueditor memory.")
  2425. ResetBlocks()
  2426. BlockPgm = False
  2427. Else
  2428. '
  2429. ' Check that the binary lenght matches just in case
  2430. '
  2431. If imagelength <> (262144 * 4) Then
  2432. ECUNotSupported.ShowDialog()
  2433. End If
  2434. '
  2435. ' Same lenght, just inform the user that he has a new bin in the memory
  2436. '
  2437. MsgBox("Ecu image is not same as comparemap. Ecu image copied into ecueditor memory.", MsgBoxStyle.Exclamation)
  2438. BlockPgm = True
  2439. End If
  2440. '
  2441. ' Make sure the ECU id is supported type
  2442. '
  2443. i = 0
  2444. ECUID.Text = ""
  2445. Do While i < 8
  2446. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  2447. i = i + 1
  2448. Loop
  2449. ECUVersion = ""
  2450. SetECUType()
  2451. If ECUVersion = "" Then
  2452. Limiters.C_RPM.Enabled = False
  2453. B_Limiters.Enabled = False
  2454. B_Shifter.Enabled = False
  2455. B_FuelMap.Enabled = False
  2456. B_IgnitionMap.Enabled = False
  2457. B_AdvancedSettings.Enabled = False
  2458. B_DataLogging.Enabled = True
  2459. B_FlashECU.Enabled = False
  2460. SaveToolStripMenuItem.Enabled = True
  2461. MsgBox("ECU read into memory, but not recognized. please save as" & ECUID.Text & ".bin and send to info@ecueditor.com with notes about the bike and model.")
  2462. Else
  2463. Limiters.C_RPM.Enabled = True
  2464. SaveToolStripMenuItem.Enabled = True
  2465. B_FlashECU.Enabled = True
  2466. B_Limiters.Enabled = True
  2467. B_Shifter.Enabled = True
  2468. B_FuelMap.Enabled = True
  2469. B_IgnitionMap.Enabled = True
  2470. B_AdvancedSettings.Enabled = True
  2471. B_DataLogging.Enabled = True
  2472. SaveToolStripMenuItem.Enabled = True
  2473. End If
  2474. End Sub
  2475. Private Sub EraseECU()
  2476. Dim cb(4) As Byte
  2477. Dim FT_status As Long
  2478. Dim lngHandle As Long
  2479. Dim rxbyte, txbyte As Byte
  2480. Dim rxqueue, txqueue, eventstat As Integer
  2481. Dim i, x, y, comportnum As Integer
  2482. Dim ACK As Integer = &H6
  2483. Dim NAK As Integer = &H15
  2484. Dim cp As Integer
  2485. Dim buff(&HFF) As Byte
  2486. Dim modemstat As Integer
  2487. Dim loopuntilack As Boolean
  2488. Dim loopcount As Integer
  2489. Dim do_once As Boolean
  2490. If K8Datastream.Visible() Then
  2491. K8Datastream.closeenginedatacomms()
  2492. End If
  2493. '
  2494. ' Get the FTDI device handle based on com port number and leave that port open
  2495. '
  2496. B_FlashECU.Enabled = False ' can not restart while flashing active
  2497. timeBeginPeriod(1)
  2498. comportnum = Val(Mid$(My.Settings.Item("ComPort"), 4))
  2499. FT_status = FT_GetNumberOfDevices(i, 0, &H80000000)
  2500. i = i - 1
  2501. For x = 0 To i
  2502. FT_status = FT_Open(x, lngHandle) ' only one
  2503. FT_status = FT_GetComPortNumber(lngHandle, y)
  2504. If y = comportnum Then
  2505. cp = x
  2506. x = i
  2507. End If
  2508. FT_status = FT_Close(lngHandle)
  2509. Next
  2510. If FT_status <> 0 Then
  2511. MsgBox("Could not open com port, please set correct port on K8 enginedata screen. Erase aborted, reset ecu and reprogram")
  2512. B_FlashECU.Enabled = True
  2513. FT_status = FT_Close(lngHandle)
  2514. K8FlashStatus.Close()
  2515. Return
  2516. End If
  2517. '
  2518. ' Open, Reset, set timeouts and set baud rate
  2519. '
  2520. FT_status = FT_Open(cp, lngHandle)
  2521. FT_status = FT_ResetDevice(lngHandle, 3) 'set device to default status
  2522. FT_status = FT_status + FT_Purge(lngHandle) 'clear rx and tx buffers
  2523. FT_status = FT_status + FT_SetBaudRate(lngHandle, 57600) 'set speed 57600
  2524. FT_status = FT_status + FT_SetDataCharacteristics(lngHandle, 8, 1, 0) ' 8bits ,1 stop, parity none
  2525. FT_status = FT_status + FT_SetTimeouts(lngHandle, 5, 5) 'rx and tx timeouts ms
  2526. If FT_status <> 0 Then
  2527. MsgBox("Could not set Com port parameters. Programming aborted, set correct com port for the interface using data monitoring screen")
  2528. B_FlashECU.Enabled = True
  2529. K8FlashStatus.Close()
  2530. FT_status = FT_Close(lngHandle)
  2531. Return
  2532. End If
  2533. '
  2534. ' Lets test that the interface is in the programming mode
  2535. '
  2536. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2537. If FT_status <> 0 Then
  2538. MsgBox("Set the correct Com port for the interface using data monitoring screen")
  2539. K8FlashStatus.Close()
  2540. B_FlashECU.Enabled = True
  2541. K8FlashStatus.Close()
  2542. Return
  2543. End If
  2544. If Not ((modemstat = &H6000) Or (modemstat = &H6200)) Then
  2545. MsgBox("Interface is not on or it is not in programming mode, set programming switch to programming mode and retry")
  2546. K8FlashStatus.Close()
  2547. B_FlashECU.Enabled = True
  2548. FT_status = FT_Close(lngHandle)
  2549. Return
  2550. Else
  2551. '
  2552. ' Reset ecu
  2553. '
  2554. FT_status = FT_SetRts(lngHandle)
  2555. System.Threading.Thread.Sleep(300)
  2556. FT_status = FT_ClrRts(lngHandle)
  2557. System.Threading.Thread.Sleep(300)
  2558. End If
  2559. i = 0
  2560. rxqueue = 0
  2561. '
  2562. ' Sync baud rate with ecu 18 x 0x00, get ack as a reply
  2563. '
  2564. x = 18 'default is 18
  2565. For i = 1 To x
  2566. txbyte = &H0
  2567. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2568. System.Threading.Thread.Sleep(40)
  2569. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2570. If rxqueue <> 0 Then i = x
  2571. Next
  2572. System.Threading.Thread.Sleep(2)
  2573. For x = 1 To rxqueue
  2574. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2575. Next
  2576. If (rxbyte <> ACK) Then
  2577. MsgBox("Unexpected or missing ECU response during intialization. Programming aborted, reset ecu and reprogram." & Hex(rxqueue) & " " & Hex(rxbyte))
  2578. K8FlashStatus.Close()
  2579. B_FlashECU.Enabled = True
  2580. FT_status = FT_Close(lngHandle)
  2581. Return
  2582. End If
  2583. '
  2584. ' check key status and send key if necessary
  2585. '
  2586. rxbyte = 0
  2587. i = 0
  2588. rxqueue = 0
  2589. While (rxqueue = 0) And (i < 10)
  2590. txbyte = &H70
  2591. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2592. System.Threading.Thread.Sleep(40)
  2593. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2594. i = i + 1
  2595. End While
  2596. For x = 1 To rxqueue
  2597. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2598. Next
  2599. If rxbyte <> &H8C Then
  2600. txbyte = &HF5
  2601. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2602. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2603. txbyte = &H84
  2604. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2605. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2606. txbyte = &H0
  2607. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2608. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2609. txbyte = &H0
  2610. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2611. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2612. txbyte = &HC
  2613. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2614. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2615. txbyte = &H53
  2616. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2617. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2618. txbyte = &H55
  2619. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2620. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2621. txbyte = &H45
  2622. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2623. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2624. txbyte = &H46
  2625. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2626. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2627. txbyte = &H49
  2628. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2629. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2630. txbyte = &H4D
  2631. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2632. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2633. txbyte = &HFF
  2634. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2635. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2636. txbyte = &HFF
  2637. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2638. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2639. txbyte = &HFF
  2640. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2641. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2642. txbyte = &HFF
  2643. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2644. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2645. txbyte = &H56
  2646. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2647. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2648. txbyte = &H30
  2649. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2650. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2651. '
  2652. ' Receive ACK if unlock code succesfull
  2653. '
  2654. System.Threading.Thread.Sleep(100)
  2655. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2656. For i = 1 To rxqueue
  2657. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2658. Next
  2659. If rxbyte <> ACK Then
  2660. MsgBox("No ACK received after sending unlock code. Programming aborted, reset ecu and reprogram")
  2661. K8FlashStatus.Close()
  2662. B_FlashECU.Enabled = True
  2663. FT_status = FT_Close(lngHandle)
  2664. Return
  2665. End If
  2666. End If
  2667. '
  2668. ' Check status after unlock code
  2669. '
  2670. txqueue = 0
  2671. i = 0
  2672. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2673. System.Threading.Thread.Sleep(50)
  2674. While rxqueue = 0 And i < 10
  2675. txbyte = &H70
  2676. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2677. System.Threading.Thread.Sleep(40)
  2678. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2679. i = i + 1
  2680. End While
  2681. If (i >= 10) Or (rxqueue = 0) Then
  2682. MsgBox("Error in validating the unlock code from ECU. Programming aborted, reset ecu and reprogram")
  2683. K8FlashStatus.Close()
  2684. B_FlashECU.Enabled = True
  2685. FT_status = FT_Close(lngHandle)
  2686. Return
  2687. Else
  2688. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '128
  2689. System.Threading.Thread.Sleep(50)
  2690. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2691. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '140
  2692. System.Threading.Thread.Sleep(50)
  2693. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2694. End If
  2695. If (rxbyte <> &H8C) Or (FT_status <> 0) Then
  2696. MsgBox("Was not able to set the ecu key. Programming aborted, reset ecu and reprogram")
  2697. K8FlashStatus.Close()
  2698. B_FlashECU.Enabled = True
  2699. FT_status = FT_Close(lngHandle)
  2700. Return
  2701. End If
  2702. '
  2703. ' Clear status register just in case
  2704. '
  2705. txbyte = &H50
  2706. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2707. System.Threading.Thread.Sleep(50)
  2708. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2709. For i = 1 To rxqueue
  2710. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2711. Next
  2712. If rxbyte <> ACK Then
  2713. MsgBox("Status query error 1. Programming aborted, reset ecu and reprogram")
  2714. K8FlashStatus.Close()
  2715. B_FlashECU.Enabled = True
  2716. FT_status = FT_Close(lngHandle)
  2717. Return
  2718. End If
  2719. '
  2720. K8FlashStatus.fmode.Text = "Performing full erase, please wait"
  2721. K8FlashStatus.fmode.ForeColor = Color.Gray
  2722. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  2723. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  2724. K8FlashStatus.Refresh()
  2725. System.Windows.Forms.Application.DoEvents()
  2726. '
  2727. ' Send Erase full memory command
  2728. '
  2729. txbyte = &HA7
  2730. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2731. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2732. txbyte = &HD0
  2733. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2734. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2735. '
  2736. ' ECU confirms a succesfull erase by sending ACK
  2737. '
  2738. loopcount = 0
  2739. loopuntilack = False
  2740. While Not loopuntilack
  2741. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2742. While (rxqueue = 0) And (i < 100)
  2743. System.Threading.Thread.Sleep(50)
  2744. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2745. i = i + 1
  2746. End While
  2747. For i = 1 To rxqueue
  2748. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2749. If rxbyte = ACK Then
  2750. loopuntilack = True
  2751. End If
  2752. Next
  2753. If loopcount > 10 Then
  2754. '
  2755. ' Clear program lock bit and status register
  2756. '
  2757. txbyte = &H75
  2758. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2759. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2760. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2761. txbyte = &H50
  2762. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2763. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2764. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2765. End If
  2766. If loopcount > 20 Then
  2767. MsgBox("No ACK after full erase, Programming aborted, reset ecu and reprogram.")
  2768. K8FlashStatus.Close()
  2769. B_FlashECU.Enabled = True
  2770. FT_status = FT_Close(lngHandle)
  2771. BlockPgm = True
  2772. Return
  2773. End If
  2774. loopcount = loopcount + 1
  2775. End While
  2776. '
  2777. ' Flashing is finished, wait until switch is flipped back and then close com and activate enginedata if visible
  2778. '
  2779. do_once = True
  2780. timeEndPeriod(1)
  2781. B_FlashECU.Enabled = True
  2782. MsgBox("Full erase done, you can now flash the ecu")
  2783. ResetBlocks()
  2784. BlockPgm = True
  2785. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2786. 'While ((modemstat = &H6000) Or (modemstat = &H6200))
  2787. 'FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2788. 'System.Threading.Thread.Sleep(200)
  2789. 'FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2790. 'End While
  2791. K8FlashStatus.Close()
  2792. B_FlashECU.Enabled = True
  2793. FT_status = FT_Close(lngHandle)
  2794. If (FT_status = 0) Then
  2795. If K8Datastream.Visible() Then
  2796. K8Datastream.startenginedatacomms()
  2797. End If
  2798. Else
  2799. MsgBox("Can not close com port")
  2800. End If
  2801. End Sub
  2802. Public Sub SetECUType()
  2803. Hayabusa.Visible = True
  2804. FlashToolStripMenuItem.Visible = True
  2805. Select Case Mid(ECUID.Text, 1, 8)
  2806. Case "DJ18SE11"
  2807. Hayabusa.Text = "Hayabusa gen2 USA 32920-15H10"
  2808. Metric = False
  2809. ECUVersion = "gen2"
  2810. Case "DJ18SE20"
  2811. Hayabusa.Text = "Hayabusa gen2 Cali 32920-15H20"
  2812. Metric = False
  2813. ECUVersion = "gen2"
  2814. Case "DJ18SE00"
  2815. Hayabusa.Text = "Hayabusa gen2 EU 32920-15H00"
  2816. Metric = True
  2817. ECUVersion = "gen2"
  2818. Case "DJ47SE01"
  2819. Hayabusa.Text = "Bking EU and AU"
  2820. Metric = True
  2821. ECUVersion = "bking"
  2822. Case "DJ47SE20"
  2823. Hayabusa.Text = "Bking USA (California)"
  2824. Metric = False
  2825. ECUVersion = "bking"
  2826. Case "DJ0HSE50"
  2827. Hayabusa.Text = "Gixxer K7- 32920-21H60"
  2828. Metric = False
  2829. ECUVersion = "gixxer"
  2830. Case "DJ0HSE51"
  2831. Hayabusa.Text = "Gixxer K8- 32920-21H50"
  2832. Metric = False
  2833. ECUVersion = "gixxer"
  2834. 'Case "DJ21SER0"
  2835. ' Hayabusa.Text = "Gixxer empro K7- 32920-21HR0"
  2836. ' Metric = False
  2837. ' ECUVersion = "gixxer"
  2838. Case "DT0HSE50"
  2839. Hayabusa.Text = "Gixxer K7- 32920-21H50"
  2840. Metric = False
  2841. ECUVersion = "gixxer"
  2842. Case "41G10___"
  2843. Hayabusa.Text = "Gixxer K5-K6 enginedata only"
  2844. Metric = False
  2845. ECUVersion = "GixxerK5"
  2846. End Select
  2847. End Sub
  2848. Private Sub TestCheckSum()
  2849. Dim cb(4) As Byte
  2850. Dim FT_status As Long
  2851. Dim lngHandle As Long
  2852. Dim rxbyte, txbyte As Byte
  2853. Dim rxqueue, txqueue, eventstat As Integer
  2854. Dim i, x, y, comportnum As Integer
  2855. Dim ACK As Integer = &H6
  2856. Dim NAK As Integer = &H15
  2857. Dim cp As Integer
  2858. Dim buff(&HFF) As Byte
  2859. Dim k As Integer
  2860. Dim modemstat As Integer
  2861. Dim im, chksumflash As Long
  2862. Dim do_once As Boolean
  2863. Dim block As Integer
  2864. Dim chksumdiff As Boolean
  2865. Dim chksumfirmware As Integer
  2866. If K8Datastream.Visible() Then
  2867. K8Datastream.closeenginedatacomms()
  2868. End If
  2869. '
  2870. ' Get the FTDI device handle based on com port number and leave that port open
  2871. '
  2872. B_FlashECU.Enabled = False ' can not restart while flashing active
  2873. timeBeginPeriod(1)
  2874. comportnum = Val(Mid$(My.Settings.Item("ComPort"), 4))
  2875. FT_status = FT_GetNumberOfDevices(i, 0, &H80000000)
  2876. i = i - 1
  2877. For x = 0 To i
  2878. FT_status = FT_Open(x, lngHandle) ' only one
  2879. FT_status = FT_GetComPortNumber(lngHandle, y)
  2880. If y = comportnum Then
  2881. cp = x
  2882. x = i
  2883. End If
  2884. FT_status = FT_Close(lngHandle)
  2885. Next
  2886. If FT_status <> 0 Then
  2887. MsgBox("Could not open com port, please set correct port on K8 enginedata screen. Programming aborted, reset ecu and reprogram")
  2888. B_FlashECU.Enabled = True
  2889. FT_status = FT_Close(lngHandle)
  2890. Return
  2891. End If
  2892. '
  2893. ' Open, Reset, set timeouts and set baud rate
  2894. '
  2895. FT_status = FT_Open(cp, lngHandle)
  2896. FT_status = FT_ResetDevice(lngHandle, 3) 'set device to default status
  2897. FT_status = FT_status + FT_Purge(lngHandle) 'clear rx and tx buffers
  2898. FT_status = FT_status + FT_SetBaudRate(lngHandle, 57600) 'set speed 57600
  2899. FT_status = FT_status + FT_SetDataCharacteristics(lngHandle, 8, 1, 0) ' 8bits ,1 stop, parity none
  2900. FT_status = FT_status + FT_SetTimeouts(lngHandle, 5, 5) 'rx and tx timeouts ms
  2901. If FT_status <> 0 Then
  2902. MsgBox("Could not set Com port parameters. Programming aborted, set correct com port for the interface using data monitoring screen")
  2903. B_FlashECU.Enabled = True
  2904. FT_status = FT_Close(lngHandle)
  2905. Return
  2906. End If
  2907. '
  2908. ' Lets test that the interface is in the programming mode
  2909. '
  2910. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  2911. If FT_status <> 0 Then
  2912. MsgBox("Set the correct Com port for the interface using data monitoring screen")
  2913. K8FlashStatus.Close()
  2914. B_FlashECU.Enabled = True
  2915. K8FlashStatus.Close()
  2916. Return
  2917. End If
  2918. If Not ((modemstat = &H6000) Or (modemstat = &H6200)) Then
  2919. MsgBox("Interface is not on or it is not in programming mode, set programming switch to programming mode and retry")
  2920. K8FlashStatus.Close()
  2921. B_FlashECU.Enabled = True
  2922. FT_status = FT_Close(lngHandle)
  2923. Return
  2924. Else
  2925. '
  2926. ' Reset ecu
  2927. '
  2928. FT_status = FT_SetRts(lngHandle)
  2929. System.Threading.Thread.Sleep(300)
  2930. FT_status = FT_ClrRts(lngHandle)
  2931. System.Threading.Thread.Sleep(300)
  2932. End If
  2933. i = 0
  2934. rxqueue = 0
  2935. '
  2936. ' Sync baud rate with ecu 18 x 0x00, get ack as a reply
  2937. '
  2938. x = 18 'default is 18
  2939. For i = 1 To x
  2940. txbyte = &H0
  2941. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2942. System.Threading.Thread.Sleep(40)
  2943. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2944. If rxqueue <> 0 Then i = x
  2945. Next
  2946. System.Threading.Thread.Sleep(2)
  2947. For x = 1 To rxqueue
  2948. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2949. Next
  2950. If (rxbyte <> ACK) Then
  2951. MsgBox("Unexpected or missing ECU response during intialization. Programming aborted, reset ecu and reprogram." & Hex(rxqueue) & " " & Hex(rxbyte))
  2952. B_FlashECU.Enabled = True
  2953. FT_status = FT_Close(lngHandle)
  2954. Return
  2955. End If
  2956. '
  2957. ' check key status and send key if necessary
  2958. '
  2959. rxbyte = 0
  2960. i = 0
  2961. rxqueue = 0
  2962. While (rxqueue = 0) And (i < 10)
  2963. txbyte = &H70
  2964. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2965. System.Threading.Thread.Sleep(40)
  2966. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2967. i = i + 1
  2968. End While
  2969. For x = 1 To rxqueue
  2970. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  2971. Next
  2972. If rxbyte <> &H8C Then
  2973. txbyte = &HF5
  2974. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2975. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2976. txbyte = &H84
  2977. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2978. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2979. txbyte = &H0
  2980. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2981. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2982. txbyte = &H0
  2983. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2984. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2985. txbyte = &HC
  2986. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2987. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2988. txbyte = &H53
  2989. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2990. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2991. txbyte = &H55
  2992. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2993. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2994. txbyte = &H45
  2995. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2996. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  2997. txbyte = &H46
  2998. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  2999. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3000. txbyte = &H49
  3001. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3002. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3003. txbyte = &H4D
  3004. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3005. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3006. txbyte = &HFF
  3007. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3008. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3009. txbyte = &HFF
  3010. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3011. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3012. txbyte = &HFF
  3013. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3014. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3015. txbyte = &HFF
  3016. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3017. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3018. txbyte = &H56
  3019. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3020. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3021. txbyte = &H30
  3022. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3023. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3024. '
  3025. ' Receive ACK if unlock code succesfull
  3026. '
  3027. System.Threading.Thread.Sleep(100)
  3028. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3029. For i = 1 To rxqueue
  3030. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3031. Next
  3032. If rxbyte <> ACK Then
  3033. MsgBox("No ACK received after sending unlock code. Test checksum aborted, reset ecu and reprogram")
  3034. K8FlashStatus.Close()
  3035. B_FlashECU.Enabled = True
  3036. FT_status = FT_Close(lngHandle)
  3037. Return
  3038. End If
  3039. End If
  3040. '
  3041. ' Check status after unlock code
  3042. '
  3043. txqueue = 0
  3044. i = 0
  3045. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3046. System.Threading.Thread.Sleep(50)
  3047. While rxqueue = 0 And i < 10
  3048. txbyte = &H70
  3049. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3050. System.Threading.Thread.Sleep(40)
  3051. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3052. i = i + 1
  3053. End While
  3054. If (i >= 10) Or (rxqueue = 0) Then
  3055. MsgBox("Error in validating the unlock code from ECU. Test checksum aborted, reset ecu and reprogram")
  3056. B_FlashECU.Enabled = True
  3057. FT_status = FT_Close(lngHandle)
  3058. Return
  3059. Else
  3060. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '128
  3061. System.Threading.Thread.Sleep(50)
  3062. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3063. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '140
  3064. System.Threading.Thread.Sleep(50)
  3065. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3066. End If
  3067. If (rxbyte <> &H8C) Or (FT_status <> 0) Then
  3068. MsgBox("Was not able to set the ecu key. Programming aborted, reset ecu and reprogram")
  3069. B_FlashECU.Enabled = True
  3070. FT_status = FT_Close(lngHandle)
  3071. Return
  3072. End If
  3073. '
  3074. ' Clear status register just in case
  3075. '
  3076. txbyte = &H50
  3077. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3078. System.Threading.Thread.Sleep(50)
  3079. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3080. For i = 1 To rxqueue
  3081. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3082. Next
  3083. If rxbyte <> ACK Then
  3084. MsgBox("Status query error 1. Test checksum aborted, reset ecu and reprogram")
  3085. K8FlashStatus.Close()
  3086. B_FlashECU.Enabled = True
  3087. FT_status = FT_Close(lngHandle)
  3088. Return
  3089. End If
  3090. chksumdiff = False
  3091. For block = 0 To &HF
  3092. '
  3093. ' Test checksum per block. In case the checksum does not match, set full
  3094. ' flash flag that forces blocks 0x0 - 0xF flashing.
  3095. '
  3096. '
  3097. ' Acquire sum value and compare checksum
  3098. '
  3099. txbyte = &HE1
  3100. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3101. txbyte = 0
  3102. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3103. txbyte = block
  3104. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3105. txbyte = &HFF
  3106. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3107. txbyte = block
  3108. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3109. System.Threading.Thread.Sleep(200)
  3110. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3111. If rxqueue <> 2 Then
  3112. MsgBox("Error in reading checksum from ecu")
  3113. BlockPgm = True
  3114. End If
  3115. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3116. k = rxbyte
  3117. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3118. k = (k) + (rxbyte * &H100)
  3119. chksumfirmware = k
  3120. '
  3121. ' Lets calculate checksum for the bin in memory
  3122. '
  3123. chksumflash = 0
  3124. k = 0
  3125. For im = (0 + (block * &H10000)) To (&HFFFF + (block * &H10000))
  3126. If k = 0 Then
  3127. 'first highorder
  3128. chksumflash = chksumflash + (Flash(im) * &H100)
  3129. k = 1
  3130. Else
  3131. 'then loworder
  3132. k = 0
  3133. chksumflash = chksumflash + Flash(im)
  3134. End If
  3135. If chksumflash > &HFFFF Then
  3136. chksumflash = chksumflash - &H10000
  3137. End If
  3138. Next
  3139. 'chksumflash = (&H5AA5 - chksumflash) And &HFFFF
  3140. If chksumfirmware <> chksumflash Then
  3141. chksumdiff = True
  3142. Else
  3143. BlockOK(block)
  3144. End If
  3145. Next
  3146. '
  3147. ' Checksumtest is finished, wait until switch is flipped back and then close com and activate enginedata if visible
  3148. '
  3149. do_once = True
  3150. timeEndPeriod(1)
  3151. B_FlashECU.Enabled = True
  3152. FT_status = FT_Close(lngHandle)
  3153. If chksumdiff Then
  3154. MsgBox("Checksum does not match with flash, please verify or flash your ecu before making changes. Turn switch to enginedata.")
  3155. BlockPgm = True
  3156. Else
  3157. MsgBox("Flash checksum match, the computer and ecu memory are likely to be identical. You should be able to use the image in computer memory for changes. Turn switch to enginedata")
  3158. BlockPgm = False
  3159. End If
  3160. End Sub
  3161. #End Region
  3162. #Region "Not In Use"
  3163. Private Sub B_MapSharing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  3164. MapSharing.Show()
  3165. MapSharing.Select()
  3166. End Sub
  3167. Private Sub B_ReadECU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  3168. '
  3169. ' Lets put the verify function running in another thread, not for any particular reason
  3170. '
  3171. 'Dim th As New System.Threading.Thread(AddressOf ReadECU)
  3172. 'th.Start()
  3173. ReadECU()
  3174. End Sub
  3175. Private Sub FlashSerial_old()
  3176. Dim path As String
  3177. Dim flashfile As String
  3178. Dim binfile As String
  3179. Dim binpath As String
  3180. Dim cb(4) As Byte
  3181. Dim FT_status As Long
  3182. Dim lngHandle As Long
  3183. Dim rxbyte, txbyte As Byte
  3184. Dim rxqueue, txqueue, eventstat As Integer
  3185. Dim i, x, y, comportnum As Integer
  3186. Dim ACK As Integer = &H6
  3187. Dim NAK As Integer = &H15
  3188. Dim block As Integer
  3189. Dim cp As Integer
  3190. Dim buff(&HFF) As Byte
  3191. Dim j As Integer
  3192. Dim k As Integer
  3193. Dim blk5 As Boolean
  3194. Dim startaddr As Integer
  3195. Dim modemstat As Integer
  3196. Dim im, chksumflash, chksum As Long
  3197. Dim blkF As Boolean
  3198. Dim loopuntilack As Boolean
  3199. Dim loopcount As Integer
  3200. Dim do_once As Boolean
  3201. Dim starttime As Date
  3202. Dim endtime As Date
  3203. Dim totaltime As TimeSpan
  3204. endtime = Date.Now
  3205. starttime = Date.Now
  3206. totaltime = endtime.Subtract(starttime)
  3207. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  3208. K8FlashStatus.Show()
  3209. K8FlashStatus.Progressbar_Flashstatus.Maximum = &HFF
  3210. K8FlashStatus.Progressbar_Flashstatus.Value = 1
  3211. K8FlashStatus.fmode.ForeColor = Color.DarkGray
  3212. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  3213. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  3214. K8FlashStatus.Refresh()
  3215. System.Windows.Forms.Application.DoEvents()
  3216. '
  3217. ' Lets calculate checksum for the bin
  3218. '
  3219. chksum = ReadFlashWord(&HFFFF8) 'old checksum
  3220. WriteFlashWord(&HFFFF8, 0)
  3221. For im = 0 To &HFFFFF
  3222. If k = 0 Then
  3223. chksumflash = chksumflash + (Flash(im) * &H100)
  3224. k = 1
  3225. Else
  3226. k = 0
  3227. chksumflash = chksumflash + Flash(im)
  3228. End If
  3229. If chksumflash > &HFFFF Then
  3230. chksumflash = chksumflash - &H10000
  3231. End If
  3232. Next
  3233. chksumflash = (&H5AA5 - chksumflash) And &HFFFF
  3234. WriteFlashWord(&HFFFF8, chksumflash) 'new checksum to written to .bin
  3235. If K8Datastream.Visible() Then
  3236. K8Datastream.closeenginedatacomms()
  3237. End If
  3238. '
  3239. ' Get the FTDI device handle based on com port number and leave that port open
  3240. '
  3241. B_FlashECU.Enabled = False ' can not restart while flashing active
  3242. timeBeginPeriod(1)
  3243. comportnum = Val(Mid$(My.Settings.Item("ComPort"), 4))
  3244. FT_status = FT_GetNumberOfDevices(i, 0, &H80000000)
  3245. i = i - 1
  3246. For x = 0 To i
  3247. FT_status = FT_Open(x, lngHandle) ' only one
  3248. FT_status = FT_GetComPortNumber(lngHandle, y)
  3249. If y = comportnum Then
  3250. cp = x
  3251. x = i
  3252. End If
  3253. FT_status = FT_Close(lngHandle)
  3254. Next
  3255. If FT_status <> 0 Then
  3256. MsgBox("Could not open com port, please set correct port on K8 enginedata screen. Programming aborted, reset ecu and reprogram")
  3257. B_FlashECU.Enabled = True
  3258. FT_status = FT_Close(lngHandle)
  3259. K8FlashStatus.Close()
  3260. Return
  3261. End If
  3262. '
  3263. ' Open, Reset, set timeouts and set baud rate
  3264. '
  3265. FT_status = FT_Open(cp, lngHandle)
  3266. FT_status = FT_ResetDevice(lngHandle, 3) 'set device to default status
  3267. FT_status = FT_status + FT_Purge(lngHandle) 'clear rx and tx buffers
  3268. FT_status = FT_status + FT_SetBaudRate(lngHandle, 57600) 'set speed 57600
  3269. FT_status = FT_status + FT_SetDataCharacteristics(lngHandle, 8, 1, 0) ' 8bits ,1 stop, parity none
  3270. FT_status = FT_status + FT_SetTimeouts(lngHandle, 50, 50) 'rx and tx timeouts ms
  3271. FT_status = FT_status + FT_SetLatencyTimer(lngHandle, 8) 'ms
  3272. FT_status = FT_status + FT_SetUSBParameters(lngHandle, 4096, 4096) 'only rx is active by FTDI
  3273. If FT_status <> 0 Then
  3274. MsgBox("Could not set Com port parameters. Programming aborted, set correct com port for the interface using data monitoring screen")
  3275. B_FlashECU.Enabled = True
  3276. K8FlashStatus.Close()
  3277. FT_status = FT_Close(lngHandle)
  3278. Return
  3279. End If
  3280. '
  3281. ' Lets test that the interface is in the programming mode
  3282. '
  3283. FT_status = FT_SetDtr(lngHandle) 'new for Interface V1.1
  3284. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  3285. If FT_status <> 0 Then
  3286. MsgBox("Set the correct Com port for the interface using data monitoring screen")
  3287. K8FlashStatus.Close()
  3288. B_FlashECU.Enabled = True
  3289. K8FlashStatus.Close()
  3290. Return
  3291. End If
  3292. If Not ((modemstat = &H6000) Or (modemstat = &H6200)) Then
  3293. MsgBox("Interface is not on or it is not in programming mode, set programming switch to programming mode and retry")
  3294. K8FlashStatus.Close()
  3295. B_FlashECU.Enabled = True
  3296. FT_status = FT_Close(lngHandle)
  3297. Return
  3298. Else
  3299. '
  3300. ' Reset ecu
  3301. '
  3302. FT_status = FT_SetRts(lngHandle)
  3303. System.Threading.Thread.Sleep(300)
  3304. FT_status = FT_ClrRts(lngHandle)
  3305. System.Threading.Thread.Sleep(300)
  3306. End If
  3307. i = 0
  3308. rxqueue = 0
  3309. '
  3310. ' Sync baud rate with ecu 18 x 0x00, get ack as a reply
  3311. '
  3312. x = 18 'default is 18
  3313. For i = 1 To x
  3314. txbyte = &H0
  3315. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3316. System.Threading.Thread.Sleep(40)
  3317. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3318. If rxqueue <> 0 Then i = x
  3319. Next
  3320. System.Threading.Thread.Sleep(2)
  3321. For x = 1 To rxqueue
  3322. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3323. Next
  3324. If (rxbyte <> ACK) Then
  3325. MsgBox("Unexpected or missing ECU response during intialization. Programming aborted, reset ecu and reprogram." & Hex(rxqueue) & " " & Hex(rxbyte))
  3326. K8FlashStatus.Close()
  3327. B_FlashECU.Enabled = True
  3328. FT_status = FT_Close(lngHandle)
  3329. Return
  3330. End If
  3331. '
  3332. ' check key status and send key if necessary
  3333. '
  3334. rxbyte = 0
  3335. i = 0
  3336. rxqueue = 0
  3337. While (rxqueue = 0) And (i < 10)
  3338. txbyte = &H70
  3339. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3340. System.Threading.Thread.Sleep(40)
  3341. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3342. i = i + 1
  3343. End While
  3344. For x = 1 To rxqueue
  3345. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3346. Next
  3347. If rxbyte <> &H8C Then
  3348. txbyte = &HF5
  3349. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3350. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3351. txbyte = &H84
  3352. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3353. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3354. txbyte = &H0
  3355. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3356. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3357. txbyte = &H0
  3358. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3359. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3360. txbyte = &HC
  3361. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3362. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3363. txbyte = &H53
  3364. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3365. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3366. txbyte = &H55
  3367. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3368. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3369. txbyte = &H45
  3370. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3371. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3372. txbyte = &H46
  3373. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3374. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3375. txbyte = &H49
  3376. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3377. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3378. txbyte = &H4D
  3379. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3380. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3381. txbyte = &HFF
  3382. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3383. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3384. txbyte = &HFF
  3385. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3386. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3387. txbyte = &HFF
  3388. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3389. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3390. txbyte = &HFF
  3391. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3392. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3393. txbyte = &H56
  3394. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3395. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3396. txbyte = &H30
  3397. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3398. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3399. '
  3400. ' Receive ACK if unlock code succesfull
  3401. '
  3402. System.Threading.Thread.Sleep(100)
  3403. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3404. For i = 1 To rxqueue
  3405. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3406. Next
  3407. If rxbyte <> ACK Then
  3408. MsgBox("No ACK received after sending unlock code. Programming aborted, reset ecu and reprogram")
  3409. K8FlashStatus.Close()
  3410. B_FlashECU.Enabled = True
  3411. FT_status = FT_Close(lngHandle)
  3412. Return
  3413. End If
  3414. End If
  3415. '
  3416. ' Check status after unlock code
  3417. '
  3418. txqueue = 0
  3419. i = 0
  3420. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3421. System.Threading.Thread.Sleep(50)
  3422. While rxqueue = 0 And i < 10
  3423. txbyte = &H70
  3424. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3425. System.Threading.Thread.Sleep(40)
  3426. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3427. i = i + 1
  3428. End While
  3429. If (i >= 10) Or (rxqueue = 0) Then
  3430. MsgBox("Error in validating the unlock code from ECU. Programming aborted, reset ecu and reprogram")
  3431. K8FlashStatus.Close()
  3432. B_FlashECU.Enabled = True
  3433. FT_status = FT_Close(lngHandle)
  3434. Return
  3435. Else
  3436. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '128
  3437. System.Threading.Thread.Sleep(50)
  3438. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3439. FT_Read_Bytes(lngHandle, rxbyte, 1, 1) '140
  3440. System.Threading.Thread.Sleep(50)
  3441. FT_status = FT_status + FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3442. End If
  3443. If (rxbyte <> &H8C) Or (FT_status <> 0) Then
  3444. MsgBox("Was not able to set the ecu key. Programming aborted, reset ecu and reprogram")
  3445. K8FlashStatus.Close()
  3446. B_FlashECU.Enabled = True
  3447. FT_status = FT_Close(lngHandle)
  3448. Return
  3449. End If
  3450. '
  3451. ' Clear status register just in case
  3452. '
  3453. txbyte = &H50
  3454. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3455. System.Threading.Thread.Sleep(50)
  3456. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3457. For i = 1 To rxqueue
  3458. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3459. Next
  3460. If rxbyte <> ACK Then
  3461. MsgBox("Status query error 1. Programming aborted, reset ecu and reprogram")
  3462. K8FlashStatus.Close()
  3463. B_FlashECU.Enabled = True
  3464. FT_status = FT_Close(lngHandle)
  3465. Return
  3466. End If
  3467. '
  3468. ' New command added, to be tested
  3469. '
  3470. 'timeBeginPeriod(0)
  3471. '
  3472. ' Lets verify that this really is suzuki hayabusa ecu, flashing any other ecu type may damage the ecu and the bike
  3473. '
  3474. System.Threading.Thread.Sleep(100)
  3475. txbyte = &HFF
  3476. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3477. txbyte = &HFF
  3478. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3479. txbyte = &HF
  3480. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3481. System.Threading.Thread.Sleep(100)
  3482. k = 0
  3483. Dim s As String
  3484. s = ""
  3485. For j = 0 To &HFF
  3486. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3487. For i = 1 To rxqueue
  3488. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3489. '
  3490. ' Lets test that the ecu id matches close enought to Hayabusa ecu
  3491. ' if rxbyte = &HFF then its likely that the ecu has been fully erased and can be reflashed
  3492. '
  3493. If (k >= &HF0) And (k <= &HF5) Then
  3494. If (rxbyte <> ReadFlashByte(&HFFF00 + k)) And (rxbyte <> &HFF) Then
  3495. If MsgBox("Not same ECU ID in memory and inside the ecu. Possibly ecu is not from a Hayabusa. You can stop the flashing by pressing cancel.", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
  3496. K8FlashStatus.Close()
  3497. B_FlashECU.Enabled = True
  3498. FT_status = FT_Close(lngHandle)
  3499. End If
  3500. End If
  3501. End If
  3502. If (k >= &HF6) And (k <= &HF7) Then
  3503. If (rxbyte <> &H30) And (rxbyte <> &H31) And (rxbyte <> &H32) And (rxbyte <> &H35) And (rxbyte <> &HFF) Then
  3504. If MsgBox("Not a Hayabusa 15H00, 15H10, 15H20 or 15Hxx Generic ecu. Programming stopped to avoid damage to ecu or bike. Press cancel to stop, ok to continue", MsgBoxStyle.OkCancel) = MsgBoxResult.Cancel Then
  3505. K8FlashStatus.Close()
  3506. B_FlashECU.Enabled = True
  3507. FT_status = FT_Close(lngHandle)
  3508. End If
  3509. End If
  3510. End If
  3511. k = k + 1
  3512. Next
  3513. Next
  3514. '
  3515. ' Lets read what is the flashingmode in ecu if memory is set to fast flashmode
  3516. ' if fastflash then...
  3517. '
  3518. If ReadFlashLongWord(&H51F10) = &H536C4 Then
  3519. System.Threading.Thread.Sleep(100)
  3520. txbyte = &HFF
  3521. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3522. txbyte = &H1F
  3523. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3524. txbyte = &H5
  3525. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3526. System.Threading.Thread.Sleep(100)
  3527. k = 0
  3528. For j = 0 To &HFF
  3529. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3530. For i = 1 To rxqueue
  3531. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3532. If k = 19 Then
  3533. i = ReadFlashByte(&H51F10 + 3)
  3534. Select Case rxbyte
  3535. Case &H18 ' stock setting not fastmode, reflash block 5 is memory in fast mode
  3536. If ReadFlashLongWord(&H51F10) = &H536C4 Then
  3537. blk5 = True
  3538. Else
  3539. blk5 = False
  3540. End If
  3541. Case &HC4 ' ecu already in fast mode, no reflashing is needed
  3542. blk5 = False
  3543. Case &HFF ' block5 is empty, may be reflashing error. reflash block 5
  3544. blk5 = True
  3545. Case Else
  3546. MsgBox("Error in reading flashingmode from ECU, programming aborted. Please reboot ecu and reflash")
  3547. BlockPgm = True
  3548. K8FlashStatus.Close()
  3549. B_FlashECU.Enabled = True
  3550. FT_status = FT_Close(lngHandle)
  3551. End Select
  3552. End If
  3553. k = k + 1
  3554. Next
  3555. Next
  3556. End If
  3557. timeBeginPeriod(1)
  3558. System.Threading.Thread.Sleep(300)
  3559. '
  3560. ' For a reason or another block 0 requires full erase
  3561. '
  3562. If BlockChanged(0) = True Then
  3563. BlockPgm = True
  3564. End If
  3565. blkF = False ' this is just used for check sum testing
  3566. '
  3567. ' Here is an erase for the full ecu
  3568. '
  3569. If BlockPgm Then
  3570. endtime = Date.Now
  3571. totaltime = endtime.Subtract(starttime)
  3572. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  3573. K8FlashStatus.fmode.Text = "Performing full erase, please wait"
  3574. K8FlashStatus.fmode.ForeColor = Color.Gray
  3575. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  3576. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  3577. K8FlashStatus.Refresh()
  3578. System.Windows.Forms.Application.DoEvents()
  3579. '
  3580. ' Send Erase full memory command
  3581. '
  3582. txbyte = &HA7
  3583. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3584. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3585. txbyte = &HD0
  3586. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3587. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3588. '
  3589. ' ECU confirms a succesfull erase by sending ACK
  3590. '
  3591. loopcount = 0
  3592. loopuntilack = False
  3593. While Not loopuntilack
  3594. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3595. While (rxqueue = 0) And (i < 100)
  3596. System.Threading.Thread.Sleep(50)
  3597. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3598. i = i + 1
  3599. End While
  3600. For i = 1 To rxqueue
  3601. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3602. If rxbyte = ACK Then
  3603. loopuntilack = True
  3604. End If
  3605. Next
  3606. If loopcount > 10 Then
  3607. '
  3608. ' Clear program lock bit and status register
  3609. '
  3610. txbyte = &H75
  3611. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3612. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3613. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3614. txbyte = &H50
  3615. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3616. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3617. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3618. End If
  3619. If loopcount > 20 Then
  3620. MsgBox("No ACK after full erase, Programming aborted, reset ecu and reprogram.")
  3621. K8FlashStatus.Close()
  3622. B_FlashECU.Enabled = True
  3623. FT_status = FT_Close(lngHandle)
  3624. BlockPgm = True
  3625. Return
  3626. End If
  3627. loopcount = loopcount + 1
  3628. End While
  3629. End If
  3630. '
  3631. ' Now programmings starts
  3632. '
  3633. j = ReadFlashLongWord(&H51F10)
  3634. If ReadFlashLongWord(&H51F10) <> &H536C4 Then
  3635. K8FlashStatus.fmode.Text = "Normal flash "
  3636. Else
  3637. K8FlashStatus.fmode.Text = "Fast flash "
  3638. End If
  3639. K8FlashStatus.fmode.ForeColor = Color.Black
  3640. K8FlashStatus.Progressbar_Flashstatus.Value = 0
  3641. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  3642. K8FlashStatus.Refresh()
  3643. System.Windows.Forms.Application.DoEvents()
  3644. startaddr = 0
  3645. For block = startaddr To &HF
  3646. ' BlockChanged returns true if there has been any changes to that block
  3647. ' BlockPgm is a global variable that forces all blocks to be written
  3648. If BlockChanged(block) Or BlockPgm Then
  3649. endtime = Date.Now
  3650. totaltime = endtime.Subtract(starttime)
  3651. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  3652. K8FlashStatus.fmode.Text = K8FlashStatus.fmode.Text & Hex(block)
  3653. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  3654. K8FlashStatus.Refresh()
  3655. System.Windows.Forms.Application.DoEvents()
  3656. If block = &HF Then blkF = True
  3657. '
  3658. ' Erase block
  3659. '
  3660. txbyte = &H20
  3661. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3662. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3663. txbyte = &H0
  3664. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3665. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3666. txbyte = block
  3667. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3668. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3669. txbyte = &HD0
  3670. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3671. '
  3672. ' ECU confirms a succesfull erase by sending ACK
  3673. '
  3674. loopcount = 0
  3675. loopuntilack = False
  3676. While Not loopuntilack
  3677. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3678. i = 0
  3679. While (rxqueue = 0) And (i < 100)
  3680. System.Threading.Thread.Sleep(50)
  3681. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3682. i = i + 1
  3683. End While
  3684. For i = 1 To rxqueue
  3685. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3686. If rxbyte = ACK Then loopuntilack = True
  3687. Next
  3688. If rxbyte = NAK Then
  3689. '
  3690. ' Lets inform the user that something is wrong
  3691. '
  3692. K8FlashStatus.fmode.ForeColor = Color.Orange
  3693. '
  3694. ' Clear status register
  3695. '
  3696. System.Threading.Thread.Sleep(200)
  3697. txbyte = &H50
  3698. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3699. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3700. i = 0
  3701. While (rxqueue = 0) And (i < 10)
  3702. System.Threading.Thread.Sleep(50)
  3703. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3704. i = i + 1
  3705. End While
  3706. For i = 1 To rxqueue
  3707. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3708. Next
  3709. '
  3710. ' Disable lock bit
  3711. '
  3712. System.Threading.Thread.Sleep(200)
  3713. txbyte = &H75
  3714. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3715. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3716. i = 0
  3717. While (rxqueue = 0) And (i < 10)
  3718. System.Threading.Thread.Sleep(50)
  3719. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3720. i = i + 1
  3721. End While
  3722. For i = 1 To rxqueue
  3723. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3724. Next
  3725. '
  3726. ' Erase block again
  3727. '
  3728. System.Threading.Thread.Sleep(200)
  3729. txbyte = &H20
  3730. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3731. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3732. txbyte = &H0
  3733. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3734. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3735. txbyte = block
  3736. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3737. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3738. txbyte = &HD0
  3739. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3740. System.Threading.Thread.Sleep(200)
  3741. End If
  3742. If loopcount > 100 Then
  3743. MsgBox("No ACK after erasing a block=" & Str(block) & " Programming aborted, reset ecu and reprogram")
  3744. K8FlashStatus.Close()
  3745. B_FlashECU.Enabled = True
  3746. FT_status = FT_Close(lngHandle)
  3747. BlockPgm = True
  3748. Return
  3749. End If
  3750. endtime = Date.Now
  3751. totaltime = endtime.Subtract(starttime)
  3752. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  3753. K8FlashStatus.L_elapsedtime.Text = TimeOfDay
  3754. K8FlashStatus.Progressbar_Flashstatus.Value = loopcount
  3755. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  3756. K8FlashStatus.Refresh()
  3757. System.Windows.Forms.Application.DoEvents()
  3758. loopcount = loopcount + 1
  3759. End While
  3760. rxqueue = 0
  3761. i = 0
  3762. '
  3763. ' Write block using page write
  3764. '
  3765. K8FlashStatus.fmode.ForeColor = Color.Black
  3766. Dim page As Integer
  3767. For page = 0 To &HFF
  3768. '
  3769. ' Write one page at time
  3770. '
  3771. endtime = Date.Now
  3772. totaltime = endtime.Subtract(starttime)
  3773. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  3774. K8FlashStatus.Progressbar_Flashstatus.Value = page
  3775. K8FlashStatus.Progressbar_Flashstatus.Refresh()
  3776. K8FlashStatus.Refresh()
  3777. System.Windows.Forms.Application.DoEvents()
  3778. '
  3779. ' Check if the page is filled with 0xFF, no need to program
  3780. '
  3781. i = 0
  3782. For y = 0 To &HFF
  3783. buff(y) = ReadFlashByte((block * &H10000) + (page * &H100) + y)
  3784. If buff(y) <> &HFF Then
  3785. i = i + 1
  3786. End If
  3787. Next
  3788. If i > 0 Then ' there is something in the page that is not 0xFF
  3789. loopcount = 0
  3790. loopuntilack = False
  3791. While Not loopuntilack
  3792. '
  3793. ' write a page
  3794. '
  3795. txbyte = &H41
  3796. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3797. txbyte = page
  3798. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3799. txbyte = block
  3800. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3801. For y = 0 To &HFF
  3802. txbyte = buff(y)
  3803. 'FT_status = FT_Write_Bytes(lngHandle, txbyte, 1, txqueue)
  3804. Next
  3805. FT_status = FT_Write(lngHandle, buff, &HFF + 1, &HFF + 1)
  3806. '
  3807. ' this should be ack from page write
  3808. '
  3809. rxbyte = 0
  3810. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3811. i = 0
  3812. While (rxqueue = 0) And (i < 30)
  3813. System.Threading.Thread.Sleep(25)
  3814. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3815. If rxqueue > 0 Then i = 30
  3816. i = i + 1
  3817. End While
  3818. For i = 1 To rxqueue
  3819. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3820. If rxbyte = ACK Then loopuntilack = True
  3821. Next
  3822. If loopcount > 5 Then
  3823. '
  3824. ' Clear program lock bit and status register
  3825. '
  3826. K8FlashStatus.fmode.ForeColor = Color.Orange
  3827. K8FlashStatus.Refresh()
  3828. System.Windows.Forms.Application.DoEvents()
  3829. System.Threading.Thread.Sleep(100)
  3830. txbyte = &H75
  3831. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3832. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3833. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3834. System.Threading.Thread.Sleep(100)
  3835. rxbyte = 0
  3836. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3837. i = 0
  3838. While (rxqueue = 0) And (i < 30)
  3839. System.Threading.Thread.Sleep(25)
  3840. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3841. If rxqueue > 0 Then i = 30
  3842. i = i + 1
  3843. End While
  3844. For i = 1 To rxqueue
  3845. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3846. Next
  3847. txbyte = &H50
  3848. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3849. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3850. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3851. System.Threading.Thread.Sleep(100)
  3852. rxbyte = 0
  3853. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3854. i = 0
  3855. While (rxqueue = 0) And (i < 30)
  3856. System.Threading.Thread.Sleep(25)
  3857. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3858. If rxqueue > 0 Then i = 30
  3859. i = i + 1
  3860. End While
  3861. For i = 1 To rxqueue
  3862. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3863. Next
  3864. End If
  3865. If loopcount > 10 Then
  3866. MsgBox("No ACK after writing a block=" & Str(block) & " page=" & Str(page) & ". Programming aborted, reset ecu and reprogram")
  3867. K8FlashStatus.Close()
  3868. B_FlashECU.Enabled = True
  3869. FT_status = FT_Close(lngHandle)
  3870. BlockPgm = True
  3871. Return
  3872. End If
  3873. loopcount = loopcount + 1
  3874. End While
  3875. End If
  3876. Next
  3877. End If
  3878. Next
  3879. '
  3880. ' Acquire sum value and compare checksum. This will be done every time as flashing always
  3881. ' starts by writing a new checksum to the image in computer memory
  3882. '
  3883. txbyte = &HE1
  3884. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3885. txbyte = 0
  3886. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3887. txbyte = 0
  3888. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3889. txbyte = &HFF
  3890. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3891. txbyte = &HF
  3892. FT_Write_Bytes(lngHandle, txbyte, 1, 1)
  3893. System.Threading.Thread.Sleep(200)
  3894. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3895. If rxqueue <> 2 Then MsgBox("Error in reading checksum from ecu")
  3896. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3897. k = rxbyte
  3898. FT_Read_Bytes(lngHandle, rxbyte, 1, 1)
  3899. k = (k) + (rxbyte * &H100)
  3900. '
  3901. ' Flashing is finished, wait until switch is flipped back and then close com and activate enginedata if visible
  3902. '
  3903. do_once = True
  3904. timeEndPeriod(1)
  3905. If k <> &H5AA5 Then
  3906. MsgBox("Checksum error when validating the flash, please reflash your ecu before using it.")
  3907. K8FlashStatus.fmode.Text = "Checksum error, please reprogram"
  3908. ResetBlocks()
  3909. BlockPgm = True
  3910. Else
  3911. K8FlashStatus.fmode.Text = "Flash OK, turn switch to enginedata"
  3912. ResetBlocks()
  3913. BlockPgm = False
  3914. End If
  3915. endtime = Date.Now
  3916. totaltime = endtime.Subtract(starttime)
  3917. K8FlashStatus.L_elapsedtime.Text = totaltime.Minutes & ":" & totaltime.Seconds
  3918. K8FlashStatus.Refresh()
  3919. System.Windows.Forms.Application.DoEvents()
  3920. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  3921. While ((modemstat = &H6000) Or (modemstat = &H6200))
  3922. FT_status = FT_GetStatus(lngHandle, rxqueue, txqueue, eventstat)
  3923. System.Threading.Thread.Sleep(200)
  3924. FT_status = FT_GetModemStatus(lngHandle, modemstat)
  3925. End While
  3926. K8FlashStatus.Close()
  3927. B_FlashECU.Enabled = True
  3928. FT_status = FT_ClrDtr(lngHandle) 'new for Interface V1.1
  3929. FT_status = FT_Close(lngHandle)
  3930. If (FT_status = 0) Then
  3931. If K8Datastream.Visible() Then
  3932. K8Datastream.startenginedatacomms()
  3933. End If
  3934. Else
  3935. MsgBox("Can not close com port, please save the bin and reboot your computer and reflash just in case.")
  3936. End If
  3937. '
  3938. ' generate the bin that was flashed as a backup file
  3939. '
  3940. flashfile = "ecuflash.bin"
  3941. binpath = Application.StartupPath
  3942. binfile = "ecuflash.bin"
  3943. path = binpath & flashfile
  3944. ' if the temporary file exists for any reason, delete it
  3945. If File.Exists(path) = True Then
  3946. File.Delete(path)
  3947. End If
  3948. ' create the temporary file from current memory
  3949. fs = File.Open(path, FileMode.CreateNew)
  3950. fs.Write(Flash, 0, (262144 * 4))
  3951. fs.Close()
  3952. '
  3953. ' Generate a backup copy of the flashed file just flashed
  3954. '
  3955. binfile = ECUID.Text & "-"
  3956. binfile = binfile & My.Computer.Clock.LocalTime.Date.Day & "-"
  3957. binfile = binfile & My.Computer.Clock.LocalTime.Date.Month & "-"
  3958. binfile = binfile & My.Computer.Clock.LocalTime.Date.Year & "-"
  3959. binfile = binfile & My.Computer.Clock.LocalTime.Hour & "-"
  3960. binfile = binfile & My.Computer.Clock.LocalTime.Minute
  3961. binfile = binfile & ".bin"
  3962. path = binpath & binfile
  3963. ' if the temporary file exists for any reason, delete it
  3964. If File.Exists(path) = True Then
  3965. File.Delete(path)
  3966. End If
  3967. ' create the temporary file from curren memory
  3968. fs = File.Open(path, FileMode.CreateNew)
  3969. fs.Write(Flash, 0, (262144 * 4))
  3970. fs.Close()
  3971. ' cleanup of old backup files
  3972. CleanUpFDTDirectory(binpath)
  3973. End Sub
  3974. #End Region
  3975. Private Sub RecoveryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecoveryToolStripMenuItem.Click
  3976. ' Lets use OpenFileDialog to open a new flash image file
  3977. Dim fdlg As OpenFileDialog = New OpenFileDialog()
  3978. Dim fs As FileStream
  3979. fdlg.InitialDirectory = My.Application.Info.DirectoryPath 'My.Application.Info.DirectoryPath
  3980. fdlg.Title = "Open ECU .bin file from recovery location"
  3981. fdlg.Filter = "ECU definitions (*.bin)|*.bin"
  3982. fdlg.FilterIndex = 1
  3983. fdlg.RestoreDirectory = True
  3984. fdlg.FileName = My.Application.Info.DirectoryPath
  3985. CloseChildWindows()
  3986. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  3987. ' OK, so the file is found, now lets start processing it
  3988. path = fdlg.FileName
  3989. If path.Length > 15 Then
  3990. L_Comparefile.Text = "..." & path.Substring(path.Length - 15)
  3991. L_File.Text = L_Comparefile.Text
  3992. Else
  3993. L_Comparefile.Text = path
  3994. L_File.Text = L_Comparefile.Text
  3995. End If
  3996. ' Open the stream and read it to global variable "Flash".
  3997. fs = File.OpenRead(path)
  3998. Dim b(1) As Byte
  3999. Dim i As Integer
  4000. i = 0
  4001. Do While fs.Read(b, 0, 1) > 0
  4002. Flash(i) = b(0)
  4003. FlashCopy(i) = b(0)
  4004. i = i + 1
  4005. Loop
  4006. fs.Close()
  4007. ' Check that the binary lenght matches expected ecu
  4008. ' and initialize variables and stuff as needed
  4009. '
  4010. ' Remove v1.5 protection if exists
  4011. '
  4012. If Flash(&H2) = 0 Then
  4013. Flash(&H2) = 4
  4014. MsgBox("ECUeditor v1.5 protection detected and removed")
  4015. End If
  4016. Select Case i
  4017. Case (262144 * 4)
  4018. ECUVersion = "gen2"
  4019. '
  4020. ' Make sure the ECU id is supported type
  4021. '
  4022. i = 0
  4023. ECUID.Text = ""
  4024. Do While i < 8
  4025. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  4026. i = i + 1
  4027. Loop
  4028. ' check the ecu id bytes and validate that the ecu flash image is supported
  4029. If (Mid(ECUID.Text, 1, 6) <> "DJ18SE") And (Mid(ECUID.Text, 1, 6) <> "DJ47SE") Then
  4030. ECUNotSupported.ShowDialog()
  4031. Else
  4032. SetECUType()
  4033. End If
  4034. BlockPgm = True
  4035. CloseChildWindows()
  4036. Case (262144)
  4037. ECUVersion = "gen1"
  4038. FlashToolStripMenuItem.Visible = False
  4039. ' Make sure the ECU id is supported type
  4040. i = 0
  4041. ECUID.Text = ""
  4042. Do While i < 8
  4043. ECUID.Text = ECUID.Text & Chr(Flash(&H3FFF0 + i))
  4044. i = i + 1
  4045. Loop
  4046. ' check the ecu id bytes and validate that the ecu flash image is supported
  4047. If Mid(ECUID.Text, 1, 6) <> "BB34BB" Then
  4048. ECUNotSupported.ShowDialog()
  4049. Else
  4050. Hayabusa.Visible = True
  4051. Select Case Mid(ECUID.Text, 1, 8)
  4052. Case "BB34BB51"
  4053. Hayabusa.Text = "Hayabusa EU"
  4054. Metric = True
  4055. ECUVersion = "gen1"
  4056. Case "BB34BB35"
  4057. Hayabusa.Text = "Hayabusa USA"
  4058. Metric = False
  4059. ECUVersion = "gen1"
  4060. Case Else
  4061. Hayabusa.Text = "Unknown model"
  4062. Metric = True
  4063. ECUVersion = ""
  4064. End Select
  4065. End If
  4066. Case Else
  4067. ECUVersion = ""
  4068. ECUNotSupported.ShowDialog()
  4069. End Select
  4070. My.Settings.Item("path") = path
  4071. My.Settings.Item("comparepath") = comparepath
  4072. ' enable controls, otherwise at form load an event will occur
  4073. Limiters.C_RPM.Enabled = True
  4074. SaveToolStripMenuItem.Enabled = True
  4075. B_FlashECU.Enabled = True
  4076. B_Limiters.Enabled = True
  4077. B_Shifter.Enabled = True
  4078. B_FuelMap.Enabled = True
  4079. B_IgnitionMap.Enabled = True
  4080. B_AdvancedSettings.Enabled = True
  4081. Select Case ECUVersion
  4082. Case "gen1"
  4083. B_EngineData.Enabled = True
  4084. FuelMap.Close()
  4085. IgnitionMap.Close()
  4086. FlashToolStripMenuItem.Visible = False
  4087. Case "gen2"
  4088. B_EngineData.Enabled = True
  4089. K8Ignitionmap.Close()
  4090. K8Fuelmap.Close()
  4091. FlashToolStripMenuItem.Visible = Enabled
  4092. Case "gixxer"
  4093. B_EngineData.Enabled = True
  4094. GixxerIgnitionmap.Close()
  4095. GixxerFuelmap.Close()
  4096. FlashToolStripMenuItem.Visible = Enabled
  4097. Case "bking"
  4098. B_EngineData.Enabled = True
  4099. FlashToolStripMenuItem.Visible = Enabled
  4100. Case Else
  4101. MsgBox("feature not yet implemented")
  4102. End Select
  4103. MsgBox("Now remember to save the file with a different name to your default ecueditor.com files directory")
  4104. End If
  4105. End Sub
  4106. Private Sub B_DataLogging_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_DataLogging.Click
  4107. K8EngineDataLogger.Show()
  4108. End Sub
  4109. Private Sub GixxerK5K6EnginedataOnlyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GixxerK5K6EnginedataOnlyToolStripMenuItem.Click
  4110. CloseChildWindows()
  4111. L_File.Text = ""
  4112. L_Comparefile.Text = ""
  4113. DisableButtons()
  4114. ECUID.Text = "41G10___"
  4115. SetECUType()
  4116. ' enable controls, otherwise at form load an event will occur
  4117. B_EngineData.Enabled = True
  4118. End Sub
  4119. Private Sub NewStockGixxerK7ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewStockGixxerK7ToolStripMenuItem.Click
  4120. Dim defpath As String ' this is for this subroutine only
  4121. CloseChildWindows()
  4122. ' OK, so the file is found, now lets start processing it
  4123. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\GixxerK7.bin"
  4124. L_File.Text = ""
  4125. L_Comparefile.Text = ""
  4126. DisableButtons()
  4127. ' Open the stream and read it to global variable "Flash".
  4128. fs = File.OpenRead(defpath)
  4129. Dim b(1) As Byte
  4130. Dim i As Integer
  4131. i = 0
  4132. Do While fs.Read(b, 0, 1) > 0
  4133. Flash(i) = b(0)
  4134. FlashCopy(i) = b(0)
  4135. i = i + 1
  4136. Loop
  4137. fs.Close()
  4138. ' Check that the binary lenght matches expected ecu
  4139. If i <> (262144 * 4) Then
  4140. ECUNotSupported.ShowDialog()
  4141. End If
  4142. ECUVersion = "gixxer"
  4143. '
  4144. ' Make sure the ECU id is supported type
  4145. '
  4146. i = 0
  4147. ECUID.Text = ""
  4148. Do While i < 8
  4149. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  4150. i = i + 1
  4151. Loop
  4152. ' check the ecu id bytes and validate that the ecu flash image is supported
  4153. If (Mid(ECUID.Text, 1, 4) <> "DJ0H") And (Mid(ECUID.Text, 1, 4) <> "DT0H") And (Mid(ECUID.Text, 1, 4) <> "DJ21") Then
  4154. ECUNotSupported.ShowDialog()
  4155. Else
  4156. SetECUType()
  4157. End If
  4158. ' enable controls, otherwise at form load an event will occur
  4159. Limiters.C_RPM.Enabled = True
  4160. SaveToolStripMenuItem.Enabled = True
  4161. B_FlashECU.Enabled = True
  4162. B_Limiters.Enabled = True
  4163. B_EngineData.Enabled = True
  4164. B_Shifter.Enabled = False
  4165. B_FuelMap.Enabled = True
  4166. B_IgnitionMap.Enabled = True
  4167. B_AdvancedSettings.Enabled = False
  4168. B_DataLogging.Enabled = False
  4169. K8Fuelmap.Close()
  4170. K8Ignitionmap.Close()
  4171. MsgBox("A new Gixxer K7- basemap is generated", MsgBoxStyle.Information)
  4172. BlockPgm = True
  4173. End Sub
  4174. Private Sub EcueditorcomHomepageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EcueditorcomHomepageToolStripMenuItem.Click
  4175. ProgramHomepage.Show()
  4176. ProgramHomepage.Select()
  4177. End Sub
  4178. Private Sub DonateForKeepingTheProgramFreeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DonateForKeepingTheProgramFreeToolStripMenuItem.Click
  4179. Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ZSM36H9HWNU2C")
  4180. End Sub
  4181. Private Sub ViewLatestDevelopmentFromDeveloperSiteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewLatestDevelopmentFromDeveloperSiteToolStripMenuItem.Click
  4182. Process.Start("https://bitbucket.org/ecueditor/ecueditor/changesets")
  4183. End Sub
  4184. Private Sub NewStockGixxerK8ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewStockGixxerK8ToolStripMenuItem.Click
  4185. Dim defpath As String ' this is for this subroutine only
  4186. CloseChildWindows()
  4187. ' OK, so the file is found, now lets start processing it
  4188. defpath = My.Application.Info.DirectoryPath & "\ecu.bin\GixxerK7.bin"
  4189. L_File.Text = ""
  4190. L_Comparefile.Text = ""
  4191. DisableButtons()
  4192. ' Open the stream and read it to global variable "Flash".
  4193. fs = File.OpenRead(defpath)
  4194. Dim b(1) As Byte
  4195. Dim i As Integer
  4196. i = 0
  4197. Do While fs.Read(b, 0, 1) > 0
  4198. Flash(i) = b(0)
  4199. FlashCopy(i) = b(0)
  4200. i = i + 1
  4201. Loop
  4202. fs.Close()
  4203. ' Check that the binary lenght matches expected ecu
  4204. If i <> (262144 * 4) Then
  4205. ECUNotSupported.ShowDialog()
  4206. End If
  4207. ECUVersion = "gixxer"
  4208. '
  4209. ' Make sure the ECU id is supported type
  4210. '
  4211. i = 0
  4212. ECUID.Text = ""
  4213. Do While i < 8
  4214. ECUID.Text = ECUID.Text & Chr(Flash(&HFFFF0 + i))
  4215. i = i + 1
  4216. Loop
  4217. ' check the ecu id bytes and validate that the ecu flash image is supported
  4218. If (Mid(ECUID.Text, 1, 4) <> "DJ0H") And (Mid(ECUID.Text, 1, 4) <> "DT0H") And (Mid(ECUID.Text, 1, 4) <> "DJ21") Then
  4219. ECUNotSupported.ShowDialog()
  4220. Else
  4221. SetECUType()
  4222. End If
  4223. ' enable controls, otherwise at form load an event will occur
  4224. Limiters.C_RPM.Enabled = True
  4225. SaveToolStripMenuItem.Enabled = True
  4226. B_FlashECU.Enabled = True
  4227. B_Limiters.Enabled = True
  4228. B_EngineData.Enabled = True
  4229. B_Shifter.Enabled = False
  4230. B_FuelMap.Enabled = True
  4231. B_IgnitionMap.Enabled = True
  4232. B_AdvancedSettings.Enabled = False
  4233. B_DataLogging.Enabled = False
  4234. K8Fuelmap.Close()
  4235. K8Ignitionmap.Close()
  4236. MsgBox("A new Gixxer K8- basemap is generated", MsgBoxStyle.Information)
  4237. BlockPgm = True
  4238. End Sub
  4239. Private Sub BuyInterfaceFromEBayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuyInterfaceFromEBayToolStripMenuItem.Click
  4240. Process.Start("http://product-search.ebay.com/ecueditor.com")
  4241. End Sub
  4242. Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter
  4243. End Sub
  4244. End Class