Sunday, 20 November 2011

Controller Area Network - Development Process


As the application is designed to be as simple as possible, the first thing to start with is a simple code which uses buttons for connecting to the board (“Connect”) and disconnecting from it (“Disconnect”).

For displaying the information received from the board, a RichEdit is placed into the application. The source code should have functions for opening ports, closing them, reading from and writing to the buffer as well as error handling.

Code Snippet
//-----Code Snippet from the TCommPort Constructor-----//
: m_CommOpen(false),
m_dcb.DCBlength = sizeof(DCB);
m_dcb.BaudRate =1382400;
m_dcb.ByteSize =8;
m_dcb.Parity =NOPARITY; //NOPARITY and friends are #defined in windows.h
m_dcb.StopBits =ONESTOPBIT; //ONESTOPBIT is also from windows.h
//----end of TCommPort constructor-----//
The first step of implementing the communication library into the application is to assign the “OpenCommPort()” function to the “Connect” Button.

// Open the COM PORT


“com” is the name of the class used to call functions from the library. This is enabled by the following code snippet at the top of the file:

TCommPort *com = new TCommPort;

The “OpenCommPort()” function first checks if the port is already open and if so throws an exception and then proceeds to handle the COM port treating it to be a file. It then writes the relevant configurations to the port like BaudRate, ByteSize and Parity. Besides the port open function, the Connect button also disables all other buttons while enebling the Disconnect Button. A safety feature to cut down on the amount of error messages the user may face.

The final application consists of seven buttons namely: Start, Stop, Exit, Connect, Disconnect, Run, Save along with two RichEdit Boxes namely: Display and Message.

A screenshot of the running application

At the start of the application, all the buttons, timers and flags are initialized to a default status.

The code snippet below provides the information:

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
// Intializing the Buttons
Start->Enabled = true;
Stop->Enabled = false;
Exit->Enabled = true;
Connect->Enabled = false;
Disconnect->Enabled = false;
Run->Enabled = false;
Save->Enabled = false;
// Initializing the flags
open_port = false;
filesaved = false;
// Initializing the Timer
Timer1->Enabled = false;

Main features of the Buttons:

Start: Initially only the Start and the Exit buttons are enabled. With the click of the Start button the system becomes active enabling the Connect, Stop and Exit buttons and displaying an information regarding the status of the system through the RichEdit Box named “Message”.

Connect: The Connect button press triggers the application to display a helpful message to the user through RichEdit Box “Message” letting them know that the port has been opened. It disables the Start button while enabling the Run, Disconnect, Stop, Exit buttons also updating various flag status mainly the open_port flag.

Run: The Run button press updates various button status. The major feature being triggering the Timer added to the application from the C++ Builder. This is done using the following code snippet:

Timer1->Enabled = true;

The enabled timer updates the RichEdit Box “Display” with the data from the CAN-Bus every millisecond, clears the buffer and then repeats itself until an external agent deactivates (Disconnect/Stop buttons) the process.

Disconnect: The Disconnect button press enables the Save, Connect, Stop and Exit buttons while keeping Run, Start and Disconnect buttons deactivated. This also disables the Timer and other flags and double checks with the open_port status and another Boolean function from the COM Library “ifConnected()”. When satisfied a helpful message is displayed through the RichEdit Box “Message” to inform the user about the connection status.

Save: The Save button opens the screen which allows options to save the information in the RichEdit Boxes to be written into .txt or .doc files for further analysis. The following code snippet is used to bring screen:


Screenshot (a) from the running application showing the options to save to files

Screenshot (b) from the running application showing the menu to save to a logfile

The Exit button shown on screenshot (a) closes the screen and goes back to the main screen.

Stop: The Stop button press triggers an application which deals with bringing the system to its default state, i.e., in a disconnected state and all buttons in disabled mode except the Start and Exit buttons.

The button press initially checks if the network is still live. If no, then it shuts down any action going on and brings the system to default state. If yes, pops up a warning message informing the user about the situation and providing options to override the system to disconnect from the network and sit at the default state or cancel the current operation and manually disconnect from the network, clear both the RichEdit boxes and passes appropriate status information for the user through RichEdit Box “Message” that the system is at halt.

The code snippet below shows the generation of the warning pop up:

// Message Box for options
int response = Application->MessageBoxA("The PORT is still connected. Are you sure you wish to STOP\n","Warning", MB_YESNOCANCEL|MB_ICONQUESTION);

Screenshot from a running application showing warning pop up.

Exit: The Exit button press, as the name indicates terminates the application.
The two RichEdit Boxes “Message” and “Display” have the same major functionality but varies in their usage. The Message Box is used to display system update information messages for the user while the Display Box displays the data streamed from the network.

1 comment: