You are on page 1of 11

About Visual Basic

...................................................................................................................................
Visual Basic (VB) is an event driven programming language and associated
development environment from Microsoft for its COM programming model.[1] Visual
Basic was derived from BA!C and ena"les t#e rapid application development ($A%) of
grap#ical user interface (&'!) applications( access to data"ases using %AO( $%O( or
A%O( and creation of Active) controls and o"*ects. cripting languages suc# as VBA
and VBcript are s+ntacticall+ similar to Visual Basic( "ut perform differentl+.[,]
A programmer can put toget#er an application using t#e components provided wit#
Visual Basic itself. -rograms written in Visual Basic can also use t#e .indows A-!( "ut
doing so re/uires e0ternal function declarations.
Language features
Visual Basic was designed to "e eas+ to learn and use. 1#e language not onl+ allows
programmers to easil+ create simple &'! applications( "ut also #as t#e fle0i"ilit+ to
develop fairl+ comple0 applications as well. -rogramming in VB is a com"ination of
visuall+ arranging components or controls on a form( specif+ing attri"utes and actions of
t#ose components( and writing additional lines of code for more functionalit+. ince
default attri"utes and actions are defined for t#e components( a simple program can "e
created wit#out t#e programmer #aving to write man+ lines of code. -erformance
pro"lems were e0perienced "+ earlier versions( "ut wit# faster computers and native code
compilation t#is #as "ecome less of an issue.
Alt#oug# programs can "e compiled into native code e0ecuta"les from version 2
onwards( t#e+ still re/uire t#e presence of runtime li"raries of appro0imatel+ , MB in
si3e. 1#is runtime is included "+ default in .indows ,444 and later( "ut for earlier
versions of .indows it must "e distri"uted toget#er wit# t#e e0ecuta"le.
5orms are created using drag and drop tec#ni/ues. A tool is used to place controls (e.g.(
te0t "o0es( "uttons( etc.) on t#e form (window). Controls #ave attri"utes and event
#andlers associated wit# t#em. %efault values are provided w#en t#e control is created(
"ut ma+ "e c#anged "+ t#e programmer. Man+ attri"ute values can "e modified during
run time "ased on user actions or c#anges in t#e environment( providing a d+namic
application. 5or e0ample( code can "e inserted into t#e form resi3e event #andler to
reposition a control so t#at it remains centered on t#e form( e0pands to fill up t#e form(
etc. B+ inserting code into t#e event #andler for a 6e+press in a te0t "o0( t#e program can
automaticall+ translate t#e case of t#e te0t "eing entered( or even prevent certain
c#aracters from "eing inserted.
Visual Basic can create e0ecuta"les (7)7 files)( Active) controls( %88 files( "ut is
primaril+ used to develop .indows applications and to interface we" data"ase s+stems.
%ialog "o0es wit# less functionalit+ (e.g.( no ma0imi3e9minimi3e control) can "e used to
provide pop:up capa"ilities. Controls provide t#e "asic functionalit+ of t#e application(
w#ile programmers can insert additional logic wit#in t#e appropriate event #andlers. 5or
e0ample( a drop:down com"ination "o0 will automaticall+ displa+ its list and allow t#e
user to select an+ element. An event #andler is called w#en an item is selected( w#ic# can
t#en e0ecute additional code created "+ t#e programmer to perform some action "ased on
w#ic# element was selected( suc# as populating a related list.
VB Serial Communication
...................................................................................................................................
1#is c#apter discusses #ow Visual Basic can "e used to access serial communication
functions. .indows #ides muc# of t#e comple0it+ of serial communications and
automaticall+ puts an+ received c#aracters in a receive "uffer and c#aracters sent into a
transmission "uffer. 1#e receive "uffer can "e read "+ t#e program w#enever it #as time
and t#e transmit "uffer is emptied w#en it is free to send c#aracters.
Communications control
Visual Basic allows man+ additional components to "e added to t#e tool"o0. 1#e
Microsoft Comm component is used to add a serial communication facilit+.
!n order to use t#e Comms component t#e files MCOMM1;.OC) (for a 1;:"it module)
or MCOMM<,.OC) (for a <,:"it module) must "e present in t#e
=.!>%O.=?17M director+. 1#e class name is MComm. 1#e communications
control provides t#e following two wa+s for #andling communications@
Event-driven. 7vent:driven communications is t#e "est met#od of #andling serial
communication as it frees t#e computer to do ot#er t#ings. 1#e event can "e defined as
t#e reception of a c#aracter( a c#ange in C% (carrier detect) or a c#ange in $1 (re/uest
to send). 1#e OnComm event can "e used to capture t#ese events. and also to detect
communications errors.
Polling. Comm7vent properties can "e tested to determine if an event or an error #as
occurred. 5or e0ample( t#e program can loop waiting for a c#aracter to "e received. Once
it is t#e c#aracter is read from t#e receive "uffer. 1#is met#od is normall+ used w#en t#e
program #as time to poll t#e communications receiver or t#at a 6nown response is
imminent.
Visual Basic uses t#e standard .indows drivers for t#e serial communication ports (suc#
as serialui.dll and serial.v0d). 1#e communication control is added to t#e application for
eac# port. 1#e parameters (suc# as t#e "it rate( parit+( and so on) can "e c#anged "+
selecting Control -anel A +stem A %evice Manager A -orts (COM and 8-1) A -ort
ettings. 1#e settings of t#e communications port (t#e !$B and t#e port address) can "e
c#anged "+ selecting Control -anel A +stem A %evice Manager A -orts (COM and
8-1) A $esources for !$B and Addresses.
Properties of Communication Port Control
...................................................................................................................................
1#e Comm component is added to a form w#enever serial communications
are re/uired. B+ default( t#e first created o"*ect is named MComm1 (t#e second is
named MComm,( and so on). !t can "e seen t#at t#e main properties of t#e o"*ect are@
Comm-ort( %1$7na"le( 7O57na"le( Cands#a6ing( !nBufferi3e( !nde0( !nput8en(
!nputMode( 8eft( >ame( >ull%iscard( OutBufferi3e( -arit+$eplace( $1#res#old(
$17na"le( ettings( 1#res#old( 1ag and 1op.
Settings
1#e ettings propert+ sets and returns t#e $:,<, parameters( suc# as "aud rate(
parit+( t#e num"er of data "it( and t#e num"er of stop "its. !ts s+nta0 is@
[form.]MComm.ettings D settr[E]
w#ere t#e strtr is a string w#ic# contains t#e $:,<, settings. 1#is string ta6es t#e form@
FBBBB(-(%(F
w#ere
BBBBdefines t#e "aud rate(
- t#e parit+(
% t#e num"er of data "its( and
t#e num"er of stop "its.
1#e following lists t#e valid "aud rates (default is G;44Baud)@
114( <44( ;44( 1,44( ,H44( G;44( 1HH44( 1G,44( <IH44( 2;444( 1,I444( ,2;444.
1#e valid parit+ values are (default is >)@ 7 (7ven)( M (Mar6)( > (>one)( O (Odd)(
(pace).
1#e valid data "it values are (default is I)@ H( 2( ;( J or I.
1#e valid stop "it values are (default is 1). 1( 1.2 or ,.
An e0ample of setting a control port to HI44Baud( even parit+( J data "its and 1 stop "it
is@
Com1.ettings D FHI44(7(J(1F
CommPort
1#e Comm-ort propert+ sets and returns t#e communication port num"er. !ts s+nta0
is@
[form.]MComm.Comm-ort D port>um"er[K]
w#ic# defines t#e port>um"er from a value "etween 1 and GG. A value of ;I is returnedif
t#e port does not e0ist.
PortOpen
1#e -ortOpen propert+ sets and returns t#e state of t#e communications port. !ts
s+nta0 is@
[form.]MComm.-ortOpen D [L1rue M 5alseN]
A 1rue setting opens t#e port( w#ile a 5alse closes t#e port and clears t#e receive
andtransmit "uffers (t#is automaticall+ #appens w#en an application is closed).
1#e following e0ample opens communications port num"er 1 (COM1@) at HI44 Baud
wit# even parit+( J data "its and 1 stop "it@
Inputting data
1#e t#ree main properties used to read data from t#e receive "uffer are !nput( !nBuffer
Count and !nBufferi3e.
Input
1#e !nput propert+ returns and removes a string of c#aracters from t#e receive "uffer. !ts
s+nta0 is@
[form.]MComm.!nput
1o determine t#e num"er of c#aracters in t#e "uffer t#e !nBufferCount propert+ is tested
(to "e covered in t#e ne0t section). etting !nput8en to 4 causes t#e !nput propert+ to read
t#e entire contents of t#e receive "uffer.
InBufferSize
1#e !nBufferi3e propert+ sets and returns t#e ma0imum num"er of c#aracters t#at can
"e received in t#e receive "uffer ("+ default it is 14,H "+tes). !ts s+nta0 is@
[form.]MComm!nBufferi3e D [numB+tesK]
1#e si3e of t#e "uffer s#ould "e set so t#at it can store t#e ma0imum num"er of
c#aracters t#at will "e received "efore t#e application program can read t#em from t#e
"uffer.
1#e !nBufferCount propert+ returns t#e num"er of c#aracters in t#e receive "uffer. !t can
also "e used to clear t#e "uffer "+ setting t#e num"er of c#aracters to 4. !ts s+nta0 is@
[form.]MComm!nBufferCountD [countK]
Outputting data
1#e t#ree main properties used to write data to t#e transmit "uffer are Output(
OutBufferCount and OutBufferi3e.
1#e Output propert+ writes a string of c#aracters to t#e transmit "uffer. !ts s+nta0 is@
[form.]MComm. outputD [outtringE]
OutBufferSize
1#e OutBufferi3e propert+ sets and returns t#e num"er of c#aracters in t#e transmit
"uffer (default si3e is 21, c#aracters). !ts s+nta0 is@
[form.]MCommOutBuffer si3e D [>umB+tesK]
OutBufferCount
1#e OutBufferCount propert+ returns t#e num"er of c#aracters in t#e transmit "uffer.1#e
transmit "uffer can also "e cleared "+ setting it to 4. !ts s+nta0 is@
[form.]MCommOutBufferCount. D [4]
Basic Circuit iagram for PC to !icrocontroller Communication
"sing t#e Serial Port
I421 provides a transmit c#annel and a receive c#annel of serial communication. 1#e
transmit data pin (1)%) is specified at -<.1( and t#e receive data pin ($)%) is at -<.4.
1#e serial signals provided on t#ese pins are 118 signal levels and must "e "oosted and
inverted t#roug# a suita"le converter(MA),<,) to compl+ wit# $,<, standard.
All modes are controlled t#roug# CO>( t#e erial CO>trol register. 1#e CO> "its are
defined as M4( M1( M,( $7>( 1BI( $BI( 1!( $! from MB to 8B. 1#e timers are
controlled using 1MO%( t#e 1imer MO%e register( and 1CO>( t#e 1imer CO>trol
register.
Sending ata $rom %#e PC to a !icrocontroller
...................................................................................................................................
!n t#is article we are going to learn #ow to send data to a microcontroller and
ma6e t#e microcontroller respond to t#e data. 5or simplicit+ we are *ust going to send
data to turn on and off an 87% w#ic# is connected at port -,.4.
o lets start off wit# designing a communications protocol. 5rom VB we will send an
AC!! ,22( as a s+nc# "+te( and t#e on9off state (4 or 1). 1#e microcontroller will wait
for two (,) "+tes of data. After t#ese two "+tes of data are received( it will t#en eit#er
switc# on9off t#e 87%
Visual Basic at PC side
1o get started open Visual Basic.
• tart a new tandard 7)7.
• >e0t go to t#e -ro*ect M Components... menu
• C#ec6 t#e Microsoft Comm control ;.4.
• Clic6 OO.
>e0t dou"le:clic6 on t#e +ellow p#one in t#e tool"ar to add t#e MComm control to +our
form.
-rivate u" 5ormP8oad()
On 7rror $esume >e0t
Quse comm port 1
MComm1.Comm-ort D 1

Q G;44 "aud( no parit+( I data "its( 1 stop "it
MComm1.ettings D FG;44(>(I(1F
Q %isa"le %1$
MComm1.%1$7na"le D 5alse
Qopen t#e port
MComm1.-ortOpen D 1rue
7nd u"
>ow add two option "uttons from t#e tool "ar name as FoptonF and FoptoffF( c#ange t#e
caption to O> and O55 for "ot# "uttopns.
>ow add a command "utton named cmdsend from t#e tool "ar( and c#age t#e caption to
F7>%F
On %o %#e Code
>ow t#at t#e form is set up and read+ to go( we need to start adding our code to t#e
pro*ect. 1#e user will select a pin state from option "utton( and t#en clic6 cmdend to
send t#e data to t#e microcontroller. o first of all we need to set up t#e MComm
control( and select one of t#e pin states to start wit#. o lets add t#e following code to our
form...
>ow we *ust need to add t#e code to send t#e data. .#en t#e user presses cmdend( we
need to do t#ree t#ings.
• &et t#e pin state...
• end t#e data...
>ow lets put it all toget#er and send t#e data w#en we press t#e cmdend "utton...
-rivate u" cmdsendPClic6()
%im 87% As 8ong
Q &et 87% tate
!f opton.Value D 1rue 1#en
87% D 4
7lse
87% D 1
7nd !f
Q end Out %ata
MComm1.Output D C#rE(,22) R C#rE(87%)
7nd u"
o we sent out t#e s+nc# "+te (,22)(followed "+ t#e 87% state.
5inall+ we need to close t#e comm port w#en t#e VB pro*ect unloads so...
-rivate u" 5ormP'nload(Cancel As !nteger)
MComm1.-ortOpen D 5alse QClose t#e COMM port
7nd u"
1#atQs all at t#e -C side
!icrocontroller Side
-rogram t#e M!crocontroller wit# t#e following code
O$& 44C S $eset
MOV 1MO%(T,4C Sena"le timer1( mode , (auto reload)
MOV 1C1(T45%C SG;44 Baud rate
MOV CO>(T24C SI "it( 1 stop "it( $7> ena"led
71B 1$1
C7$7@U>B $!(C7$7 Swait for c#aracter to come in
MOV A(B'5 Sget data in A
CU>7 A(T455C(%O.> Sc#6 if t#e c#ar is s+nc# "+te ie.off# or ,22
Sif not *ump to clear $!
C8$ $! Sget read+ to get ne0t "+te
.A!1@U>B $!(.A!1 Swait for c#aracter to come in
MOV A(B'5 Sget data in A
CU>7 A(T44C(>)1 Sc#6 if t#e c#ar is 4
C8$ -,.4 Sswitc# on 87%
UM- %O.> S*ump to clear $!
>)1@CU>7 A(T41C(%O.> Sc#6 if t#e c#ar is 1
71B -,.4 Sswitc# off 87%
%O.>@C8$ $! Sget read+ to get ne0t "+te
UM- C7$7 S6eep getting t#e data
>ow run t#e VB pro*ect( select eit#er O> or O55( and press send. ?ou s#ould see t#e 87% turn O>
w#en +ou send a O> command( and turn O55 w#en +ou send a O55 command.
&eceiving ata $rom %#e !icrocontroller
...................................................................................................................................
!n t#e last article we sent data to a microcontroller and #ad it respond to t#e
data t#at we sent...
!n t#is article we are going to learn #ow to receive data from a microcontroller and ma6e
t#e -C respond. 5or simplicit+ we are going to get t#e -ort 4 and send to t#e -C for us to
receive using VB.
Visual Basic at PC side
1o get started open Visual Basic.
• tart a new tandard 7)7.
• >e0t go to t#e -ro*ect M Components... menu
• C#ec6 t#e Microsoft Comm control ;.4.
• Clic6 OO.
• >e0t dou"le:clic6 on t#e +ellow p#one in t#e tool"ar to add t#e MComm control
to +our form.
• 5inall+ add a la"el to +our form and name it l"ldata
-rivate u" 5ormP8oad()
Q 5ire $0 7vent 7ver+ single B+tes
MComm1.$1#res#old D 1
Q .#en !nputting %ata( !nput 1 B+te at a time
MComm1.!nput8en D 1
Q G;44 Baud( >o -arit+( I %ata Bits( 1 top Bit
MComm1.ettings D FG;44(>(I(1F
Q %isa"le %1$
MComm1.%1$7na"le D 5alse
Q Open COM1
MComm1.Comm-ort D 1
MComm1.-ortOpen D 1rue
7nd u"
On %o %#e VB Code
>ow t#at t#e form is set up and read+( we need to get a /uic6 understanding of #ow t#e
MComm control can receive data from t#e serial port. 1#ere are "asicall+ two met#ods(
polling t#e port and responding to communications events.
-olling t#e port is done "+ setting up a timer to c#ec6 t#e "uffer for data( and if data is
t#ere( process it. -olling is "etter w#en +ou #ave varia"le lengt# data t#at starts and ends
wit# #eader and footer "+tes respectivel+.
1#e event driven met#od is designed more for receiving fi0ed lengt# data. !t is also "etter
"ecause +ou donQt waist time polling t#e "uffer for data if none is t#ere. !nstead t#e
MComm control will tell +ou w#en data is t#ere "+ firing t#e OnComm() event. 1#is
event fires *ust li6e a Clic6() event would fire if +ou clic6ed on a Command Button( onl+
it is not t#e users action t#at fires t#is event( somet#ing must #appen at t#e serial port.
.#en t#e OnComm() event is fired +ou must c#ec6 t#e value of t#e Comm7vent
propert+ to see w#at e0actl+ #appened. 1#e Comm7vent propert+ will contain a different
value for eac# different 6ind of communication event t#at occurs.
!n t#is pro*ect we are onl+ concerned wit# t#e com7v$eceive constant w#ic# #as t#e
value of , and is fired w#en data is availa"le in t#e "uffer.
>ow t#at we #ave a feel for #ow t#e MComm control will assist us in receiving data(
lets first set up t#e MComm control. Cop+ t#is commented code to +our pro*ect...
?ou ma+ notice t#at t#ere are two new properties in t#e a"ove code t#at #ave +et to "e
e0plained in t#ese articles. 1#e first is $1#res#old. !n t#is e0ample( setting t#e
$1#res#old propert+ e/ual to 1 tells t#e MComm control to fire t#e com7v$eceive
event w#en t#ere are at least one "+tes availa"le in t#e "uffer. 1#e second propert+(
!nput8en( tells t#e MComm control to onl+ give us t#e first "+te w#en we re/uest data
from t#e "uffer.
.it# t#at out of t#e wa+( lets ta6e a loo6 at t#e code t#at is e0ecuted w#en t#e
OnComm() event is fired. 1#e comments s#ould #elp +ou along...
-rivate u" MComm1POnComm()
%im %ata As tring
Q !f com7v$eceive 7vent t#en get data and displa+
!f MComm1.Comm7vent D com7v$eceive 1#en
%ata D MComm1.!nput Qget data
l"ldata.caption D Asc(%ata1) Qconvert to AC!! and displa+
7nd !f
7nd u"
5inall+ we need to close t#e comm port w#en t#e VB pro*ect unloads so...
-rivate u" 5ormP'nload(Cancel As !nteger)
MComm1.-ortOpen D 5alse QClose t#e COMM port
7nd u"
1#atQs all at t#e -C side
!icrocontroller Side
-rogram t#e Microcontroller wit# t#e following code( it reads t#e port -4 and sent t#e
data to t#e -C.
O$& 44C S $eset
MOV 1MO%(T,4C Sena"le timer1( mode , (auto reload)
MOV 1C1(T45%C SG;44 Baud rate
MOV CO>(T24C SI "it( 1 stop "it( $7> ena"led
71B 1$1
MOV -4(T455C Sma6e -4 as input port
A&A!>@MOV A(-4 S$ead -4
MOV B'5(A Ssend data
C7$7@U>B 1!(C7$7 Swait for data to "e transferred
C8$ 1! Sclear 1! for ne0t c#ar
UM- A&A!> S6eep doing it
After Burning up t#e a"ove -rogram to t#e microcontroller run +our VB pro*ect( power
up t#e microcontroller( c#ange t#e values of t#e switc#es. and t#e datas will "e displa+ed
on t#e -C.