Code for Form2 Load event

advertisement
How to Create a Communications Template in Visual Basic Express
The following is a guide to building a communications template with Visual Basic
Express and using the PC com port to communicate with a Basic Stamp Module.
It is not a guide to the VB IDE, and it is assumed that you have knowledge of how to
create a Windows application and navigate the IDE menus.
Most of the forms and controls are left with their default names and properties and it’s up
to you to finalize the design.
Outline
The project contains two forms and one module.
Form1 is solely there as an interface for you to build your application on after the
template is finished. We will work an example of this later.
Form2 is for the user to configure communications and where most of the template code
will go.
Module1 is a container for global code
The idea is for Form1 to be displayed when our application is first run. Form1 contains a
menu that allows us to bring up Form2 (the configuration form) where we can select baud
and port number. We would then press Connect and close the configuration.
Now with a few buttons and textboxes we can communicate/control serially from Form1.
To close the port we would bring up Form2 and hit the Disconnect button.
Add the Forms and Module
Open a new VB windows application and add one extra form and one module so that you
have Form1, Form2 and Module1.
Controls Form1
The only control we want for now is a Menu Strip. Where it says “Type Here” in the
menu strip type Comms. After you have typed Comms double click the text and it will
open Code View and insert the PortToolStripMenuItem_Click event.
The only code needed here for now is
Form2.Show()
Run the app and check that the menu brings up Form2 before going on to the next step.
Module1 Code
Module1 is a general-purpose container for code and variables for use with other forms in
our program. The only line we require for now is to declare our Serial port.
Public _port As New System.IO.Ports.SerialPort
Controls Form2
Switch to design view for Form2 and add three buttons and two combo boxes
Form2 Code.
The code in Form2 is all contained in four events, Form2_Load, Button1_Click,
Button2_Click and Button3_Click.
The button designations are as follows
Button1 = Port open
Button2 = Port close
Button3 = Form2 close
Code for Button3 click event
Me.Close()
Code for Button2 click event
Button2.Enabled = False
Button1.Enabled = True
_port.Close()
Code for Button1 click event
If Not ComboBox1.SelectedItem Is Nothing And Not ComboBox2.SelectedItem
Is Nothing Then
If Not _port.IsOpen Then
Try
_port.BaudRate = ComboBox2.SelectedItem
_port.PortName = ComboBox1.SelectedItem
_port.Open()
Button1.Enabled = False
Button2.Enabled = True
Catch ex As Exception
MsgBox("Error trying to open the port",
MsgBoxStyle.Critical)
End Try
End If
Else
MsgBox("Select a Port and BaudRate",
MsgBoxStyle.Information)
End If
Code for Form2 Load event
ComboBox2.Items.Add(2400)
ComboBox2.Items.Add(4800)
ComboBox2.Items.Add(9600)
For Each portName As String In My.Computer.Ports.SerialPortNames
ComboBox1.Items.Add(portName)
Next
If Not _port.IsOpen Then
Button2.Enabled = False
ComboBox1.Text = ComboBox1.Items(0)
ComboBox2.Text = ComboBox2.Items(0)
Else
Button1.Enabled = False
ComboBox1.Text = _port.PortName
ComboBox2.Text = _port.BaudRate
End If
After you have entered all the code run the project and make sure everything looks good.
Testing the Program with a Stamp
First let’s get the Visual Basic program ready to send data.
Add one button and one textbox to Form1.
Insert the following code into the button’s click event
Dim send As Byte = Val(TextBox1.Text)
If _port.IsOpen Then
_port.WriteLine(send)
End If
Here is the SERIN command we need at the Stamp, you will need to add additional code
to determine that the number VB is sending is actually what we are receiving, perhaps
light up an LED.
X Var Byte
SERIN 16,baud, [DEC X]
Run the program and configure and open the port.
Typing a number into the VB textbox between 0 and 255 and clicking the button will
transmit the number to the Stamp.
Appendix A
Visual Basis (VB) Help
This information is supplemental to the original document. It covers the VB aspect for
those just learning the language. This information is for users of VB Express 2010.
However the same principles may be applied to earlier versions.
Original Text
Add the Forms and Module
Open a new VB windows application and add one extra form and one module so that you
have Form1, Form2 and Module1.
Using the Tutorials available from Microsoft, the “Picture Viewer” tutorial can be used to
establish a project with a form. Once the only the form has been placed, continue with the
instruction in this document.
Once the form has been placed, go to the solution box and right-click over the project
name (e.g. the MS tutorial suggests naming the project “PictureViewer”, so, that would
be the name found at the top of the directory in the solutions box.) Scroll to “Add”, then
select “Windows Form” to add Form2, Repeat this process, but, scroll to “Module” and
select it. This will place the required Form2 and Module1.
Controls Form1
The only control we want for now is a Menu Strip. Where it says “Type Here” in the
menu strip type Comms. After you have typed Comms double click the text and it will
open Code View and insert the PortToolStripMenuItem_Click event.
The only code needed here for now is
Form2.Show()
The Menu strip is found in the toolbox to the left of the IDE. However, when the Menu
strip is double clicked and the Code view is presented in VB 2010,
CommsToolsStripMenuItem_Click is placed in the code rather than the PortToolStrip
reference. Place Form2.Show() as shown.
Public Class Form1
Private Sub CommsToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
CommsToolStripMenuItem.Click
Form2.Show()
End Sub
End Class
Module1 Code
Module1 is a general-purpose container for code and variables for use with other forms in
our program. The only line we require for now is to declare our Serial port.
Public _port As New System.IO.Ports.SerialPort
Click on the Module1.vb tab to view that code. There will be Module and End Module.
Place the above code between as shown below:
Module Module1
Public _port As New System.IO.Ports.SerialPort
End Module
Controls Form2
Switch to design view for Form2 and add three buttons and two combo boxes
Select the Form2 design window and double click the form. This will establish the code
for the Form2_load event. It should be noted that adding Form2 establishes the Public
Class for the form. Double clicking the form then establishes the load event for that form.
Next add button1 and double click it. Repeat for button 2, button 3, combobox1, and
combobox2. Double clicking each one establishes a subroutine for each.
At this point, the remaining code should be placed in each of the corresponding sections,
between the Private sub and End sub lines, as outlined in the original document. No
functional code has been placed in the combo box subroutines. But establishing them
allows the editor to recognize their existence.
Download