PicBasic Pro, PicBasic, BASIC Stamp, Microchip PIC, 8051, and Remote Control Projects

  BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List"Micro-News"BASIC Stamp, Microchip PIC, 8051, and Remote Control Mailing-List
  Micro-Mailing-List

Understanding and Using Visual Basic Part 6
By: Jared Hoylman -

Receiving Data From A Microcontroller

In the last article we sent data to a microcontroller and had it respond to the data that we sent...

In this 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 measurement...

The VB Part

bulletTo get started open Visual Basic.
bulletStart a new Standard EXE.
bulletNext go to the Project | Components... menu
bulletCheck the MSComm Control.
bulletClick OK.
bulletNext double-click on the yellow phone in the toolbar to add the MSComm control to your form.
bulletFinally add a label to your form and name it lblRCTime.



On To The Code
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.

Polling the 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 respectively.

The event 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...

Constant Value Description
comEvSend 1 Send event.
comEvReceive 2 Receive event.
comEvCTS 3 Change in clear-to-send line.
comEvDSR 4 Change in data-set ready line.
comEvCD 5 Change in carrier detect line.
comEvRing 6 Ring detect.
comEvEOF 7 End of file.

In this 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...

Private Sub Form_Load()

' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 2

' When 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

End Sub

You may 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.

With that 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...

Private Sub MSComm1_OnComm()
Dim sData As String ' Holds our incoming data
Dim lHighByte As Long   ' Holds HighByte value
Dim lLowByte As Long    ' Holds LowByte value
Dim lWord As Long       ' Holds the Word result

' If comEvReceive Event then get data and display
If MSComm1.CommEvent = comEvReceive Then

    sData = MSComm1.Input
' Get data (2 bytes)
    lHighByte = Asc(Mid$(sData, 1, 1))
' get 1st byte
    lLowByte = Asc(Mid$(sData, 2, 1))  
' Get 2nd byte
    
    
' Combine bytes into a word
    lWord = (lHighByte * &H100) Or lLowByte
    
    
' Convert value to a string and display
    lblRCTime.Caption = CStr(lWord)
    
End If
End Sub

The above 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..?

The only thing left to do is close the COM port when our project is unloaded so...

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen =
False
End Sub

The Microcontroller Part
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 following code...

Result Var Word

Main:
High 0
Pause 1
RCTime 0,1,Result
Serout 16,16780,[Result.HighByte, Result.LowByte]
Pause 100
Goto Main

You will 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.

After 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 Stamp..!

Can't 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...

Conclusion
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 | 

Copyright © 1999-2007
Reynolds Electronics

| Contact Information |