#tag Class Protected Class ICSSerialPort Inherits serial #tag Method, Flags = &h0 Sub resetCar() //This sends the command to reset the car back to a default configuration //Defaults are different for each car type 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" 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 //default thing to do MsgBox "Error invalid car type" return end select //trigger the event definition so main program knows the values changed valuesChanged() End Sub #tag EndMethod #tag Method, Flags = &h0 Sub setCarType(value as string) //When setting car type we must reconfigure the serial port //Some car types have a different baud rate select case value case "MR-03" carType = value byte15 = chrb(&h05) byte16 = chrb(&h5A) byte17 = chrb(&h3C) case "dNaNo" carType = value byte15 = chrb(&hFF) byte16 = chrb(&h80) byte17 = chrb(&h80) case "ASF" carType = value byte15 = chrb(&hFF) byte16 = chrb(&hFF) byte17 = chrb(&hFF) else MsgBox "Error setting Car Type" end select valuesChanged() End Sub #tag EndMethod #tag Method, Flags = &h0 Sub writeCar() //Send set bytes to the car mode = "write" dim sendstring as string calculateChecksum() sendstring = byte01 + byte02 + byte03 + byte04 + byte05 + byte06 + byte07 + byte08 + byte09 + byte10 + byte11 + byte12 + byte13 + byte14 + byte15 + byte16 + byte17 + byte18 me.Write(sendstring) End Sub #tag EndMethod #tag Method, Flags = &h0 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) End Sub #tag EndMethod #tag Method, Flags = &h0 Sub readCar() //Read values from the currently attached car dim sendstring as string mode = "read" sendstring = chrb(&hC5) me.Write(sendstring) 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 //Set default byte values 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) carType = "MR-03" 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 INTEGER, byte02 INTEGER, byte03 INTEGER, byte04 INTEGER, byte05 INTEGER, byte06 INTEGER"+_ ", byte07 INTEGER, byte08 INTEGER, byte09 INTEGER, byte10 INTEGER, byte11 INTEGER, byte12 INTEGER, byte13 INTEGER, byte14 INTEGER, byte15 INTEGER, byte16 INTEGER, byte17 INTEGER"+_ ", byte18 INTEGER, UNIQUE(name))" fsicsdb.SQLExecute(query) if fsicsdb.Error then MsgBox "Database Error (carprofiles):" + fsicsdb.ErrorMessage fsicsdb.Rollback else fsicsdb.Commit 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 #tag Method, Flags = &h0 Function createProfile(theName as String) As Boolean dim success as boolean success = false //Make sure we got a name if theName = "" then return success else //Build a new database record dim rec as DatabaseRecord rec = New DatabaseRecord rec.Column("name") = theName rec.Column("cartype") = carType rec.IntegerColumn("byte01") = asc(byte01) rec.IntegerColumn("byte02") = asc(byte02) rec.IntegerColumn("byte03") = asc(byte03) rec.IntegerColumn("byte04") = asc(byte04) rec.IntegerColumn("byte05") = asc(byte05) rec.IntegerColumn("byte06") = asc(byte06) rec.IntegerColumn("byte07") = asc(byte07) rec.IntegerColumn("byte08") = asc(byte08) rec.IntegerColumn("byte09") = asc(byte09) rec.IntegerColumn("byte10") = asc(byte10) rec.IntegerColumn("byte11") = asc(byte11) rec.IntegerColumn("byte12") = asc(byte12) rec.IntegerColumn("byte13") = asc(byte13) rec.IntegerColumn("byte14") = asc(byte14) rec.IntegerColumn("byte15") = asc(byte15) rec.IntegerColumn("byte16") = asc(byte16) rec.IntegerColumn("byte17") = asc(byte17) rec.IntegerColumn("byte18") = asc(byte18) fsicsdb.InsertRecord("carprofiles", rec) if fsicsdb.Error = True then MsgBox "Error creating profile, " + fsicsdb.ErrorMessage fsicsdb.Rollback else fsicsdb.Commit success = true end if end if Return success End Function #tag EndMethod #tag Method, Flags = &h0 Sub deleteProfile(theName as String) if theName <> "" then //Delete the profile selected fsicsdb.SQLExecute("DELETE FROM carprofiles WHERE name = '" + theName + "'") //Check for errors if fsicsdb.Error = True then MsgBox "Error deleting profile" fsicsdb.Rollback else fsicsdb.Commit end if else MsgBox "Please select a profile to delete" end if End Sub #tag EndMethod #tag Method, Flags = &h0 Sub saveProfile(theName as String) if theName <> "" then dim rs as RecordSet dim status as Boolean //Find a record rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'") //Make sure we got a record if rs <> nil then //delete the record before saving deleteProfile(theName) end if rs.Close status = createProfile(theName) else MsgBox "Please select a profile to modify" end if End Sub #tag EndMethod #tag Method, Flags = &h0 Function listProfiles() As String() dim rs as RecordSet dim s() as string //Find records rs = fsicsdb.SQLSelect("SELECT name FROM carprofiles") if rs <> nil then while rs.EOF = false s.Append rs.Field("name").StringValue rs.MoveNext wend end if rs.Close Return s() End Function #tag EndMethod #tag Method, Flags = &h0 Sub loadProfile(theName as string) //Find the profile in the database, update the bytes and fire the event dim rs as RecordSet rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'") //Make sure we got a record to work with if rs <> Nil then if rs.Field("cartype").StringValue <> "" then carType = rs.Field("cartype").StringValue end if if rs.Field("byte01").StringValue <> "" then byte01 = chrb(rs.Field("byte01").IntegerValue) end if if rs.Field("byte02").StringValue <> "" then byte02 = chrb(rs.Field("byte02").IntegerValue) end if if rs.Field("byte03").StringValue <> "" then byte03 = chrb(rs.Field("byte03").IntegerValue) end if if rs.Field("byte04").StringValue <> "" then byte04 = chrb(rs.Field("byte04").IntegerValue) end if if rs.Field("byte05").StringValue <> "" then byte05 = chrb(rs.Field("byte05").IntegerValue) end if if rs.Field("byte06").StringValue <> "" then byte06 = chrb(rs.Field("byte06").IntegerValue) end if if rs.Field("byte07").StringValue <> "" then byte07 = chrb(rs.Field("byte07").IntegerValue) end if if rs.Field("byte08").StringValue <> "" then byte08 = chrb(rs.Field("byte08").IntegerValue) end if if rs.Field("byte09").StringValue <> "" then byte09 = chrb(rs.Field("byte09").IntegerValue) end if if rs.Field("byte10").StringValue <> "" then byte10 = chrb(rs.Field("byte10").IntegerValue) end if if rs.Field("byte11").StringValue <> "" then byte11 = chrb(rs.Field("byte11").IntegerValue) end if if rs.Field("byte12").StringValue <> "" then byte12 = chrb(rs.Field("byte12").IntegerValue) end if if rs.Field("byte13").StringValue <> "" then byte13 = chrb(rs.Field("byte13").IntegerValue) end if if rs.Field("byte14").StringValue <> "" then byte14 = chrb(rs.Field("byte14").IntegerValue) end if if rs.Field("byte15").StringValue <> "" then byte15 = chrb(rs.Field("byte15").IntegerValue) end if if rs.Field("byte16").StringValue <> "" then byte16 = chrb(rs.Field("byte16").IntegerValue) end if if rs.Field("byte17").StringValue <> "" then byte17 = chrb(rs.Field("byte17").IntegerValue) end if if rs.Field("byte18").StringValue <> "" then byte18 = chrb(rs.Field("byte18").IntegerValue) end if valuesChanged() end if End Sub #tag EndMethod #tag Hook, Flags = &h0 Event valuesChanged() #tag EndHook #tag Note, Name = General Car type must be set for this to operate Car types supported by this application are MR-03 dNaNo ASF #tag EndNote #tag Property, Flags = &h0 byte01 As String #tag EndProperty #tag Property, Flags = &h0 byte11 As String #tag EndProperty #tag Property, Flags = &h0 byte12 As String #tag EndProperty #tag Property, Flags = &h0 byte13 As String #tag EndProperty #tag Property, Flags = &h0 byte14 As String #tag EndProperty #tag Property, Flags = &h0 byte15 As String #tag EndProperty #tag Property, Flags = &h0 byte16 As String #tag EndProperty #tag Property, Flags = &h0 byte17 As String #tag EndProperty #tag Property, Flags = &h0 byte18 As String #tag EndProperty #tag Property, Flags = &h0 byte02 As String #tag EndProperty #tag Property, Flags = &h0 byte03 As String #tag EndProperty #tag Property, Flags = &h0 byte04 As String #tag EndProperty #tag Property, Flags = &h0 byte05 As String #tag EndProperty #tag Property, Flags = &h0 byte06 As String #tag EndProperty #tag Property, Flags = &h0 byte07 As String #tag EndProperty #tag Property, Flags = &h0 byte08 As String #tag EndProperty #tag Property, Flags = &h0 byte09 As String #tag EndProperty #tag Property, Flags = &h0 byte10 As String #tag EndProperty #tag Property, Flags = &h0 carType As String = "MR-03" #tag EndProperty #tag Property, Flags = &h0 fsicsdb As REALSQLDatabase #tag EndProperty #tag Property, Flags = &h0 mode As String = "none" #tag EndProperty #tag ViewBehavior #tag ViewProperty Name="Name" Visible=true Group="ID" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="Index" Visible=true Group="ID" Type="Integer" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="Super" Visible=true Group="ID" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="Left" Visible=true Group="Position" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="Top" Visible=true Group="Position" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="Baud" Visible=true Group="Behavior" InitialValue="13" Type="Integer" EditorType="Enum" InheritedFrom="serial" #tag EnumValues "0 - 300" "1 - 600" "2 - 1200" "3 - 1800" "4 - 2400" "5 - 3600" "6 - 4800" "7 - 7200" "8 - 9600" "9 - 14400" "10 - 19200" "11 - 28800" "12 - 38400" "13 - 57600" "14 - 115200" "15 - 230400" #tag EndEnumValues #tag EndViewProperty #tag ViewProperty Name="Bits" Visible=true Group="Behavior" InitialValue="3" Type="Integer" EditorType="Enum" InheritedFrom="serial" #tag EnumValues "0 - 5 Data Bits" "1 - 6 Data Bits" "2 - 7 Data Bits" "3 - 8 Data bits" #tag EndEnumValues #tag EndViewProperty #tag ViewProperty Name="Parity" Visible=true Group="Behavior" InitialValue="0" Type="Integer" EditorType="Enum" InheritedFrom="serial" #tag EnumValues "0 - No Parity" "1 - Odd Parity" "2 - EvenParity" #tag EndEnumValues #tag EndViewProperty #tag ViewProperty Name="Stop" Visible=true Group="Behavior" InitialValue="0" Type="Integer" EditorType="Enum" InheritedFrom="serial" #tag EnumValues "0 - 1 Stop Bit" "1 - 1.5 Stop Bits" "2 - 2 Stop Bits" #tag EndEnumValues #tag EndViewProperty #tag ViewProperty Name="XON" Visible=true Group="Behavior" Type="Boolean" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="CTS" Visible=true Group="Behavior" Type="Boolean" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="DTR" Visible=true Group="Behavior" Type="Boolean" InheritedFrom="serial" #tag EndViewProperty #tag ViewProperty Name="byte01" Group="Behavior" InitialValue="&hFF" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte11" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte12" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte13" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte14" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte15" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte16" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte17" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte18" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte02" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte03" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte04" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte05" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte06" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte07" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte08" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte09" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="byte10" Group="Behavior" Type="String" EditorType="MultiLineEditor" #tag EndViewProperty #tag ViewProperty Name="carType" Group="Behavior" InitialValue="MR-03" Type="String" #tag EndViewProperty #tag ViewProperty Name="mode" Group="Behavior" InitialValue="read" Type="String" #tag EndViewProperty #tag EndViewBehavior End Class #tag EndClass