Pages

Friday 3 July 2015

INTEGRATED EMBEDDED SYSTEM WITH Vb.Net: Chapter-1

OVERVIEW:
               VB.Net is a simple, modern, object-oriented computer programming language developed by Microsoft to combine the power of .NET Framework and the common language runtime with the productivity benefits that are the hallmark of Visual Basic.
              Visual Basic .NET (VB.NET) is an object-oriented computer programming language implemented on the .NET Framework. Although it is an evolution of classic Visual Basic language, it is not backwards-compatible with VB6, and any code written in the old version does not compile under VB.NET.
The following reasons make VB.NET a widely used professional language:
  •          Modern, general-purpose programming language.
  •          Object oriented.
  •          Component oriented.
  •          Easy to learn.
  •          Structured language.
  •          It produces efficient programs.
  •          It can be compiled on a variety of computer platforms.
  •          Part of .Net Framework.

 GETTING STARTED:

Creating a new  VB.NET project:
Once Visual Studio is running the first step is to create a new project. Do this by selecting New Project from the File menu. This will cause the New Project window to appear containing a range of different types of project. For the purposes of this tutorial we will be developing a Windows Forms Application so make sure that this option is selected.

CREATING NEW PROJECT:

·        The first thing you do when you want to create a new application is to create a NEW PROJECT.

This can be done from start page.



New project created from the NEW PROJECT window:





Then the “NEW PROJECT” window will appear.
In this window you will select an appropriate template based on what kind of application you want to create, and a name and location for your project and solution.
The most common applications are:

  • ·         Windows form application.
  • ·         Console application.
  • ·         WPF application
  • ·         ASP.NET web application.
  • ·         Silverlight application.

Select WINDOWS FORMS APPLICATION.




TOOLBOX


When you select WINDOWS FORM APPLICATION, you will get FORM DESIGN WINDOW,it is used to design USER interface by making use of TOOLBOX on the left side of window,
The TOOLBOX contains all the necessary controls, etc. You need to create a user interface by making use of these controls as shown in figure below.
In order to use these controls, just drag and drop it on to your Design forms, as shown in figure.

Figure shows TOOLBOX and DESIGN FORM:



The following screenshot shows, making use of these toolbox controls for designing the user interface on DESIGN FORM.




PROPERTIES WINDOW               

Each TOOLBOX we have used on our form has many properties that we can set. This is done by using Properties window. We can find the property window on the right bottom side of your project 


BUILD AND DEBUGGING TOOL

               
                The visual studio has lots of Build and Debugging Tools,

BUILD MENU:

Below we see the Build menu. The most used Build tool is BUILD SOLUTIONS.







DEBUG MENU:


                In order to RUN or DEBUG your windows form we make use of DEBUG TOOLs. The most used debug tool is START DEBUGGING. it can be find the shortcut for this on the top of your visual studio windows.                                                                                                                                                                                                           


Windows programming
                When creating ordinary windows form application, we can select between the following:
  •         Windows form Application
  •         WPF application

HELLO WORLD

                We start by creating traditional “HELLO WORLD” application using Windows Form Application is shown below. The visual studio UI shown below.


In this application we make use of simple textbox and Button(Button name is changed to Submit in the properties) when we click on submit the “HELLO WORLD ”massage will be displayed in the Textbox.
The OUTPUT  of this form as shown below:
               
The code is as follow:
Imports System.Text

Imports System

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = "HELLO WORLD"
    End Sub

End Class

DATA TYPES AND VARIABLES

                “Variables” are simply storage locations for data. You can place data into them and retrieve their contents as part of a VB.NET expression. The interpretation of the data in a variable is controlled through “Types”.

The VB.NET simple types consist of:

  •          Boolean type
  •          Numeric types: Integrals, Decimal
  •          String type

BOOLEAN TYPES

Boolean types are declared using the keyword “Boolean”. They have two values: “true” or “false”. In other languages, such as C and C++, boolean conditions can be satisfied where 0 means false and anything else means true. However, in VB.NET the only values that satisfy a boolean condition is true and false, which are official keywords.
Example:

Dim  Content as Boolean
Content=True 

Numeric types: Integrals, Floating Point, Decimal


Example:
Dim I As Integer
I=35
Dim x As Decimal
X=10.5
Dim d As Double
d=10000

String type

Example:

Dim abc As String

abc=”Hai..”


Special characters that may be used in strings:

Arrays


Example:

Dim A(100) As Integer
A(0)=40
A(1)=10
A(2)=20
A(3)=30 

Control Flow


To be able to control the flow in your program is important in every programming language.

The two most important techniques are:

  •          The if Statement
  •          The switch Statement

The if Statement


The if statement is probably the most used mechanism to control the flow in your application. An if statement allows you to take different paths of logic, depending on a given condition. When the condition evaluates to a Boolean true, a block of code for that true condition will execute. You have the option of a single if statement, multiple else if statements, and an optional else statement.
Example:

myTest=false

