Index: trunk/desktop/mainWindow.rbfrm
===================================================================
--- trunk/desktop/mainWindow.rbfrm	(revision 9)
+++ trunk/desktop/mainWindow.rbfrm	(revision 10)
@@ -2630,6 +2630,6 @@
 
 #tag WindowCode
-	#tag Method, Flags = &h0
-		Sub disableControls()
+	#tag Method, Flags = &h21
+		Private Sub disableControls()
 		  select case ICSSerialPort1.carType
 		    
@@ -2678,6 +2678,6 @@
 	#tag EndMethod
 
-	#tag Method, Flags = &h0
-		Sub updateCarScreen()
+	#tag Method, Flags = &h21
+		Private Sub updateCarScreen()
 		  //Use this method to update the car screen
 		  
@@ -2857,6 +2857,6 @@
 	#tag EndMethod
 
-	#tag Method, Flags = &h0
-		Sub updateAdvancedScreen()
+	#tag Method, Flags = &h21
+		Private Sub updateAdvancedScreen()
 		  //Update the fields on the advanced screen to match current values
 		  EditField_Byte1.Text = str(asc(ICSSerialPort1.byte01))
@@ -2880,4 +2880,69 @@
 		End Sub
 	#tag EndMethod
+
+	#tag Method, Flags = &h21
+		Private Sub readPreferences()
+		  dim filePreference as FolderItem = GetFolderItem("icspref.xml")
+		  dim xdoc as XmlDocument
+		  dim root as XmlNode
+		  dim i as Integer
+		  dim count as integer
+		  dim item as string
+		  
+		  //Read the settings from the preferences file
+		  if filePreference <> Nil then
+		    xdoc = New XmlDocument(filePreference)
+		    
+		    count = xdoc.DocumentElement.ChildCount
+		    
+		    for i = 0 to count - 1
+		      root = xdoc.DocumentElement.Child(i)
+		      
+		      item = root.FirstChild.Value
+		      
+		      select case root.Name
+		      case "comPort"
+		        comPort = item
+		      end select
+		    next
+		    
+		  else
+		    //Do nothing
+		    
+		  end if
+		End Sub
+	#tag EndMethod
+
+	#tag Method, Flags = &h21
+		Private Sub savePreferences()
+		  dim xml as XmlDocument
+		  dim root as XMLNode
+		  dim comPortXML as XmlNode
+		  dim f as FolderItem
+		  
+		  //Save the preferences
+		  
+		  f = GetFolderItem("icspref.xml")
+		  
+		  if f <> Nil then
+		    //Create a new XML document
+		    xml = New XmlDocument
+		    root = xml.AppendChild(xml.CreateElement("icspref"))
+		    
+		    //Add elements
+		    comPortXML = root.AppendChild(xml.CreateElement("comPort"))
+		    comPortXML.AppendChild(xml.CreateTextNode(comPort))
+		    
+		    //Save the output
+		    xml.SaveXml(f)
+		  end if
+		  
+		End Sub
+	#tag EndMethod
+
+
+	#tag Property, Flags = &h0
+		comPort As string
+	#tag EndProperty
 
 
Index: trunk/desktop/ICSSerialPort.rbbas
===================================================================
--- trunk/desktop/ICSSerialPort.rbbas	(revision 9)
+++ trunk/desktop/ICSSerialPort.rbbas	(revision 10)
@@ -6,51 +6,76 @@
 		  //This sends the command to reset the car back to a default configuration
 		  //Defaults are different for each car type
-		  
-		  dim sendstring as string
+		  setDefaultValues
+		  
+		  //Send the default values to the car
+		  writeCar
+		End Sub
+	#tag EndMethod
+
+	#tag Method, Flags = &h0
+		Sub setDefaultValues()
+		  //Set the defaults for the bytes sent to a car
 		  
 		  select case carType
 		    
 		  case "MR-03"
-		    
+		    byte01 = chrb(&hD5)
+		    byte02 = chrb(&h5A)
+		    byte03 = chrb(&h64)
+		    byte04 = chrb(&hFF)
+		    byte05 = chrb(&h02)
+		    byte06 = chrb(&h02)
+		    byte07 = chrb(&h01)
+		    byte08 = chrb(&hFF)
+		    byte09 = chrb(&hBC)
+		    byte10 = chrb(&h44)
+		    byte11 = chrb(&h88)
+		    byte12 = chrb(&h78)
+		    byte13 = chrb(&hFF)
+		    byte14 = chrb(&h2C)
+		    byte15 = chrb(&h05)
+		    byte16 = chrb(&h5A)
+		    byte17 = chrb(&h3C)
+		    byte18 = chrb(&h87)
 		    
 		  case "dNaNo"
-		    
+		    byte01 = chrb(&hD5)
+		    byte02 = chrb(&h5A)
+		    byte03 = chrb(&hFF)
+		    byte04 = chrb(&hFF)
+		    byte05 = chrb(&h0A)
+		    byte06 = chrb(&h03)
+		    byte07 = chrb(&h01)
+		    byte08 = chrb(&h40)
+		    byte09 = chrb(&hBC)
+		    byte10 = chrb(&h44)
+		    byte11 = chrb(&h88)
+		    byte12 = chrb(&h78)
+		    byte13 = chrb(&h03)
+		    byte14 = chrb(&hFF)
+		    byte15 = chrb(&hFF)
+		    byte16 = chrb(&h80)
+		    byte17 = chrb(&h80)
+		    byte18 = chrb(&hA7)
 		    
 		  case "ASF"
