Visual Studio Setup
Downloading Relevant Software
The Screen Simulation was developed on Microsoft Visual Studio – Community. The Microsoft Visual Studio IDE is free and must be downloaded to run the Screen Simulation. The Visual Studio IDE uses C# programming language, can be used to develop computer programs, mobile apps, and is compatible with the Commander. The availability of online material makes Visual Studio very easy to use and perfect for this prototyping project. Instructions for most Visual Studio features and tasks are available online. The Screen Simulation was developed using the Visual Studio IDE. A free download of Visual Studio – Community is available online.
When installing Visual Studio, you will get the opportunity to choose which tool packages to download. In order to follow this “How-To” tutorial, it is important to check the four boxes below. Doing so will give you access to the tools used in this tutorial.
Running the Screen Simulation
After opening the program, you can run the program by pressing the “START” button at the top of Visual Studio. You can also run the program using the .exe file. Using the .exe file may allow the Screen Simulation to be run on computers without Visual Studio software installed.
Figure 1 - Pressing the “START” button, circled in red, will launch the program.
Expected Errors when Running Screen Simulation
Occasionally, you will get an error message when running the Screen Simulation. The error is a result of the Arduino and Screen Simulation being out of sync during the sending/receiving data exchange. Ignore the error and re-compile the program.
Microcontroller & Visual Studio Communication
Using a microcontroller to communicate with the vehicle’s CAN-Bus port is easy. The microcontroller comes with a library of functions which make extracting data, as well as exporting data, from the vehicle. The Arduino and Visual Studio Program can communicate through the computer’s Serial Port. To demo this, two programs were written.
The first program was written using the Arduino IDE. This program prints three values to the Serial Monitor: X, Y and Z. Where X is a specified value, Y is two times the value of X, and Z is three times the value of X. The code for this program, as well as the values sent to the Serial Monitor (reference Figures 21 & 22).
Figure 21 - Code written for the program described above.
Figure 22 - Serial Monitor output for the program described above.
The second program was written using the Visual Studio IDE. This program reads three values from the Serial Monitor. When setting up the program, a Serial Port and Timer were added to the Form. This allows the program to retrieve data from the Serial Port on a time interval determined by the Timer. It is critical that the COM-Port selected in the Serial Port’s properties is the same as the COM-Port that the Arduino is running on.
Figure 23 - Code
Figure 24 - The Form contains a Serial Port and Timer. Both of these can be found inside of the Toolbox menu.
We verify that the Arduino is sending data to Visual Studio with this program. While the Arduino is running its program, it will print values to the Serial Monitor. If we run the Visual Studio program, we see that it is retrieving the values being sent from the Arduino via the Serial Monitor.
Figure 25 - Visual Studios program retrieves data from the Arduino via Serial Monitor. The program then presents the data onto the form / screen.
This shows that we can send data from the Arduino to the Screen Simulation. The Screen Simulation seems to be receiving the data slowly. The computer Arduino is writing to the Serial Monitor and the Computer / Screen Simulation is reading it from Serial Monitor.
The necessary Arduino code can be found via the following code was used to have vehicle diagnostic data to correctly display on the Screen Simulation:
#include <Canbus.h>
char UserInput;
int data;
char buffer[456]; //Data will be temporarily stored to this buffer before being written to the file
void setup() {
Serial.begin(9600);
}
void loop() {
// 1) Speed
Serial.print(S);
Canbus.ecu_req(VEHICLE_SPEED, buffer);
Serial.println(buffer);
delay(100);
// 2) RPM
Serial.print(R);
Canbus.ecu_req(ENGINE_RPM, buffer);
Serial.println(buffer);
delay(100);
// 3) Throttle
Serial.print(T);
Canbus.ecu_req(THROTTLE, buffer);
Serial.println(buffer);
delay(100);
// 4) Coolant Temperature
Serial.print(C);
Canbus.ecu_req(ENGINE_COOLANT_TEMP, buffer);
Serial.println(buffer);
delay(100);
// 5) O2 Voltage
Serial.print(O);
Canbus.ecu_req(O2_VOLTAGE, buffer);
Serial.println(buffer);
delay(100);
// 6) MAF Sensor
Serial.print(M);
Canbus.ecu_req(MAF_SENSOR, buffer);
Serial.println(buffer);
delay(100);
}
This Arduino code file can be found using the following path:
C:\Users\rpierso3\Documents\Arduino\CAN_Diagnostics
Figure 26 - Vehicle diagnostics correctly displays on Screen Simulation.
Using a Microcontroller to Acquire Vehicle Data
To enable
Microcontroller-to-Vehicle communication, a CAN-Bus Shield must be attached to
the microcontroller.
Figure 8 -
Microcontroller (left) and CAN-Bus Shield (right).
Figure 9 -
CAN-Bus Shield attached to the Microcontroller.
Figure 10 -
A cable is used to connect the CAN-Bus Shield to the vehicle.
The CAN-Bus
Shield comes with a library to make writing code and acquiring vehicle
diagnostic data easy. This library is free and must be downloaded[1] off the internet. Use the following link: https://github.com/sparkfun/SparkFun_CAN-Bus_Arduino_Library.
Picture 1 – Press “Clone or Download” to
download the Library’s zip file.
When using the CAN-Bus Library for the
first time, you will need to add it to the Arduino IDE using the following path:
Sketch → Include Library → Add . ZIP
Library
Picture 2 – Adding a Library to the
Arduino IDE & Program.
This process of adding the .ZIP CAN-Bus
Library will automatically store it in the following location:
C:\Users\rpierso3\Documents\Arduino\libraries. This gives the Arduino IDE
access to the libarary. To give an Arduino program access to the library,
you must add the following line of code: #include <Canbus.h>. Code
for acquiring vehicle diagnostic data has already been written for this
Prototyping Platform (see Section 8 Technical Details).
Sending Data from Microcontroller to Visual Studio
The Screen Simulation communicates with
the Microcontroller via Serial Monitor. If a form on the Screen
Simulation requires data from the Microcontroller, for example on the
Diagnostics.vb form, then it will have a “Serial Port” tool.
Picture
3 – The Diagnostics form has a Serial Port tool, circled in red.
When the Arduino is connected to the computer, it does through a certain COM-Port. This COM-Port can be identified within the Arduino IDE (see Section 6.2 Uploading Code to Microcontroller). The “Port Name” in the Properties menu must be changed to whichever COM-Port the Arduino is connected to (reference Figure 11). For modifying code, see Section 8 Technical Details.
Figure 11 - Input the correct COM-Port to enable Arduino-to-Visual Studio communication.
Modifying the Screen Simulation
The Screen Simulation is made up of multiple screens. In
Visual Studio, these screens are called “forms”. When the Screen Simulation file is open
in Visual Studio, its forms can be accessed using the Solution Explorer tools
(reference Figure 12).
Figure 12 - Double clicking forms in the Solution Explorer, circled above, will open forms for editing.
Double-click on forms within the Solution Explorer to open it. Once a form is open, it is displayed and is ready for editing.
Figure 13 – “Are You Sure” form is opened after double clicking in the Solution Explorer.
Objects on the form can be moved, deleted and size-adjusted. To make other changes to the form, click individual items that you want to change. Then use the “Properties” menu to change things such as color, text, font, borders and alignment (reference Figure 14).
Figure 14 - Items on the form can be dragged or deleted. To make other changes, use the “Properties” menu, circled above.
New features can be added to the form by using the “Toolbox” menu. Buttons, labels, etc. can be dragged from the Toolbox onto the form (reference Figure 15).
Figure 15 - The “Toolbox” menu gives you the ability to add new features to the form.
Adding New Screens
To add a new screen to the Screen Simulation program, use the top/main menu and the following path in Visual Studio:
Project → Add Windows Form
When adding a new Form, use the “Windows Form” format option (reference Figure 16).
Figure 16 - When creating a new form, use the Windows Form format option.
Please reference “Editing Screens” (Section 7.1.) for instructions on formatting and editing screens.
Button Forwarding
When a form is open, you can edit objects by double-clicking them. This enables button functionality and will allow them to forward you to different screens.
Double-clicking a button on the form will take you to the code behind the form. You can edit the code to give the button a function.
The most common features being used in this code for the buttons are:
NameOfForm.Show() – Opens the specified form when the button is pressed.
Me.Close() – Closes the current form when the button is pressed.
Figure 17 - The code above is an example of how to open/close screens when buttons are pressed.
Enabling Keyboard & Commander Inputs
The Commander control has a “Back”, “Entertainment”, “Home”, “Navigation”, and “Favorites” button.
One notable feature about the buttons is the ability to make them keyboard-activated. In other words, pressing a specific key on the keyboard will effectively press the button. To do this, select the button on the form which you would like to edit. Then reference the “Text” property in the Properties menu. By adding an “&” before a letter, you make that letter key cause the button to be pressed. One example is having the Text property read “&Back”, this would cause the button to be pressed when it is clicked or when the “B” key is pressed. A second example is “E&xit”, this would cause the button to be pressed when it is clicked or when the “X” key is pressed.
Pressing the Entertainment, Home, Navigation, and Favorites buttons on the Commander will send keyboard inputs to the computer. For the Commander to work correctly with the Screen Simulation, entertainment / home / navigation / favorites buttons must be created on every form.
For example, we will use the “Are You Sure” form:
When this form is open on the Screen Simulation program, all buttons on the Commander should work. More thoroughly, when the “Are You Sure” screen appears, the user should be able to press the Entertainment, Home, Navigation, and Favorites buttons and the program should appropriately respond.
That being said, buttons should be created on the form to mirror the Commander.
Having buttons which mirror that of the Commander, create clutter on the form. To improve the appearance, we can hide buttons:
You can hide the button if you right click the button and click “Send to Back”.
This process was repeated for each of the Entertainment, Home, Navigation and Favorites buttons.
Visual Studio Serial Port Setup
To
establish communication between a form/screen and the microcontroller, select
its SerialPort tool (as shown in Figure 27). Look at the Serial Port
Properties and make sure that he “Port Name” is the same COM-Port that the
microcontroller is connected to (for more details see Section 6).
Figure 28 – Selecting the correct COM-Port
to enable communication.
Visual Studio Timer Setup
To use the Timer tool, you must adjust its
properties. Select the timer tool, change the “Enabled” property to
“True”, and change the “Interval” property to “100”. This will cause the
timer to run automatically while using the program and will create timer
intervals of 100 milliseconds.
Figure
28 – Set Timer tool properties.
Code:
Visual Studio Code - Code for Help Requested Screen:
Public Class Help_Requested_Screen
Dim SerialString As String
Private Sub Help_Requested_Screen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Maintenance_Required_Screen.Close()
'SerialPort1.Open()
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Roadside_Assistance_Screen.Show()
'Maintenance_Required_Screen.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select_Service_Shop_Screen.Show()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Are_You_Sure.Show()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs)
Me.Close()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Home_Screen.Show()
Me.Close()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs)
Navigation_Unavailable.Show()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Home_Screen.Show()
Me.Close()
End Sub
Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
Favorites_Unavailable.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Entertainment_Unavailable.Show()
End Sub
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
Navigation_Unavailable.Show()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Diagnostics.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'SerialString = SerialPort1.ReadLine()
'If SerialString <> “” Then
' If Mid(SerialString, 1, 1) = “M” Then
' Maintenance_Required_Screen.Show()
' Me.Close()
' End If
'End If
'Label2.Text = Date.Now.ToString("hh:mm")
Label2.Text = Date.Now.ToShortTimeString
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs)
Home_Screen.Show()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs)
End Sub
End Class
Code that has green text is commented and will not be run (for example: 'SerialPort1.Open()). Certain code must be un-commented for the Maintenance Alert feature to work.
The code from above was edited by uncommenting certain sections. The following code will work for enabling the maintenance required alert:
Public Class Help_Requested_Screen
Dim SerialString As String
Private Sub Help_Requested_Screen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Maintenance_Required_Screen.Close()
SerialPort1.Open()
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Roadside_Assistance_Screen.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select_Service_Shop_Screen.Show()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Are_You_Sure.Show()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs)
Me.Close()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Home_Screen.Show()
Me.Close()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs)
Navigation_Unavailable.Show()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Home_Screen.Show()
Me.Close()
End Sub
Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
Favorites_Unavailable.Show()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
Entertainment_Unavailable.Show()
End Sub
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
Navigation_Unavailable.Show()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Diagnostics.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
SerialString = SerialPort1.ReadLine()
If SerialString <> “” Then
If Mid(SerialString, 1, 1) = “M” Then
Maintenance_Required_Screen.Show()
Me.Close()
End If
End If
Label2.Text = Date.Now.ToString("hh:mm")
Label2.Text = Date.Now.ToShortTimeString
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs)
Home_Screen.Show()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs)
End Sub
End Class
Making changes to these commented sections will allow the Maintenance Alert feature to work. When the “Help Requested” screen is displayed, this code will constantly check the Serial Port to see if maintenance is required. The SerialPort1.Open() line of code lets the program know to start receiving messages from the microcontroller.
The code below is a section from the code provided above. Since the code is for the Timer tool, this section of code is run every interval, or 100 milliseconds. This code reads data from the serial monitor and if an “M” is received, then maintenance is required and the program opens the “Maintenance Required” screen.
Breakdown of Code Sub-Section:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
SerialString = SerialPort1.ReadLine()
If SerialString <> “” Then
If Mid(SerialString, 1, 1) = “M” Then
Maintenance_Required_Screen.Show()
Me.Close()
End If
End If
Label2.Text = Date.Now.ToString("hh:mm")
Label2.Text = Date.Now.ToShortTimeString
End Sub
Microcontroller Code
For the Screen Simulation to display maintenance alerts, the microcontroller must constantly check if the engine light is on and send that information to Visual Studio. The bolded section of the code, below, is where the microcontroller checks the CAN-Bus to know if the engine maintenance light is on.
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
unsigned char len = 0;
unsigned char buf[8];
char str[20];
int ticker = 0;
void setup() {
Serial.begin(9600);
Serial.println("CAN Write - Testing transmission of CAN Bus messages");
delay(1000);
if (Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok");
else
Serial.println("Can't init CAN");
delay(1000);
}
void loop() {
tCAN message;
//Read CAN-Bus Messages
if (mcp2515_check_message()) {
if (mcp2515_get_message(&message)) {
if (message.id == 0x4F2) { // Check for specific in HEX form, ID 4F2
if (message.data[5] == 32) { // Check the 5th object in ID 4F2, if it is equal to 32, then the Engine Light is on
Serial.println("M"); // If Engine Light is on, then send an “M” for “Maintenance Required” to Serial Monitor
} else {
Serial.println("N"); // If Engine Light is not on, then send an “N” for “No Maintenance Required” to Serial Monitor
}
}
}
}
delay(10); // Check signal every 100 miliseconds
}
No comments:
Post a Comment