if myTest=false Then
MsgBox ("Hello”)
End If


OUTPUT:
 

For more complex logic we use the if … else statement.

Example:

Dim myTest As Boolean

myTest=true

if myTest=false Then


MsgBox ("Hello1")


else

MsgBox ("Hello2")


End If


Or you can use nested if… else if sentences.

Example:

Dim myTest As Integer

myTest=2

if myTest =1 Then


MsgBox ("Hello1")



elseif myTest = 2 Then


MsgBox ("Hello2")


else


MsgBox ("Hello3")
End If


The  Select case statement


Another form of selection statement is the select case statement, which executes a set of logic depending on the value of a given parameter. The types of the values a select statement operates on can be booleans, enums, integral types, and strings.
Example:

Dim number As Integer = 8
Select Case number
    Case 1 To 5
        MsgBox("Between 1 and 5, inclusive")
        ' The following is the only Case clause that evaluates to True. 
    Case 6, 7, 8
        MsgBox ("Between 6 and 8, inclusive")
    Case 9 To 10
        MsgBox ("Equal to 9 or 10")
    Case Else
        MsgBox ("Not between 1 and 10, inclusive")
End Select

Loops


In VB.NET we have different kind of loops:
  •          The while loop
  •         The do loop
  •         The for loop
  •         The foreach loop

The while Loop


A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a boolean value of true.

Example:

Dim myInt As Integer = 0

While myInt < 10


     MsgBox("Inside Loop: " & myInt.ToString())

     myInt += 1
End While


MsgBox("Outside Loop: " & myInt.ToString())

Output



The do Loop


A do loop is similar to the while loop, except that it checks its condition at the end of the loop. This means that the do loop is guaranteed to execute at least one time. On the other hand, a while loop evaluates its boolean expression at the beginning and there is generally no guarantee that the statements inside the loop will be executed, unless you program the code to explicitly do so.
Example:
Dim myInt As Integer = 0
Do
MsgBox("Inside Loop: " & myInt.ToString())
myInt += 1
Loop While myInt < 10
MsgBox("Outside Loop: " & myInt.ToString())





The for Loop 
A for loop works like a while loop, except that the syntax of the for loop includes initialization and condition modification. for loops are appropriate when you know exactly how many times you want to perform the statements within the loop.

Example:

For i As Integer = 0 To 9
MsgBox("Inside Loop: " & myInt.ToString())
myInt += 1
Next
MsgBox("Outside Loop: " & myInt.ToString())

OUTPUT:


 


The for each Loop


A foreach loop is used to iterate through the items in a list. It operates on arrays or collections.

Example:

Dim names As String() = {"Elvis", "Beatles", "Eagles", "Rolling Stones"}
For Each person As String In names
     MsgBox(person)
Next

SERIAL COMMUNICATION


In telecommunication and computer science, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus. This is in contrast to parallel communication, where several bits are sent as a whole, on a link with several parallel channels.

Setting Up


Imports System.Data.SqlClient
Imports System.Net.Sockets
Imports System.Text






Shown in the above form before communicating with the particular hardware device we should add SerialPort tool from the Toolbox.




This is  standard Windows Forms Application via File menu. To this add the button (name Ports) and a Rich Text Box.The button is called btnGetSerialPorts and the Rich Text called as rtbIncomingData (the name will become apparent later).The rich text box is used as it is more flexible than the ordinary text box. Its uses for sorting and aligning text are considerably more than the straight textbox.





This shows all the devices that appear as com ports, a mistake to make is thinking that a device if plugged into the USB will appear as a COM Port.

The baud rate is the amount of possible events that can happen in a second. It is displays usually as a number of bit per second, the possible number that can be used are 300, 600, 1200, 2400, 9600, 14400, 19200, 38400, 57600, and 115200 (these come from the UAR 8250 chip is used, if a 16650 the additional rates of 230400, 460800 and 921600) .


The next box is the number of Data bits, these represent the total number of transitions of the data transmission (or Tx line) 8 is the standard ( 8 is useful for reading certain embedded application as it gives two nibbles (4 bit sequences).
The Handshaking property is used when a full set of connections are used (such as the grey 9 way D-types that litter my desk). It was used originally to ensure both ends lined up with each other and the data was sent and received properly. A common handshake was required between both sender and receiver. Below is the code for the combo box:
                Here is the complete code for serial communication between transmitter and receiver.
Imports System.Data.SqlClient
Imports System.Net.Sockets
Imports System.Text



Public  class Form1  
    {

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label11.Text = "Hi" + " " + Form2.TextBox1.Text + " , " + "Welcome you to PSA"
        user = Form2.TextBox1.Text


        total = 0
        k = 0
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If
        Try
            With SerialPort1
                .PortName = "COM5" Initilizing Components…Using code
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Handshake = IO.Ports.Handshake.None
            End With
            SerialPort1.Open()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub

Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
     
InBuff = SerialPort1.ReadExisting() ‘ Receiving Value From the Serial Port
TextBox1.Text=InBuff

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click       
Seialport.WriteLine("Hello World!")
     End Sub

End Class

OUTPUT: