| 1 | #tag Class |
---|
| 2 | Protected Class ICSSerialPort |
---|
| 3 | Inherits serial |
---|
| 4 | #tag Method, Flags = &h0 |
---|
| 5 | Sub resetCar() |
---|
| 6 | //This sends the command to reset the car back to a default configuration |
---|
| 7 | //Defaults are different for each car type |
---|
| 8 | setDefaultValues |
---|
| 9 | |
---|
| 10 | //Send the default values to the car |
---|
| 11 | writeCar |
---|
| 12 | End Sub |
---|
| 13 | #tag EndMethod |
---|
| 14 | |
---|
| 15 | #tag Method, Flags = &h0 |
---|
| 16 | Sub setDefaultValues() |
---|
| 17 | //Set the defaults for the bytes sent to a car |
---|
| 18 | |
---|
| 19 | select case carType |
---|
| 20 | |
---|
| 21 | case "MR-03" |
---|
| 22 | byte01 = chrb(&hD5) |
---|
| 23 | byte02 = chrb(&h5A) |
---|
| 24 | byte03 = chrb(&h64) |
---|
| 25 | byte04 = chrb(&hFF) |
---|
| 26 | byte05 = chrb(&h02) |
---|
| 27 | byte06 = chrb(&h02) |
---|
| 28 | byte07 = chrb(&h01) |
---|
| 29 | byte08 = chrb(&hFF) |
---|
| 30 | byte09 = chrb(&hBC) |
---|
| 31 | byte10 = chrb(&h44) |
---|
| 32 | byte11 = chrb(&h88) |
---|
| 33 | byte12 = chrb(&h78) |
---|
| 34 | byte13 = chrb(&hFF) |
---|
| 35 | byte14 = chrb(&h2C) |
---|
| 36 | byte15 = chrb(&h05) |
---|
| 37 | byte16 = chrb(&h5A) |
---|
| 38 | byte17 = chrb(&h3C) |
---|
| 39 | byte18 = chrb(&h87) |
---|
| 40 | |
---|
| 41 | case "dNaNo" |
---|
| 42 | byte01 = chrb(&hD5) |
---|
| 43 | byte02 = chrb(&h5A) |
---|
| 44 | byte03 = chrb(&hFF) |
---|
| 45 | byte04 = chrb(&hFF) |
---|
| 46 | byte05 = chrb(&h0A) |
---|
| 47 | byte06 = chrb(&h03) |
---|
| 48 | byte07 = chrb(&h01) |
---|
| 49 | byte08 = chrb(&h40) |
---|
| 50 | byte09 = chrb(&hBC) |
---|
| 51 | byte10 = chrb(&h44) |
---|
| 52 | byte11 = chrb(&h88) |
---|
| 53 | byte12 = chrb(&h78) |
---|
| 54 | byte13 = chrb(&h03) |
---|
| 55 | byte14 = chrb(&hFF) |
---|
| 56 | byte15 = chrb(&hFF) |
---|
| 57 | byte16 = chrb(&h80) |
---|
| 58 | byte17 = chrb(&h80) |
---|
| 59 | byte18 = chrb(&hA7) |
---|
| 60 | |
---|
| 61 | case "ASF" |
---|
| 62 | byte01 = chrb(&hD5) |
---|
| 63 | byte02 = chrb(&h5A) |
---|
| 64 | byte03 = chrb(&hFF) |
---|
| 65 | byte04 = chrb(&hFF) |
---|
| 66 | byte05 = chrb(&h0A) |
---|
| 67 | byte06 = chrb(&h03) |
---|
| 68 | byte07 = chrb(&h01) |
---|
| 69 | byte08 = chrb(&h78) |
---|
| 70 | byte09 = chrb(&hBC) |
---|
| 71 | byte10 = chrb(&h44) |
---|
| 72 | byte11 = chrb(&h88) |
---|
| 73 | byte12 = chrb(&h78) |
---|
| 74 | byte13 = chrb(&h03) |
---|
| 75 | byte14 = chrb(&hFF) |
---|
| 76 | byte15 = chrb(&hFF) |
---|
| 77 | byte16 = chrb(&hFF) |
---|
| 78 | byte17 = chrb(&hFF) |
---|
| 79 | byte18 = chrb(&hDD) |
---|
| 80 | |
---|
| 81 | else |
---|
| 82 | //default thing to do |
---|
| 83 | MsgBox "Error invalid car type" |
---|
| 84 | return |
---|
| 85 | end select |
---|
| 86 | |
---|
| 87 | //trigger the event definition so main program knows the values changed |
---|
| 88 | valuesChanged() |
---|
| 89 | End Sub |
---|
| 90 | #tag EndMethod |
---|
| 91 | |
---|
| 92 | #tag Method, Flags = &h0 |
---|
| 93 | Sub setCarType(value as string) |
---|
| 94 | //When setting car type we must reconfigure the serial port |
---|
| 95 | //Some car types have a different baud rate |
---|
| 96 | |
---|
| 97 | select case value |
---|
| 98 | |
---|
| 99 | case "MR-03" |
---|
| 100 | carType = value |
---|
| 101 | byte15 = chrb(&h05) |
---|
| 102 | byte16 = chrb(&h5A) |
---|
| 103 | byte17 = chrb(&h3C) |
---|
| 104 | case "dNaNo" |
---|
| 105 | carType = value |
---|
| 106 | byte15 = chrb(&hFF) |
---|
| 107 | byte16 = chrb(&h80) |
---|
| 108 | byte17 = chrb(&h80) |
---|
| 109 | case "ASF" |
---|
| 110 | carType = value |
---|
| 111 | byte15 = chrb(&hFF) |
---|
| 112 | byte16 = chrb(&hFF) |
---|
| 113 | byte17 = chrb(&hFF) |
---|
| 114 | |
---|
| 115 | else |
---|
| 116 | MsgBox "Error setting Car Type" |
---|
| 117 | end select |
---|
| 118 | |
---|
| 119 | valuesChanged() |
---|
| 120 | End Sub |
---|
| 121 | #tag EndMethod |
---|
| 122 | |
---|
| 123 | #tag Method, Flags = &h0 |
---|
| 124 | Sub writeCar() |
---|
| 125 | //Send set bytes to the car |
---|
| 126 | mode = "write" |
---|
| 127 | |
---|
| 128 | dim sendstring as string |
---|
| 129 | |
---|
| 130 | calculateChecksum() |
---|
| 131 | |
---|
| 132 | sendstring = byte01 + byte02 + byte03 + byte04 + byte05 + byte06 + byte07 + byte08 + byte09 + byte10 + byte11 + byte12 + byte13 + byte14 + byte15 + byte16 + byte17 + byte18 |
---|
| 133 | |
---|
| 134 | me.Write(sendstring) |
---|
| 135 | End Sub |
---|
| 136 | #tag EndMethod |
---|
| 137 | |
---|
| 138 | #tag Method, Flags = &h0 |
---|
| 139 | Sub calculateChecksum() |
---|
| 140 | //Use this to calculate byte 18, the checksum |
---|
| 141 | //The checksum is just adding bytes 2-17 together but rounded at each byte |
---|
| 142 | |
---|
| 143 | dim i as integer |
---|
| 144 | |
---|
| 145 | i = (asc(byte02) + asc(byte03)) mod &h100 |
---|
| 146 | i = (i + asc(byte04)) mod &h100 |
---|
| 147 | i = (i + asc(byte05)) mod &h100 |
---|
| 148 | i = (i + asc(byte06)) mod &h100 |
---|
| 149 | i = (i + asc(byte07)) mod &h100 |
---|
| 150 | i = (i + asc(byte08)) mod &h100 |
---|
| 151 | i = (i + asc(byte09)) mod &h100 |
---|
| 152 | i = (i + asc(byte10)) mod &h100 |
---|
| 153 | i = (i + asc(byte11)) mod &h100 |
---|
| 154 | i = (i + asc(byte12)) mod &h100 |
---|
| 155 | i = (i + asc(byte13)) mod &h100 |
---|
| 156 | i = (i + asc(byte14)) mod &h100 |
---|
| 157 | i = (i + asc(byte15)) mod &h100 |
---|
| 158 | i = (i + asc(byte16)) mod &h100 |
---|
| 159 | i = (i + asc(byte17)) mod &h100 |
---|
| 160 | |
---|
| 161 | byte18 = chrb(i) |
---|
| 162 | End Sub |
---|
| 163 | #tag EndMethod |
---|
| 164 | |
---|
| 165 | #tag Method, Flags = &h0 |
---|
| 166 | Sub readCar() |
---|
| 167 | //Read values from the currently attached car |
---|
| 168 | dim sendstring as string |
---|
| 169 | |
---|
| 170 | mode = "read" |
---|
| 171 | |
---|
| 172 | sendstring = chrb(&hC5) |
---|
| 173 | |
---|
| 174 | me.Write(sendstring) |
---|
| 175 | End Sub |
---|
| 176 | #tag EndMethod |
---|
| 177 | |
---|
| 178 | #tag Method, Flags = &h0 |
---|
| 179 | Sub Constructor() |
---|
| 180 | //Make sure we have a database and if not create it |
---|
| 181 | dim exists as boolean |
---|
| 182 | |
---|
| 183 | exists = prepareDB() |
---|
| 184 | if exists = false then |
---|
| 185 | //No database available, create one |
---|
| 186 | createDB() |
---|
| 187 | else |
---|
| 188 | //Database exists, connect to it |
---|
| 189 | if fsicsdb.Connect = false then |
---|
| 190 | MsgBox "Database connection failed" |
---|
| 191 | end if |
---|
| 192 | end if |
---|
| 193 | |
---|
| 194 | //Set default byte values |
---|
| 195 | byte01 = chrb(&hD5) |
---|
| 196 | byte02 = chrb(&h5A) |
---|
| 197 | byte03 = chrb(&h64) |
---|
| 198 | byte04 = chrb(&hFF) |
---|
| 199 | byte05 = chrb(&h02) |
---|
| 200 | byte06 = chrb(&h02) |
---|
| 201 | byte07 = chrb(&h01) |
---|
| 202 | byte08 = chrb(&hFF) |
---|
| 203 | byte09 = chrb(&hBC) |
---|
| 204 | byte10 = chrb(&h44) |
---|
| 205 | byte11 = chrb(&h88) |
---|
| 206 | byte12 = chrb(&h78) |
---|
| 207 | byte13 = chrb(&hFF) |
---|
| 208 | byte14 = chrb(&h2C) |
---|
| 209 | byte15 = chrb(&h05) |
---|
| 210 | byte16 = chrb(&h5A) |
---|
| 211 | byte17 = chrb(&h3C) |
---|
| 212 | byte18 = chrb(&h87) |
---|
| 213 | |
---|
| 214 | carType = "MR-03" |
---|
| 215 | End Sub |
---|
| 216 | #tag EndMethod |
---|
| 217 | |
---|
| 218 | #tag Method, Flags = &h21 |
---|
| 219 | Private Function prepareDB() As Boolean |
---|
| 220 | //Open the database file |
---|
| 221 | fsicsdb = new REALSQLDatabase |
---|
| 222 | dim f as FolderItem = GetFolderItem("fsicsdb") |
---|
| 223 | fsicsdb.DatabaseFile = f |
---|
| 224 | |
---|
| 225 | return f.Exists |
---|
| 226 | End Function |
---|
| 227 | #tag EndMethod |
---|
| 228 | |
---|
| 229 | #tag Method, Flags = &h21 |
---|
| 230 | Private Sub createDB() |
---|
| 231 | //Create a new database |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | //make sure we can create the file |
---|
| 235 | if fsicsdb.CreateDatabaseFile() then |
---|
| 236 | if fsicsdb.Connect() then |
---|
| 237 | dim query as string |
---|
| 238 | query = "CREATE TABLE carprofiles (id INTEGER PRIMARY KEY, name VARCHAR, cartype VARCHAR, byte01 INTEGER, byte02 INTEGER, byte03 INTEGER, byte04 INTEGER, byte05 INTEGER, byte06 INTEGER"+_ |
---|
| 239 | ", byte07 INTEGER, byte08 INTEGER, byte09 INTEGER, byte10 INTEGER, byte11 INTEGER, byte12 INTEGER, byte13 INTEGER, byte14 INTEGER, byte15 INTEGER, byte16 INTEGER, byte17 INTEGER"+_ |
---|
| 240 | ", byte18 INTEGER, UNIQUE(name))" |
---|
| 241 | fsicsdb.SQLExecute(query) |
---|
| 242 | if fsicsdb.Error then |
---|
| 243 | MsgBox "Database Error (carprofiles):" + fsicsdb.ErrorMessage |
---|
| 244 | fsicsdb.Rollback |
---|
| 245 | |
---|
| 246 | else |
---|
| 247 | fsicsdb.Commit |
---|
| 248 | end if |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | else |
---|
| 252 | MsgBox "Failed to connect to new database file" |
---|
| 253 | end if |
---|
| 254 | else |
---|
| 255 | //Failed to create database file |
---|
| 256 | MsgBox "Failed to create database file" |
---|
| 257 | end if |
---|
| 258 | End Sub |
---|
| 259 | #tag EndMethod |
---|
| 260 | |
---|
| 261 | #tag Method, Flags = &h0 |
---|
| 262 | Function createProfile(theName as String) As Boolean |
---|
| 263 | dim success as boolean |
---|
| 264 | success = false |
---|
| 265 | |
---|
| 266 | //Make sure we got a name |
---|
| 267 | if theName = "" then |
---|
| 268 | return success |
---|
| 269 | else |
---|
| 270 | //Build a new database record |
---|
| 271 | dim rec as DatabaseRecord |
---|
| 272 | rec = New DatabaseRecord |
---|
| 273 | |
---|
| 274 | rec.Column("name") = theName |
---|
| 275 | rec.Column("cartype") = carType |
---|
| 276 | rec.IntegerColumn("byte01") = asc(byte01) |
---|
| 277 | rec.IntegerColumn("byte02") = asc(byte02) |
---|
| 278 | rec.IntegerColumn("byte03") = asc(byte03) |
---|
| 279 | rec.IntegerColumn("byte04") = asc(byte04) |
---|
| 280 | rec.IntegerColumn("byte05") = asc(byte05) |
---|
| 281 | rec.IntegerColumn("byte06") = asc(byte06) |
---|
| 282 | rec.IntegerColumn("byte07") = asc(byte07) |
---|
| 283 | rec.IntegerColumn("byte08") = asc(byte08) |
---|
| 284 | rec.IntegerColumn("byte09") = asc(byte09) |
---|
| 285 | rec.IntegerColumn("byte10") = asc(byte10) |
---|
| 286 | rec.IntegerColumn("byte11") = asc(byte11) |
---|
| 287 | rec.IntegerColumn("byte12") = asc(byte12) |
---|
| 288 | rec.IntegerColumn("byte13") = asc(byte13) |
---|
| 289 | rec.IntegerColumn("byte14") = asc(byte14) |
---|
| 290 | rec.IntegerColumn("byte15") = asc(byte15) |
---|
| 291 | rec.IntegerColumn("byte16") = asc(byte16) |
---|
| 292 | rec.IntegerColumn("byte17") = asc(byte17) |
---|
| 293 | rec.IntegerColumn("byte18") = asc(byte18) |
---|
| 294 | |
---|
| 295 | fsicsdb.InsertRecord("carprofiles", rec) |
---|
| 296 | |
---|
| 297 | if fsicsdb.Error = True then |
---|
| 298 | MsgBox "Error creating profile, " + fsicsdb.ErrorMessage |
---|
| 299 | fsicsdb.Rollback |
---|
| 300 | else |
---|
| 301 | fsicsdb.Commit |
---|
| 302 | success = true |
---|
| 303 | end if |
---|
| 304 | end if |
---|
| 305 | |
---|
| 306 | Return success |
---|
| 307 | End Function |
---|
| 308 | #tag EndMethod |
---|
| 309 | |
---|
| 310 | #tag Method, Flags = &h0 |
---|
| 311 | Sub deleteProfile(theName as String) |
---|
| 312 | if theName <> "" then |
---|
| 313 | |
---|
| 314 | //Delete the profile selected |
---|
| 315 | fsicsdb.SQLExecute("DELETE FROM carprofiles WHERE name = '" + theName + "'") |
---|
| 316 | |
---|
| 317 | //Check for errors |
---|
| 318 | if fsicsdb.Error = True then |
---|
| 319 | MsgBox "Error deleting profile" |
---|
| 320 | fsicsdb.Rollback |
---|
| 321 | else |
---|
| 322 | fsicsdb.Commit |
---|
| 323 | end if |
---|
| 324 | |
---|
| 325 | else |
---|
| 326 | |
---|
| 327 | MsgBox "Please select a profile to delete" |
---|
| 328 | |
---|
| 329 | end if |
---|
| 330 | End Sub |
---|
| 331 | #tag EndMethod |
---|
| 332 | |
---|
| 333 | #tag Method, Flags = &h0 |
---|
| 334 | Sub saveProfile(theName as String) |
---|
| 335 | if theName <> "" then |
---|
| 336 | |
---|
| 337 | dim rs as RecordSet |
---|
| 338 | dim status as Boolean |
---|
| 339 | |
---|
| 340 | //Find a record |
---|
| 341 | rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'") |
---|
| 342 | |
---|
| 343 | //Make sure we got a record |
---|
| 344 | if rs <> nil then |
---|
| 345 | //delete the record before saving |
---|
| 346 | deleteProfile(theName) |
---|
| 347 | end if |
---|
| 348 | |
---|
| 349 | rs.Close |
---|
| 350 | |
---|
| 351 | status = createProfile(theName) |
---|
| 352 | |
---|
| 353 | else |
---|
| 354 | |
---|
| 355 | MsgBox "Please select a profile to modify" |
---|
| 356 | |
---|
| 357 | end if |
---|
| 358 | End Sub |
---|
| 359 | #tag EndMethod |
---|
| 360 | |
---|
| 361 | #tag Method, Flags = &h0 |
---|
| 362 | Function listProfiles() As String() |
---|
| 363 | dim rs as RecordSet |
---|
| 364 | dim s() as string |
---|
| 365 | |
---|
| 366 | //Find records |
---|
| 367 | rs = fsicsdb.SQLSelect("SELECT name FROM carprofiles") |
---|
| 368 | |
---|
| 369 | if rs <> nil then |
---|
| 370 | |
---|
| 371 | while rs.EOF = false |
---|
| 372 | s.Append rs.Field("name").StringValue |
---|
| 373 | rs.MoveNext |
---|
| 374 | wend |
---|
| 375 | |
---|
| 376 | end if |
---|
| 377 | |
---|
| 378 | rs.Close |
---|
| 379 | Return s() |
---|
| 380 | End Function |
---|
| 381 | #tag EndMethod |
---|
| 382 | |
---|
| 383 | #tag Method, Flags = &h0 |
---|
| 384 | Sub loadProfile(theName as string) |
---|
| 385 | //Find the profile in the database, update the bytes and fire the event |
---|
| 386 | dim rs as RecordSet |
---|
| 387 | |
---|
| 388 | rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'") |
---|
| 389 | |
---|
| 390 | //Make sure we got a record to work with |
---|
| 391 | if rs <> Nil then |
---|
| 392 | |
---|
| 393 | if rs.Field("cartype").StringValue <> "" then |
---|
| 394 | carType = rs.Field("cartype").StringValue |
---|
| 395 | end if |
---|
| 396 | |
---|
| 397 | if rs.Field("byte01").StringValue <> "" then |
---|
| 398 | byte01 = chrb(rs.Field("byte01").IntegerValue) |
---|
| 399 | end if |
---|
| 400 | if rs.Field("byte02").StringValue <> "" then |
---|
| 401 | byte02 = chrb(rs.Field("byte02").IntegerValue) |
---|
| 402 | end if |
---|
| 403 | if rs.Field("byte03").StringValue <> "" then |
---|
| 404 | byte03 = chrb(rs.Field("byte03").IntegerValue) |
---|
| 405 | end if |
---|
| 406 | if rs.Field("byte04").StringValue <> "" then |
---|
| 407 | byte04 = chrb(rs.Field("byte04").IntegerValue) |
---|
| 408 | end if |
---|
| 409 | if rs.Field("byte05").StringValue <> "" then |
---|
| 410 | byte05 = chrb(rs.Field("byte05").IntegerValue) |
---|
| 411 | end if |
---|
| 412 | if rs.Field("byte06").StringValue <> "" then |
---|
| 413 | byte06 = chrb(rs.Field("byte06").IntegerValue) |
---|
| 414 | end if |
---|
| 415 | if rs.Field("byte07").StringValue <> "" then |
---|
| 416 | byte07 = chrb(rs.Field("byte07").IntegerValue) |
---|
| 417 | end if |
---|
| 418 | if rs.Field("byte08").StringValue <> "" then |
---|
| 419 | byte08 = chrb(rs.Field("byte08").IntegerValue) |
---|
| 420 | end if |
---|
| 421 | if rs.Field("byte09").StringValue <> "" then |
---|
| 422 | byte09 = chrb(rs.Field("byte09").IntegerValue) |
---|
| 423 | end if |
---|
| 424 | if rs.Field("byte10").StringValue <> "" then |
---|
| 425 | byte10 = chrb(rs.Field("byte10").IntegerValue) |
---|
| 426 | end if |
---|
| 427 | if rs.Field("byte11").StringValue <> "" then |
---|
| 428 | byte11 = chrb(rs.Field("byte11").IntegerValue) |
---|
| 429 | end if |
---|
| 430 | if rs.Field("byte12").StringValue <> "" then |
---|
| 431 | byte12 = chrb(rs.Field("byte12").IntegerValue) |
---|
| 432 | end if |
---|
| 433 | if rs.Field("byte13").StringValue <> "" then |
---|
| 434 | byte13 = chrb(rs.Field("byte13").IntegerValue) |
---|
| 435 | end if |
---|
| 436 | if rs.Field("byte14").StringValue <> "" then |
---|
| 437 | byte14 = chrb(rs.Field("byte14").IntegerValue) |
---|
| 438 | end if |
---|
| 439 | if rs.Field("byte15").StringValue <> "" then |
---|
| 440 | byte15 = chrb(rs.Field("byte15").IntegerValue) |
---|
| 441 | end if |
---|
| 442 | if rs.Field("byte16").StringValue <> "" then |
---|
| 443 | byte16 = chrb(rs.Field("byte16").IntegerValue) |
---|
| 444 | end if |
---|
| 445 | if rs.Field("byte17").StringValue <> "" then |
---|
| 446 | byte17 = chrb(rs.Field("byte17").IntegerValue) |
---|
| 447 | end if |
---|
| 448 | if rs.Field("byte18").StringValue <> "" then |
---|
| 449 | byte18 = chrb(rs.Field("byte18").IntegerValue) |
---|
| 450 | end if |
---|
| 451 | |
---|
| 452 | valuesChanged() |
---|
| 453 | end if |
---|
| 454 | End Sub |
---|
| 455 | #tag EndMethod |
---|
| 456 | |
---|
| 457 | #tag Method, Flags = &h0 |
---|
| 458 | Sub exportProfile(theName as string) |
---|
| 459 | dim xml as XmlDocument |
---|
| 460 | dim root as XmlNode |
---|
| 461 | dim rootchild as XmlNode |
---|
| 462 | dim rs as RecordSet |
---|
| 463 | dim i as integer |
---|
| 464 | dim dlg as SaveAsDialog |
---|
| 465 | dim f as FolderItem |
---|
| 466 | |
---|
| 467 | dlg = New SaveAsDialog |
---|
| 468 | dlg.Title = "Export your profile" |
---|
| 469 | #if TargetLinux |
---|
| 470 | dlg.InitialDirectory = SpecialFolder.Home |
---|
| 471 | #else |
---|
| 472 | dlg.InitialDirectory = SpecialFolder.Documents |
---|
| 473 | #endif |
---|
| 474 | |
---|
| 475 | dlg.SuggestedFileName = "ics_"+theName+".xml" |
---|
| 476 | f = dlg.ShowModal() |
---|
| 477 | |
---|
| 478 | if f <> Nil then |
---|
| 479 | //Nothing to see here, move along |
---|
| 480 | else |
---|
| 481 | MsgBox "You must choose a file" |
---|
| 482 | Return |
---|
| 483 | end if |
---|
| 484 | |
---|
| 485 | //Find the record |
---|
| 486 | rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'") |
---|
| 487 | |
---|
| 488 | if rs <> Nil then |
---|
| 489 | //We have a record, so do something |
---|
| 490 | |
---|
| 491 | //Create the root element of the XML file |
---|
| 492 | xml = New XmlDocument |
---|
| 493 | root = xml.AppendChild(xml.CreateElement("icsprofile")) |
---|
| 494 | |
---|
| 495 | //Itterate all available fields and write them to the xml document |
---|
| 496 | for i = 1 to rs.FieldCount |
---|
| 497 | if rs.IdxField(i).Name <> "" and rs.IdxField(i).StringValue<> "" then |
---|
| 498 | |
---|
| 499 | rootchild = root.AppendChild(xml.CreateElement(rs.IdxField(i).Name)) |
---|
| 500 | rootchild.AppendChild(xml.CreateTextNode(rs.IdxField(i).StringValue)) |
---|
| 501 | end if |
---|
| 502 | next |
---|
| 503 | |
---|
| 504 | //Write the document to a file |
---|
| 505 | xml.SaveXml(f) |
---|
| 506 | else |
---|
| 507 | MsgBox "No profile to export" |
---|
| 508 | Return |
---|
| 509 | end if |
---|
| 510 | |
---|
| 511 | End Sub |
---|
| 512 | #tag EndMethod |
---|
| 513 | |
---|
| 514 | #tag Method, Flags = &h0 |
---|
| 515 | Function importProfile() As Boolean |
---|
| 516 | dim success as boolean |
---|
| 517 | dim dlg as OpenDialog |
---|
| 518 | dim f as FolderItem |
---|
| 519 | dim xdoc as XmlDocument |
---|
| 520 | dim root as XmlNode |
---|
| 521 | dim i as integer |
---|
| 522 | dim count as Integer |
---|
| 523 | dim item as string |
---|
| 524 | dim rec as DatabaseRecord |
---|
| 525 | dim theName as string |
---|
| 526 | |
---|
| 527 | rec = New DatabaseRecord |
---|
| 528 | success = False |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | //Get the user to select a file |
---|
| 532 | dlg = New OpenDialog |
---|
| 533 | dlg.Title = "Select a profile to import" |
---|
| 534 | #if TargetLinux |
---|
| 535 | dlg.InitialDirectory = SpecialFolder.Home |
---|
| 536 | #else |
---|
| 537 | dlg.InitialDirectory = SpecialFolder.Documents |
---|
| 538 | #endif |
---|
| 539 | f = dlg.ShowModal() |
---|
| 540 | |
---|
| 541 | //If we have a valid file we need to read it as an xml file to process it |
---|
| 542 | //Items are individually specified to prevent the program from trying to insert columns that do not exist |
---|
| 543 | //All column names should match the database table |
---|
| 544 | |
---|
| 545 | if f <> nil then |
---|
| 546 | |
---|
| 547 | //Read the XML file |
---|
| 548 | xdoc = New XmlDocument(f) |
---|
| 549 | |
---|
| 550 | count = xdoc.DocumentElement.ChildCount |
---|
| 551 | |
---|
| 552 | for i = 0 to count - 1 |
---|
| 553 | root = xdoc.DocumentElement.Child(i) |
---|
| 554 | |
---|
| 555 | item = root.FirstChild.Value |
---|
| 556 | |
---|
| 557 | select case root.Name |
---|
| 558 | |
---|
| 559 | case "name" |
---|
| 560 | theName = item |
---|
| 561 | case "cartype" |
---|
| 562 | carType = item |
---|
| 563 | case "byte01" |
---|
| 564 | byte01 = chrb(val(item)) |
---|
| 565 | case "byte02" |
---|
| 566 | byte02 = chrb(val(item)) |
---|
| 567 | case "byte03" |
---|
| 568 | byte03 = chrb(val(item)) |
---|
| 569 | case "byte04" |
---|
| 570 | byte04 = chrb(val(item)) |
---|
| 571 | case "byte05" |
---|
| 572 | byte05 = chrb(val(item)) |
---|
| 573 | case "byte06" |
---|
| 574 | byte06 = chrb(val(item)) |
---|
| 575 | case "byte07" |
---|
| 576 | byte07 = chrb(val(item)) |
---|
| 577 | case "byte08" |
---|
| 578 | byte08 = chrb(val(item)) |
---|
| 579 | case "byte09" |
---|
| 580 | byte09 = chrb(val(item)) |
---|
| 581 | case "byte10" |
---|
| 582 | byte10 = chrb(val(item)) |
---|
| 583 | case "byte11" |
---|
| 584 | byte11 = chrb(val(item)) |
---|
| 585 | case "byte12" |
---|
| 586 | byte12 = chrb(val(item)) |
---|
| 587 | case "byte13" |
---|
| 588 | byte13 = chrb(val(item)) |
---|
| 589 | case "byte14" |
---|
| 590 | byte14 = chrb(val(item)) |
---|
| 591 | case "byte15" |
---|
| 592 | byte15 = chrb(val(item)) |
---|
| 593 | case "byte16" |
---|
| 594 | byte16 = chrb(val(item)) |
---|
| 595 | case "byte17" |
---|
| 596 | byte17 = chrb(val(item)) |
---|
| 597 | case "byte18" |
---|
| 598 | byte18 = chrb(val(item)) |
---|
| 599 | |
---|
| 600 | end select |
---|
| 601 | |
---|
| 602 | next |
---|
| 603 | |
---|
| 604 | else |
---|
| 605 | //File not select we can stop |
---|
| 606 | success = False |
---|
| 607 | return success |
---|
| 608 | end if |
---|
| 609 | |
---|
| 610 | //Now check to make sure their is not an existing profile with this name |
---|
| 611 | dim rs as RecordSet |
---|
| 612 | |
---|
| 613 | rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'") |
---|
| 614 | |
---|
| 615 | if rs <> Nil then |
---|
| 616 | |
---|
| 617 | if rs.RecordCount > 0 then |
---|
| 618 | //we have a duplicate record |
---|
| 619 | //we need to prompt the user about what to do |
---|
| 620 | dim d as New MessageDialog |
---|
| 621 | dim b as MessageDialogButton |
---|
| 622 | d.Icon = MessageDialog.GraphicCaution |
---|
| 623 | d.ActionButton.Caption = "Yes" |
---|
| 624 | d.CancelButton.Visible = True |
---|
| 625 | d.CancelButton.Caption = "No" |
---|
| 626 | d.Message = "A profile exists with this name, do you want to overwrite the existing profile?" |
---|
| 627 | b = d.ShowModal |
---|
| 628 | |
---|
| 629 | //Now determine what the user chose |
---|
| 630 | Select Case b |
---|
| 631 | case d.ActionButton |
---|
| 632 | //The user wants to overwrite the record so we can just save what we have |
---|
| 633 | saveProfile(theName) |
---|
| 634 | success = True |
---|
| 635 | //Values changed because of the overwrite |
---|
| 636 | valuesChanged() |
---|
| 637 | case d.CancelButton |
---|
| 638 | //The user chose not to overwrite the record, we need to come up with a new name |
---|
| 639 | //We will increment the number until we find a name not used or we hit 32 just in case an infinite loop would occur |
---|
| 640 | dim tempString as string |
---|
| 641 | dim tempName as string |
---|
| 642 | i = 0 |
---|
| 643 | while rs.RecordCount > 0 |
---|
| 644 | i = i + 1 |
---|
| 645 | tempString = str(i) |
---|
| 646 | tempName = theName + tempString |
---|
| 647 | //Check to make sure we are not in an infinite looooooooop |
---|
| 648 | //If we are something went wrong |
---|
| 649 | if i > 31 then |
---|
| 650 | exit While |
---|
| 651 | end if |
---|
| 652 | rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name = '"+tempName+"'") |
---|
| 653 | |
---|
| 654 | wend |
---|
| 655 | //OK we have a unique name at this point |
---|
| 656 | theName = theName + tempString |
---|
| 657 | saveProfile(theName) |
---|
| 658 | success = True |
---|
| 659 | //new profile was imported |
---|
| 660 | profileImported(theName) |
---|
| 661 | end select |
---|
| 662 | |
---|
| 663 | else |
---|
| 664 | //No duplicate exists so we can just insert the record |
---|
| 665 | saveProfile(theName) |
---|
| 666 | success = True |
---|
| 667 | //new profile was imported |
---|
| 668 | profileImported(theName) |
---|
| 669 | end if |
---|
| 670 | //Close our record |
---|
| 671 | rs.Close |
---|
| 672 | |
---|
| 673 | end if |
---|
| 674 | |
---|
| 675 | Return success |
---|
| 676 | |
---|
| 677 | End Function |
---|
| 678 | #tag EndMethod |
---|
| 679 | |
---|
| 680 | |
---|
| 681 | #tag Hook, Flags = &h0 |
---|
| 682 | Event valuesChanged() |
---|
| 683 | #tag EndHook |
---|
| 684 | |
---|
| 685 | #tag Hook, Flags = &h0 |
---|
| 686 | Event profileImported(profileName as string) |
---|
| 687 | #tag EndHook |
---|
| 688 | |
---|
| 689 | |
---|
| 690 | #tag Note, Name = General |
---|
| 691 | Car type must be set for this to operate |
---|
| 692 | Car types supported by this application are |
---|
| 693 | MR-03 |
---|
| 694 | dNaNo |
---|
| 695 | ASF |
---|
| 696 | #tag EndNote |
---|
| 697 | |
---|
| 698 | |
---|
| 699 | #tag Property, Flags = &h0 |
---|
| 700 | byte01 As String |
---|
| 701 | #tag EndProperty |
---|
| 702 | |
---|
| 703 | #tag Property, Flags = &h0 |
---|
| 704 | byte11 As String |
---|
| 705 | #tag EndProperty |
---|
| 706 | |
---|
| 707 | #tag Property, Flags = &h0 |
---|
| 708 | byte12 As String |
---|
| 709 | #tag EndProperty |
---|
| 710 | |
---|
| 711 | #tag Property, Flags = &h0 |
---|
| 712 | byte13 As String |
---|
| 713 | #tag EndProperty |
---|
| 714 | |
---|
| 715 | #tag Property, Flags = &h0 |
---|
| 716 | byte14 As String |
---|
| 717 | #tag EndProperty |
---|
| 718 | |
---|
| 719 | #tag Property, Flags = &h0 |
---|
| 720 | byte15 As String |
---|
| 721 | #tag EndProperty |
---|
| 722 | |
---|
| 723 | #tag Property, Flags = &h0 |
---|
| 724 | byte16 As String |
---|
| 725 | #tag EndProperty |
---|
| 726 | |
---|
| 727 | #tag Property, Flags = &h0 |
---|
| 728 | byte17 As String |
---|
| 729 | #tag EndProperty |
---|
| 730 | |
---|
| 731 | #tag Property, Flags = &h0 |
---|
| 732 | byte18 As String |
---|
| 733 | #tag EndProperty |
---|
| 734 | |
---|
| 735 | #tag Property, Flags = &h0 |
---|
| 736 | byte02 As String |
---|
| 737 | #tag EndProperty |
---|
| 738 | |
---|
| 739 | #tag Property, Flags = &h0 |
---|
| 740 | byte03 As String |
---|
| 741 | #tag EndProperty |
---|
| 742 | |
---|
| 743 | #tag Property, Flags = &h0 |
---|
| 744 | byte04 As String |
---|
| 745 | #tag EndProperty |
---|
| 746 | |
---|
| 747 | #tag Property, Flags = &h0 |
---|
| 748 | byte05 As String |
---|
| 749 | #tag EndProperty |
---|
| 750 | |
---|
| 751 | #tag Property, Flags = &h0 |
---|
| 752 | byte06 As String |
---|
| 753 | #tag EndProperty |
---|
| 754 | |
---|
| 755 | #tag Property, Flags = &h0 |
---|
| 756 | byte07 As String |
---|
| 757 | #tag EndProperty |
---|
| 758 | |
---|
| 759 | #tag Property, Flags = &h0 |
---|
| 760 | byte08 As String |
---|
| 761 | #tag EndProperty |
---|
| 762 | |
---|
| 763 | #tag Property, Flags = &h0 |
---|
| 764 | byte09 As String |
---|
| 765 | #tag EndProperty |
---|
| 766 | |
---|
| 767 | #tag Property, Flags = &h0 |
---|
| 768 | byte10 As String |
---|
| 769 | #tag EndProperty |
---|
| 770 | |
---|
| 771 | #tag Property, Flags = &h0 |
---|
| 772 | carType As String = "MR-03" |
---|
| 773 | #tag EndProperty |
---|
| 774 | |
---|
| 775 | #tag Property, Flags = &h0 |
---|
| 776 | fsicsdb As REALSQLDatabase |
---|
| 777 | #tag EndProperty |
---|
| 778 | |
---|
| 779 | #tag Property, Flags = &h0 |
---|
| 780 | mode As String = "none" |
---|
| 781 | #tag EndProperty |
---|
| 782 | |
---|
| 783 | |
---|
| 784 | #tag ViewBehavior |
---|
| 785 | #tag ViewProperty |
---|
| 786 | Name="Name" |
---|
| 787 | Visible=true |
---|
| 788 | Group="ID" |
---|
| 789 | InheritedFrom="serial" |
---|
| 790 | #tag EndViewProperty |
---|
| 791 | #tag ViewProperty |
---|
| 792 | Name="Index" |
---|
| 793 | Visible=true |
---|
| 794 | Group="ID" |
---|
| 795 | Type="Integer" |
---|
| 796 | InheritedFrom="serial" |
---|
| 797 | #tag EndViewProperty |
---|
| 798 | #tag ViewProperty |
---|
| 799 | Name="Super" |
---|
| 800 | Visible=true |
---|
| 801 | Group="ID" |
---|
| 802 | InheritedFrom="serial" |
---|
| 803 | #tag EndViewProperty |
---|
| 804 | #tag ViewProperty |
---|
| 805 | Name="Left" |
---|
| 806 | Visible=true |
---|
| 807 | Group="Position" |
---|
| 808 | InheritedFrom="serial" |
---|
| 809 | #tag EndViewProperty |
---|
| 810 | #tag ViewProperty |
---|
| 811 | Name="Top" |
---|
| 812 | Visible=true |
---|
| 813 | Group="Position" |
---|
| 814 | InheritedFrom="serial" |
---|
| 815 | #tag EndViewProperty |
---|
| 816 | #tag ViewProperty |
---|
| 817 | Name="Baud" |
---|
| 818 | Visible=true |
---|
| 819 | Group="Behavior" |
---|
| 820 | InitialValue="13" |
---|
| 821 | Type="Integer" |
---|
| 822 | EditorType="Enum" |
---|
| 823 | InheritedFrom="serial" |
---|
| 824 | #tag EnumValues |
---|
| 825 | "0 - 300" |
---|
| 826 | "1 - 600" |
---|
| 827 | "2 - 1200" |
---|
| 828 | "3 - 1800" |
---|
| 829 | "4 - 2400" |
---|
| 830 | "5 - 3600" |
---|
| 831 | "6 - 4800" |
---|
| 832 | "7 - 7200" |
---|
| 833 | "8 - 9600" |
---|
| 834 | "9 - 14400" |
---|
| 835 | "10 - 19200" |
---|
| 836 | "11 - 28800" |
---|
| 837 | "12 - 38400" |
---|
| 838 | "13 - 57600" |
---|
| 839 | "14 - 115200" |
---|
| 840 | "15 - 230400" |
---|
| 841 | #tag EndEnumValues |
---|
| 842 | #tag EndViewProperty |
---|
| 843 | #tag ViewProperty |
---|
| 844 | Name="Bits" |
---|
| 845 | Visible=true |
---|
| 846 | Group="Behavior" |
---|
| 847 | InitialValue="3" |
---|
| 848 | Type="Integer" |
---|
| 849 | EditorType="Enum" |
---|
| 850 | InheritedFrom="serial" |
---|
| 851 | #tag EnumValues |
---|
| 852 | "0 - 5 Data Bits" |
---|
| 853 | "1 - 6 Data Bits" |
---|
| 854 | "2 - 7 Data Bits" |
---|
| 855 | "3 - 8 Data bits" |
---|
| 856 | #tag EndEnumValues |
---|
| 857 | #tag EndViewProperty |
---|
| 858 | #tag ViewProperty |
---|
| 859 | Name="Parity" |
---|
| 860 | Visible=true |
---|
| 861 | Group="Behavior" |
---|
| 862 | InitialValue="0" |
---|
| 863 | Type="Integer" |
---|
| 864 | EditorType="Enum" |
---|
| 865 | InheritedFrom="serial" |
---|
| 866 | #tag EnumValues |
---|
| 867 | "0 - No Parity" |
---|
| 868 | "1 - Odd Parity" |
---|
| 869 | "2 - EvenParity" |
---|
| 870 | #tag EndEnumValues |
---|
| 871 | #tag EndViewProperty |
---|
| 872 | #tag ViewProperty |
---|
| 873 | Name="Stop" |
---|
| 874 | Visible=true |
---|
| 875 | Group="Behavior" |
---|
| 876 | InitialValue="0" |
---|
| 877 | Type="Integer" |
---|
| 878 | EditorType="Enum" |
---|
| 879 | InheritedFrom="serial" |
---|
| 880 | #tag EnumValues |
---|
| 881 | "0 - 1 Stop Bit" |
---|
| 882 | "1 - 1.5 Stop Bits" |
---|
| 883 | "2 - 2 Stop Bits" |
---|
| 884 | #tag EndEnumValues |
---|
| 885 | #tag EndViewProperty |
---|
| 886 | #tag ViewProperty |
---|
| 887 | Name="XON" |
---|
| 888 | Visible=true |
---|
| 889 | Group="Behavior" |
---|
| 890 | Type="Boolean" |
---|
| 891 | InheritedFrom="serial" |
---|
| 892 | #tag EndViewProperty |
---|
| 893 | #tag ViewProperty |
---|
| 894 | Name="CTS" |
---|
| 895 | Visible=true |
---|
| 896 | Group="Behavior" |
---|
| 897 | Type="Boolean" |
---|
| 898 | InheritedFrom="serial" |
---|
| 899 | #tag EndViewProperty |
---|
| 900 | #tag ViewProperty |
---|
| 901 | Name="DTR" |
---|
| 902 | Visible=true |
---|
| 903 | Group="Behavior" |
---|
| 904 | Type="Boolean" |
---|
| 905 | InheritedFrom="serial" |
---|
| 906 | #tag EndViewProperty |
---|
| 907 | #tag ViewProperty |
---|
| 908 | Name="byte01" |
---|
| 909 | Group="Behavior" |
---|
| 910 | InitialValue="&hFF" |
---|
| 911 | Type="String" |
---|
| 912 | EditorType="MultiLineEditor" |
---|
| 913 | #tag EndViewProperty |
---|
| 914 | #tag ViewProperty |
---|
| 915 | Name="byte11" |
---|
| 916 | Group="Behavior" |
---|
| 917 | Type="String" |
---|
| 918 | EditorType="MultiLineEditor" |
---|
| 919 | #tag EndViewProperty |
---|
| 920 | #tag ViewProperty |
---|
| 921 | Name="byte12" |
---|
| 922 | Group="Behavior" |
---|
| 923 | Type="String" |
---|
| 924 | EditorType="MultiLineEditor" |
---|
| 925 | #tag EndViewProperty |
---|
| 926 | #tag ViewProperty |
---|
| 927 | Name="byte13" |
---|
| 928 | Group="Behavior" |
---|
| 929 | Type="String" |
---|
| 930 | EditorType="MultiLineEditor" |
---|
| 931 | #tag EndViewProperty |
---|
| 932 | #tag ViewProperty |
---|
| 933 | Name="byte14" |
---|
| 934 | Group="Behavior" |
---|
| 935 | Type="String" |
---|
| 936 | EditorType="MultiLineEditor" |
---|
| 937 | #tag EndViewProperty |
---|
| 938 | #tag ViewProperty |
---|
| 939 | Name="byte15" |
---|
| 940 | Group="Behavior" |
---|
| 941 | Type="String" |
---|
| 942 | EditorType="MultiLineEditor" |
---|
| 943 | #tag EndViewProperty |
---|
| 944 | #tag ViewProperty |
---|
| 945 | Name="byte16" |
---|
| 946 | Group="Behavior" |
---|
| 947 | Type="String" |
---|
| 948 | EditorType="MultiLineEditor" |
---|
| 949 | #tag EndViewProperty |
---|
| 950 | #tag ViewProperty |
---|
| 951 | Name="byte17" |
---|
| 952 | Group="Behavior" |
---|
| 953 | Type="String" |
---|
| 954 | EditorType="MultiLineEditor" |
---|
| 955 | #tag EndViewProperty |
---|
| 956 | #tag ViewProperty |
---|
| 957 | Name="byte18" |
---|
| 958 | Group="Behavior" |
---|
| 959 | Type="String" |
---|
| 960 | EditorType="MultiLineEditor" |
---|
| 961 | #tag EndViewProperty |
---|
| 962 | #tag ViewProperty |
---|
| 963 | Name="byte02" |
---|
| 964 | Group="Behavior" |
---|
| 965 | Type="String" |
---|
| 966 | EditorType="MultiLineEditor" |
---|
| 967 | #tag EndViewProperty |
---|
| 968 | #tag ViewProperty |
---|
| 969 | Name="byte03" |
---|
| 970 | Group="Behavior" |
---|
| 971 | Type="String" |
---|
| 972 | EditorType="MultiLineEditor" |
---|
| 973 | #tag EndViewProperty |
---|
| 974 | #tag ViewProperty |
---|
| 975 | Name="byte04" |
---|
| 976 | Group="Behavior" |
---|
| 977 | Type="String" |
---|
| 978 | EditorType="MultiLineEditor" |
---|
| 979 | #tag EndViewProperty |
---|
| 980 | #tag ViewProperty |
---|
| 981 | Name="byte05" |
---|
| 982 | Group="Behavior" |
---|
| 983 | Type="String" |
---|
| 984 | EditorType="MultiLineEditor" |
---|
| 985 | #tag EndViewProperty |
---|
| 986 | #tag ViewProperty |
---|
| 987 | Name="byte06" |
---|
| 988 | Group="Behavior" |
---|
| 989 | Type="String" |
---|
| 990 | EditorType="MultiLineEditor" |
---|
| 991 | #tag EndViewProperty |
---|
| 992 | #tag ViewProperty |
---|
| 993 | Name="byte07" |
---|
| 994 | Group="Behavior" |
---|
| 995 | Type="String" |
---|
| 996 | EditorType="MultiLineEditor" |
---|
| 997 | #tag EndViewProperty |
---|
| 998 | #tag ViewProperty |
---|
| 999 | Name="byte08" |
---|
| 1000 | Group="Behavior" |
---|
| 1001 | Type="String" |
---|
| 1002 | EditorType="MultiLineEditor" |
---|
| 1003 | #tag EndViewProperty |
---|
| 1004 | #tag ViewProperty |
---|
| 1005 | Name="byte09" |
---|
| 1006 | Group="Behavior" |
---|
| 1007 | Type="String" |
---|
| 1008 | EditorType="MultiLineEditor" |
---|
| 1009 | #tag EndViewProperty |
---|
| 1010 | #tag ViewProperty |
---|
| 1011 | Name="byte10" |
---|
| 1012 | Group="Behavior" |
---|
| 1013 | Type="String" |
---|
| 1014 | EditorType="MultiLineEditor" |
---|
| 1015 | #tag EndViewProperty |
---|
| 1016 | #tag ViewProperty |
---|
| 1017 | Name="carType" |
---|
| 1018 | Group="Behavior" |
---|
| 1019 | InitialValue="MR-03" |
---|
| 1020 | Type="String" |
---|
| 1021 | #tag EndViewProperty |
---|
| 1022 | #tag ViewProperty |
---|
| 1023 | Name="mode" |
---|
| 1024 | Group="Behavior" |
---|
| 1025 | InitialValue="read" |
---|
| 1026 | Type="String" |
---|
| 1027 | #tag EndViewProperty |
---|
| 1028 | #tag EndViewBehavior |
---|
| 1029 | End Class |
---|
| 1030 | #tag EndClass |
---|