Visual Basic is the programming language developed by Microsoft. Visual Basic is a visual representation of the classic Basic programming language. Visual Basic 6 is the version of the programming application that was released prior to the .NET version. While programming with Visual Basic, you may need to access different parts of the computer so that the program can do what you need it to do. Read and/or send data to the serial ports on the computer by using the right Visual Basic coding.
- Skill level:
Other People Are Reading
Open Microsoft Visual Basic 6. The "New Project" dialogue box appears every time you open VB6. On the "New" tab, click on "Standard EXE." If you want to work in an existing project, click on the "Existing" tab and then select the project.
Click on the "Project" menu item and then "Add Module." Click "Open" when the "Add Module" dialogue box appears on the screen.
Type in a function to create an instance of the serial port on the computer. Since the serial ports are usually "Com1" and "Com2," use this as a reference to make sure you are connecting to the right serial port.
"Function OpenSerialPort() as String
Dim get String as String = " " Dim comSerial As IO.Ports.SerialPort comSerial = My.Computer.Ports.OpenSerialPort("Com1")
Type in the function that will read data from the serial using the "comSerial" variable that you already established.
"Function readSerialPort( comSerial as IO.Ports.SerialPort) as String
Dim get String as String = " "
Using comSerial Do Dim Receiving As String = comSerial.ReadLine() If Receiving Is Nothing Then Exit Do Else returnStr &= Receiving & vbCrLf End If Loop
End Using Return get String
Type in the following sub procedure that sends data to the serial port. Once this sub procedure is established in this module, you can call it from anywhere in the current project to send data to the serial ports any time you choose.
"Sub sendDataToSerial(ByVal data as String)
Using comSendData As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM1") comSendData.WriteLine(data)
Pass the information to the sub procedure using a variable named "data." An example of this is:
"Dim data As String
data = "This is example data to transmit to the serial port Com1"
Console.WriteLine("Write the data to the serial port COM1: " & data)
Place the code in any function, method or procedure during the project to write the data to the serial port. Run the code by pressing the "Ctrl" and "F5" keys together.
Save the project by clicking on the "Save" icon in the toolbar.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for