Using Visual Basic Part 6
By: Jared Hoylman
Receiving Data From
In the last
article we sent data to a microcontroller and had it
respond to the data that we sent...
article we are going to learn how to receive data
from a microcontroller and make the PC respond. For
simplicity we are going to have a Basic Stamp II get
the RCTime value of a simple circuit and send the
value to the PC for us to receive using VB.
We are going
to get the RCTime value in the Stamp as a Word. This
causes a little trouble when sending data to the PC,
so I will show you how to split the word into two
bytes, a HighByte and LowByte, and then join them
back together to preserve the accuracy of the RCTime
started open Visual Basic.|
new Standard EXE.|
to the Project | Components... menu|
the MSComm Control.|
on the yellow phone in the toolbar to
add the MSComm control to your form.|
add a label to your form and name it lblRCTime.|
On To The
Now that the form is set up and ready, we need to get
a quick understanding of how the MSComm control can
receive data from the serial port. There are
basically two methods, polling the port and
responding to communications events.
port is done by setting up a timer to check the
buffer for data, and if data is there, process it.
Polling is better when you have variable length data
that starts and ends with header and footer bytes
driven method is designed more for receiving fixed
length data. It is also better because you don't
waist time polling the buffer for data if none is
there. Instead the MSComm control will tell you when
data is there by firing the OnComm()
event. This event fires just like a Click()
event would fire if you clicked on a Command Button,
only it is not the users action that fires this
event, something must happen at the serial port.
When the OnComm()
event is fired you must check the value of the CommEvent
property to see what exactly happened. The CommEvent
property will contain a different value for each
different kind of communication event that occurs.
Below is a table that shows the event, the value of
the CommEvent property, and the
corresponding VB Constant...
data-set ready line.
carrier detect line.
||End of file.
project we are only concerned with the comEvReceive
constant which has the value of 2
and is fired when data is available in the buffer.
Now that we
have a feel for how the MSComm control will assist us
in receiving data, lets first set up the MSComm
control. Copy this commented code to your project...
' Fire Rx
Event Every Two Bytes
MSComm1.RThreshold = 2
Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 2
' 2400 Baud,
No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "2400,N,8,1"
' Disable DTR
MSComm1.DTREnable = False
' Open COM1
MSComm1.CommPort = 1
MSComm1.PortOpen = True
notice that there are two new properties in the above
code that have yet to be explained in these articles.
The first is RThreshold. In this
example, setting the RThreshold
property equal to 2 tells the MSComm
control to fire the comEvReceive event
when there are at least two bytes available in the
buffer. The second property, InputLen,
tells the MSComm control to only give us the first
two bytes when we request data from the buffer.
out of the way, lets take a look at the code that is
executed when the OnComm() event is
fired. The comments should help you along...
' Holds our
Long ' Holds LowByte value
' Holds the
comEvReceive Event then get data and display
MSComm1.CommEvent = comEvReceive Then
sData = MSComm1.Input
' Get data (2
Asc(Mid$(sData, 1, 1)) ' get 1st byte
Asc(Mid$(sData, 2, 1)) ' Get 2nd byte
' Combine bytes into a
lWord = (lHighByte *
&H100) Or lLowByte
' Convert value to a
string and display
code first checks the CommEvent
property to see if a comEvReceive
event has been fired. If so, we input the first two
bytes of data, combine the bytes to make a word, and
display the value in lblRCTime. Is
it easier than you thought..?
thing left to do is close the COM port when our
project is unloaded so...
MSComm1.PortOpen = False
For simplicity I am going to use a Basic Stamp II to
send the data to the PC. You will need to hook up the
Basic Stamp II just like you are going to program it
and use the programming port (pin 16) for
communications. Program the Basic Stamp II with the
|Result Var Word
need to interface the Basic Stamp with a few
components. Below is a schematic...
I used a 50k
ohm pot and a 2.2 uF cap.
This gave me results that almost covered the whole
range of the RCTime measurement.
hooking up the above circuit run your VB project,
power up your Stamp, adjust the pot and watch the
values change. You are now receiving data from the
quite get it..?
If you just can't get it to work, or want to check
out my project, I have uploaded something similar for
you to download. It is pretty much the same code, but
the form is more eye appealing and it shows the
RCTime result in an analog sliding scale.
Click here to download it...
This project could easily be amended to receive any
kind of data. Create you own data logging software,
plot data on a graph, even have your microcontroller
control your PC. The possibilities are endless..!
I hope you
enjoyed and learned from the Understanding
And Using Visual Basic series of articles.
This is the last of this series, but don't be
surprised if you see more of my articles at Rentron
in the future...
<< Sending Data | Intro |