-		    
-		    
-		  case "AD"
-		    
-		    
-		  else
-		    //default thing to do
-		    MsgBox "Error invalid car type"
-		    Return
-		    
-		  end select
-		  
-		  me.Write(sendstring)
-		End Sub
-	#tag EndMethod
-
-	#tag Method, Flags = &h0
-		Sub setDefaultValues()
-		  //Set the defaults for the bytes sent to a car
-		  
-		  dim sendstring as string
-		  
-		  select case carType
-		    
-		  case "MR-03"
-		    
-		    
-		  case "dNaNo"
-		    
-		    
-		  case "ASF"
-		    
-		    
-		  case "AD"
-		    
+		    byte01 = chrb(&hD5)
+		    byte02 = chrb(&h5A)
+		    byte03 = chrb(&hFF)
+		    byte04 = chrb(&hFF)
+		    byte05 = chrb(&h0A)
+		    byte06 = chrb(&h03)
+		    byte07 = chrb(&h01)
+		    byte08 = chrb(&h78)
+		    byte09 = chrb(&hBC)
+		    byte10 = chrb(&h44)
+		    byte11 = chrb(&h88)
+		    byte12 = chrb(&h78)
+		    byte13 = chrb(&h03)
+		    byte14 = chrb(&hFF)
+		    byte15 = chrb(&hFF)
+		    byte16 = chrb(&hFF)
+		    byte17 = chrb(&hFF)
+		    byte18 = chrb(&hDD)
 		    
 		  else
@@ -103,4 +128,25 @@
 		Sub calculateChecksum()
 		  //Use this to calculate byte 18, the checksum
+		  //The checksum is just adding bytes 2-17 together but rounded at each byte
+		  
+		  dim i as integer
+		  
+		  i = (asc(byte02) + asc(byte03)) mod &h100
+		  i = (i + asc(byte04)) mod &h100
+		  i = (i + asc(byte05)) mod &h100
+		  i = (i + asc(byte06)) mod &h100
+		  i = (i + asc(byte07)) mod &h100
+		  i = (i + asc(byte08)) mod &h100
+		  i = (i + asc(byte09)) mod &h100
+		  i = (i + asc(byte10)) mod &h100
+		  i = (i + asc(byte11)) mod &h100
+		  i = (i + asc(byte12)) mod &h100
+		  i = (i + asc(byte13)) mod &h100
+		  i = (i + asc(byte14)) mod &h100
+		  i = (i + asc(byte15)) mod &h100
+		  i = (i + asc(byte16)) mod &h100
+		  i = (i + asc(byte17)) mod &h100
+		  
+		  byte18 = chrb(i)
 		  
 		  valuesChanged()
@@ -111,4 +157,61 @@
 		Sub readCar()
 		  //Read values from the currently attached car
+		End Sub
+	#tag EndMethod
+
+	#tag Method, Flags = &h0
+		Sub Constructor()
+		  //Make sure we have a database and if not create it
+		  dim exists as boolean
+		  
+		  exists = prepareDB()
+		  if exists = false then
+		    //No database available, create one
+		    createDB()
+		  else
+		    //Database exists, connect to it
+		    if fsicsdb.Connect = false then
+		      MsgBox "Database connection failed"
+		    end if
+		  end if
+		End Sub
+	#tag EndMethod
+
+	#tag Method, Flags = &h21
+		Private Function prepareDB() As Boolean
+		  //Open the database file
+		  fsicsdb = new REALSQLDatabase
+		  dim f as FolderItem = GetFolderItem("fsicsdb")
+		  fsicsdb.DatabaseFile = f
+		  
+		  return f.Exists
+		End Function
+	#tag EndMethod
+
+	#tag Method, Flags = &h21
+		Private Sub createDB()
+		  //Create a new database
+		  
+		  
+		  //make sure we can create the file
+		  if fsicsdb.CreateDatabaseFile() then
+		    if fsicsdb.Connect() then
+		      dim query as string
+		      query = "CREATE TABLE carprofiles (id INTEGER PRIMARY KEY, name VARCHAR, cartype VARCHAR, byte01 VARCHAR, byte02 VARCHAR, byte03 VARCHAR, byte04 VARCHAR, byte05 VARCHAR, byte06 VARCHAR"+_
+		      ", byte07 VARCHAR, byte08 VARCHAR, byte09 VARCHAR, byte10 VARCHAR, byte11 VARCHAR, byte12 VARCHAR, byte13 VARCHAR, byte14 VARCHAR, byte15 VARCHAR, byte16 VARCHAR, byte17 VARCHAR"+_
+		      ", byte18 VARCHAR, UNIQUE(name))"
+		      fsicsdb.SQLExecute(query)
+		      if fsicsdb.Error then
+		        MsgBox "Database Error (carprofiles):" + fsicsdb.ErrorMessage
+		      end if
+		      
+		      
+		    else
+		      MsgBox "Failed to connect to new database file"
+		    end if
+		  else
+		    //Failed to create database file
+		    MsgBox "Failed to create database file"
+		  end if
 		End Sub
 	#tag EndMethod
@@ -126,5 +229,4 @@
 		dNaNo
 		ASF
-		AD
 	#tag EndNote
 
@@ -204,4 +306,8 @@
 	#tag Property, Flags = &h0
 		carType As String = "MR-03"
+	#tag EndProperty
+
+	#tag Property, Flags = &h0
+		fsicsdb As REALSQLDatabase
 	#tag EndProperty
 
