"Micro-News"
Micro-Mailing-List
Understanding and Using Visual Basic Part 5
By: Jared Hoylman -
Sending Data To A Microcontroller
In the last article we went a little more in depth into Advanced String Parsing. These skills will be needed when receiving data from a microcontroller. All of the data received will be in a buffer and you will have to
separate the data, and use it accordingly.
In this article we are going to learn how to send data to a microcontroller and make the microcontroller respond to the data. For simplicity we are just going to send data to turn certain pins on and off.
So lets start off with designing a communications protocol. From VB we will send an ASCII 255, as a synch byte, the pin number (0 to 15), and the on/off state (0 or 1). The microcontroller will wait for three (3) bytes of data. After these three bytes of data are received, it will then either switch the pins to their High or Low state.
The VB Part
 | To get started open Visual Basic. |
 | Start a new Standard EXE. |
 | Next go to the Project | Components... menu |
 | Check the MSComm Control. |
 | Click OK. |
 | Next double-click on the yellow phone in the toolbar to add the MSComm control to your form. |
Your form should look like this...
Next we are going to add a Combo Box to our form and name it cboPinNumber. We are also going to add an Option Button to the form. Add the first one and name it optState. Select optState and press Ctrl-C to copy, and then Ctrl-V to paste. You will get a propmt like the one below...
Select Yes.
Now you should notice that the first option button that you created has a name of optState(0) and the second one has a name of optState(1). The number after the control name is the control array index. We will use this to our advantage later. Also add a Command button named cmdSend to the form.
Go ahead and pretty up your form and add some labels to make it look better. The only
requirement is that you set optState(0)'s Caption property equal to "Low", optState(1)'s Caption property equal to "High", cmdSend's Caption property equal to "Send", and cboPinNumber's Style property equal to (2). Your form should look something like mine, below...

On To The Code
Now that the form is set up and ready to go, we need to start adding our code to
the project. The user will select a pin number from cboPinNumber, select a pin state from optState, and then click cmdSend to send the data to the microcontroller. So first of all we need to get the pin numbers into the combo box, set up the MSComm control, and select one of the pin states to start with. So lets add the following code to our form...
Private Sub Form_Load()
Dim Pins As Long
' Add the pin numbers 0 to 15 to cboPinNumber
For Pins = 0 To 15
cboPinNumber.AddItem CStr(Pins)
Next Pins
' Default to Pin 0 being selected
cboPinNumber.ListIndex = 0
' Default to optState(0) being selected
optState(0).Value = True
' Use COM1
MSComm1.CommPort = 1
' 2400 baud, no parity, 8 data bits, 1 stop bit
MSComm1.Settings = "2400,N,8,1"
' Disable DTR
MSComm1.DTREnable = False
' Open the port
MSComm1.PortOpen = True
End Sub
|
Now we just need to add the code to send the data. When the user presses cmdSend, we need to do three things.
 | Get the pin number... |
 | Get the pin state... |
 | Send the data... |
Getting the pin number is very easy with the way that we have set up our form. As you may know a combo box lists items starting from a zero index. So this means that if we select pin 7 in the combo box, the combo box's ListIndex property is equal to 7.
To get the selected state we simply write a short If statement like so...
Dim PinState As Long
If optState(0).Value = True Then
PinState = 0
Else
PinState = 1
End If
|
Now lets put it all together and send the data when we press the cmdSend button...
Private Sub cmdSend_Click()
Dim PinNumber As Long
Dim PinState As Long
' Get Pin Number
PinNumber = cboPinNumber.ListIndex
' Get Pin State
If optState(0).Value = True Then
PinState = 0
Else
PinState = 1
End If
' Send Out Data
MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(PinState)
End Sub
|
So we sent out the synch byte (255), followed by the pin number, followed by the pin state.
Finally we need to close the comm port when the VB project unloads so...
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
|
We are finished with the VB part...! Not so bad, huh..?
The Microcontroller Part
For simplicity I am going to use a Basic Stamp II to receive the data and act accordingly to the data. 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...
PinNumber var byte
PinState var byte
Main:
' Use the programming port to receive
' data at 2400 baud
' Wait for the synch byte (255) and then
' get the PinNumber and PinState
Serin 16,16780,[WAIT(255),PinNumber,PinState]
' If PinState=0 Then go to GoLow
' otherwise go to GoHigh
Branch PinState,[GoLow,GoHigh]
Goto Main
' Set The pin low
GoLow:
LOW PinNumber
Goto Main
' Set the pin high
GoHigh:
HIGH PinNumber
Goto Main
|
Now run the VB project, hook up some LEDs to some of the pins, select the pin number, select either HIGH or LOW, and press send. You should see the LED turn ON when you send a high command, and turn OFF when you send a LOW command.
Conclusion
This project could easily be expanded to send PWM, Serial data, ShiftOut data, and frequencies with just a little work. In the next article we will master the art of receiving data from the stamp and using it in VB..! Until then...have fun..!
Got questions...? Contact the author Jared
Hoylman.....
To download a copy of Jared's VB project click here.
<< Advanced Parsing |
Intro | Receiving
Data From The Microcontroller >>
|