e00a052.pdf
(
284 KB
)
Pobierz
000074-2-UK Basiskurs-2
BEGINNERS
COURSE
PC Peripheral Design
Part 2: Software
By B. Kainka
In this second part we
concentrate on software.
We take a closer look at the
IOtest program presented in
the first article and use simple program examples
in Visual Basic to control a model traffic light and a clock generator.
In the first article in this series we introduced
the IOtest program. The majority of readers
with some programming experience will be
interested in how we access the serial inter-
face using Visual Basic. Here we will use a
library of program subroutines called
PORT.DLL written by H.-J.Berndt. This DLL
file is available from the Free Downloads sec-
tion of the
Elektor Electronics
website at
http://www.elektor-electronics.co.uk. For
those with no access to the Internet, the file
is also found on the diskette containing the
course software (see Readers Services
pages).
In Visual Basic, all the procedures and
functions defined in the DLL are declared and
must be in an external module. In our case
this is called PORTS.BAS (
Listing 1
).
The experienced (Visual Basic) user will
probably recognise the most commonly used
routines for serial communications, e.g.,
OPENCOM which is used to open the inter-
face for communication and indicates that it
is available to the program. SENDBYTE and
READBYTE are used to transfer data over the
serial interface. For our purposes here, the
more important routines are those
that access the control and status
lines of the interface. The procedures
DTR, RTS and TXD control these out-
put signals and CTS, DSR, RI and
DCD read the state of these input
signals. One input line that you can-
not read directly is RXD. This is
because it would normally be used
to carry the received serial data.
Also the PORT.DLL file contains rou-
tines for time measurement that we
will be using later, as well as many
other functions for the other PC inter-
faces (Parallel port, Joystick, Sound
and Video).
DTR
RTS
TXD
I/Otest
GND
The application program I/Otest is
shown in
Listing 2
.
The essential core of the program is
the timer procedure
Timer1
_
Timer
, which is automati-
cally called at predetermined inter-
vals. The check boxes 1 to 4 are acti-
000074 - 2 - 11
Figure 1. The traffic light LEDs.
52
Elektor Electronics
10/2000
(2)
BEGINNERS
COURSE
Listing 1
Declarations for PORT.DLL
Declare Function OPENCOM Lib “Port” (ByVal A$) As Integer
Declare Sub CLOSECOM Lib “Port” ()
Declare Sub SENDBYTE Lib “Port” (ByVal b%)
Declare Function READBYTE Lib “Port” () As Integer
Declare Sub DTR Lib “Port” (ByVal b%)
Declare Sub RTS Lib “Port” (ByVal b%)
Declare Sub TXD Lib “Port” (ByVal b%)
Declare Function CTS Lib “Port” () As Integer
Declare Function DSR Lib “Port” () As Integer
Declare Function RI Lib “Port” () As Integer
Declare Function DCD Lib “Port” () As Integer
Declare Sub DELAY Lib “Port” (ByVal b%)
Declare Sub TIMEINIT Lib “Port” ()
Declare Sub TIMEINITUS Lib “Port” ()
Declare Function TIMEREAD Lib “Port” () As Long
Declare Function TIMEREADUS Lib “Port” () As Long
Declare Sub DELAYUS Lib “Port” (ByVal l As Long)
Declare Sub REALTIME Lib “Port” (ByVal i As Boolean)
vated and will show a tick when the corre-
sponding input line is switched to a ‘1’. The
three output lines are switched when the
user activates the corresponding check box.
The other parts of the program are used
to select and open the interface and we will
look at them a little later. We can see that the
interface will be initialised with a communi-
cation rate of 1200 baud, no parity bit, 8 data
bits and 1 stop bit. For our purposes here,
these communication parameters are entirely
superfluous because we are only interested
in the control lines that form part of the inter-
face. Windows will however configure the
port as if it were to communicate with, for
example, a modem. But all we need to do
here is to read the input status lines and
write to the output control lines.
Traffic Light controller
Listing 2
User program IOtest
Private Sub Form_Load()
i = OPENCOM(“COM2,1200,N,8,1”)
If i = 0 Then
i = OPENCOM(“COM1,1200,N,8,1”)
Option1.Value = True
End If
If i = 0 Then MsgBox (“COM Interface Error”)
TXD 1
RTS 1
DTR 1
TIMEINIT
End Sub
If you are new to Visual Basic it is a good idea
to choose some hardware that you can actu-
ally see working. For this reason a model traf-
fic light with three LEDs was chosen (
Fig-
ure 1
). The effects of current limiting and
reverse voltage on the LEDs were discussed in
the first article of this series.
The model lamp is well suited to experi-
menting for our first excursion into program-
ming with Visual Basic. For the program
development you will need to start with a
new blank form, and using the mouse, select
and drag graphic control elements from the
list of tools onto the form (
Figure 2
). The size
of each element and its position can be eas-
ily altered. Each element has a complete
range of properties that can be attached to it,
including size, colour, text and much more.
Those that you are not sure of yet can be sim-
ply left as they are. For our application we use
the following elements:
Private Sub Form_Unload(Cancel As Integer)
CLOSECOM
End Sub
Private Sub Option1_Click()
i = OPENCOM(“COM1,1200,N,8,1”)
If i = 0 Then MsgBox (“COM1 not available”)
TXD 1
RTS 1
DTR 1
End Sub
- Two labels with the properties (the text)
Caption = “fast” and “slow” respectively.
Private Sub Option2_Click()
i = OPENCOM(“COM2,1200,N,8,1”)
If i = 0 Then MsgBox (“COM2 not available”)
TXD 1
RTS 1
DTR 1
End Sub
- A horizontal slider or scroll bar (HScrollBar)
with the properties Min = 50, Max = 500
and Position =100
- A timer (Timer) with the property Interval =
100, i.e. 100 ms
Private Sub Timer1_Timer()
Check1.Value = CTS()
Check2.Value = DSR()
Check3.Value = DCD()
Check4.Value = RI()
If Check5.Value Then TXD 1 Else TXD 0
If Check6.Value Then DTR 1 Else DTR 0
If Check7.Value Then RTS 1 Else RTS 0
End Sub
- Two Option Buttons with the Caption =
“COM1” and “COM2” respectively, the but-
ton for COM2 is “true”
- The form itself contains the Caption = “Traf-
fic Light”
Figure 3
gives an overview of the project. The
aforementioned module PORTS.BAS is also
included in the project and contains all the
10/2000
Elektor Electronics
53
BEGINNERS
COURSE
Listing 3
Traffic lights program
Dim Time As Integer
TXD 1
RTS 1
DTR 1
End Sub
Private Sub Form_Load()
i = OPENCOM(“COM2,1200,N,8,1”)
If i = 0 Then
i = OPENCOM(“COM1,1200,N,8,1”)
Option1.Value = True
End If
If i = 0 Then MsgBox (“COM Interface Error”)
TXD 0
RTS 0
DTR 0
Time = 0
End Sub
Private Sub Timer1_Timer()
Time = Time + 1
If Time = 1 Then red
If Time = 40 Then redyellow
If Time = 50 Then green
If Time = 90 Then yellow
If Time = 100 Then Time = 0
End Sub
Sub red()
RTS 1
DTR 0
TXD 0
End Sub
Private Sub Form_Unload(Cancel As Integer)
CLOSECOM
End Sub
Sub redyellow()
RTS 1
DTR 1
TXD 0
End Sub
Private Sub HScroll1_Change()
Timer1.Interval = HScroll1.Value
End Sub
Private Sub Option1_Click()
i = OPENCOM(“COM1,1200,N,8,1”)
If i = 0 Then MsgBox (“COM1 not available”)
TXD 1
RTS 1
DTR 1
End Sub
Sub yellow()
RTS 0
DTR 1
TXD 0
End Sub
Sub green()
RTS 0
DTR 0
TXD 1
End Sub
Private Sub Option2_Click()
i = OPENCOM(“COM2,1200,N,8,1”)
If i = 0 Then MsgBox (“COM2 not available”)
necessary declarations for PORT.DLL. It
needs to linked with the software in each of
the projects so that they can all have access
to the procedures and functions that are
needed to control the serial interface.
In a Visual Basic program, events are
produced by procedures that are isolated
from each other. The overall flow of the pro-
gram is controlled by Windows. The proce-
dure ‘Private Sub Form_Load()’ will be called
right at the beginning of the program (
List-
ing 3
) This procedure contains all the instruc-
tions needed to initialise the serial interface.
In this case, the serial interface will be open
and all the outputs of the interface will be
switched off. Lastly, the global variable
‘Time’ is reset to zero.
The function OPENCOM in
PORT.DLL returns a value indicating
if the interface has been successfully
opened. It will not be possible to
open it if it is already in use by
another program. Initially the pro-
gram will use this function to open
COM2. If the return value from this
function is 0 i.e. it is busy then
COM1 will be opened. This will be
displayed on the screen with the
value of option button 1 (COM1)
‘True’. If both COM1 and COM2 are
busy then a failure message will pop
up in a MessageBox.
In the normal course of events
the program will open COM2. If
however you want to use COM1
then you can point to the corre-
sponding button and click. This will
cause Windows to call the proce-
dure Option1.Click which will open
COM1 and check for a successful
returned status. This automatic
selection and manual override has
already been used in the IOtest pro-
gram and will also used in the
upcoming program examples. It
would be a simple matter to add
extra buttons to control the COM3
and COM4 interface.
54
Elektor Electronics
10/2000
BEGINNERS
COURSE
Listing 4
Timerprocedure for blink program
Figure 2. The traffic light controller
form.
Figure 4. The traffic light program
during run time.
Private Sub Timer1_Timer()
Time = Time + 1
If Time = 1 Then
RTS 1
DTR 0
End If
If Time = 2 Then
RTS 0
DTR 1
End If
If Time = 2 Then Time = 0
End Sub
Figure 3. Project overview.
Figure 5. The Blinker Program.
Blinker/Clock generator
This next application is a blinker or clock gen-
erator with adjustable frequency (
Figure 5
).
The circuit is the same as for the traffic light
experiment. This time, the TXD output is per-
manently switched on while DTR and RTS are
switched in anti-phase, i.e., when one is on the
other is off and vice versa. It is not too difficult
to see how this program can be expanded to
turn it into a mini running light display.
The software from the traffic light pro-
gram is used again for this exercise. The
timer procedure is, however, new (
Listing 4
).
One difference here is that no additional pro-
cedures are called to control the output lines,
they are switched directly from inside ‘timer-
procedure’.
For the traffic light controller we
will need some timer function to
introduce a delay between chang-
ing the lights. In the DLL there is,
for example, a procedure called
DELAY. However, unlike program-
ming in DOS, individual Windows
programs do not have the entire
processing time devoted to them. It
would be pointless to write a pro-
cedure containing a simple timing
loop to give us the delays that we
need for the traffic light. Instead,
the traffic light timer will be event
controlled. For this we will use a
Windows timer. The timer period is
set to 100 ms. The procedure will be
called every 100 ms.
In the timer procedure there is
a variable called “Time” which is
incremented and gives the time in
tenths of a second, so the proce-
dure can be called to find out if a
specific period has elapsed before,
for example, the traffic light LEDs
are changed. The speed of switch-
ing has been chosen arbitrarily and
can be easily altered. The IF state-
ments are used to compare the
time values and switch the corre-
sponding LED.
The form (
Figure 4
) also con-
tains a horizontal slider or scroll
bar. This slider is used to alter the
speed of the traffic light changing.
As soon as the slider is moved, the
procedure
Hscroll1.Change
is
called. This procedure will change
the interval in the timer corre-
sponding to the actual position
(Value) of the slider control. The
slider is calibrated from 50 to 500;
the timer can therefore be altered in
the range from 50 ms to 500 ms.
The LEDs on the DTR and RTS lines should
now blink alternately. The speed of the blink-
ing can be increased or decreased. As you
increase the speed you may notice that the
timing for each blink is not precisely regular.
The reason for this is that Windows cannot
maintain a time interval of exactly 50 mil-
liseconds because it is a multitasking oper-
ating system many other processes will be
running at the same time. For this reason
Windows is generally considered to be not
capable of ‘real time’ operation. However,
there are some tricks and techniques that we
can use and we shall be investigating them
in the coming articles.
(00074-2e)
10/2000
Elektor Electronics
55
Plik z chomika:
TirNaNog
Inne pliki z tego folderu:
bge.jpg
(22 KB)
detail10.htm
(5 KB)
detail1.htm
(5 KB)
detail12.htm
(15 KB)
detail11.htm
(7 KB)
Inne foldery tego chomika:
1974
1975
1976
1977
1978
Zgłoś jeśli
naruszono regulamin