Serial Port Visual Basic Example Code !!TOP!! 📤


Serial Port Visual Basic Example Code

There are a number of different ways to access the serial port and in this tutorial we will be using Microsoft Visual Basic 2010 to try to access the serial port in VB 2010. We will access the serial port in both directions. This is a simple way to get started with writing to the serial port, but there are many different ways to access the serial port, and it is more complicated than it appears initially.

The serial port in VB 2010 is a real pain. Do note that in VB 2010 the serial port is still a “raw” serial port and you can’t assign a custom baud rate or data bits. If you need more options you will probably need to use an external library like this one.

This is a more advanced way to access the serial port from Microsoft Visual Basic 2010. You will have to allow the serial port to handle the data received and possibly send data back to the computer using a serial port baud rate of 115200 for this tutorial. Otherwise you will have a problem with the serial port as the main method is serialPortReceiveData(Bytes()). This means that we send data back to the computer using serialPortSendData(0x00, 0x00, 0x00). Another advantage is that you can adjust the data bits and the baud rate automatically.

Just as in C#, VB gives you a method for everything. We will create a class called SerialPort as shown below for easier reference later. We will add this class to the project:
SerialPort SerialPortClass Library Open the class “SerialPort.vb” and add the following code:

This is the complete code for the class “SerialPortClass”. You can easily study it, but the class has a lot of methods for a single class that we will use later. First we want to define an object to hold our port information and open the port. This is done by:
Dim port As SerialPort
port = New SerialPort()
port.PortName = “COM2”
port.BaudRate = 2400
port.Parity = SerialPortParity.None
port.DataBits = 8
port.StopBits = 1
port.Handshake = System.IO.Ports.Handshake.None
port.NewLine = “”
port.DtrEnable = False
port.RtsEnable = False
This is the complete part of the constructor
Now we can use this object to open the serial port or to check if the port is already open. If we want to check whether the port is open or not we want to ask the object about it; this is the reason why the method “Open()” returns a Boolean value.

This is the complete method for “Open()”. Now we can use this object to open the serial port or to check if the port is already open.

before i can start writing to a serial port, i need to open it. this is done with the following code:; this opens the serial port and starts the communication. if you run this code without opening the serial port, you will not receive anything.
to write to a serial port, we use the write method: myserialport.write(“a”); this will send an “a” (hex value 0x41) to the serial port. we can also use the writeline method like this: myserialport.writeline(“b”); this will send the “b” (hex value 0x42) to the serial port. the writeline method includes a line terminator (carriage return) automatically. if we were to omit the carriage return it would print “ab”. a carriage return is a line termination that is usually sent after the last character in a line of text so that the line appears on a new line in the output. in general, we would want to do this whenever we send data out as if it is a new line.
you may be asking yourself “why do i need to open and write to the serial port to begin with if the data is already there when it is received?” well you may be confused because you are used to serial ports being read-only. however, serial ports on the computer are treated the same as a telephone line. a person can “talk” and “listen” at the same time, and the other side will hear them. serial ports are usually “listening” at any given time so that they are ready to receive incoming data when a call comes in. if we were to write to the serial port while it was listening, we would be sending data to the wrong port. thats why we have to open the port before we write to it.