To control a serial port in Visual Basic, there is a method to use the .NET Framework SerialPort class and a method to use the Win32 API. Here we will introduce a programming example that sends and receives data using the relatively simple SerialPort class.
A COM port class (SerialPort component) has been added to the .NET Framework from Ver. 2.0. Visual Basic 2005 or later can use the .NET Framework SerialPort component which allows relatively simple control of a COM port. Please note that you can no longer use the MSComm control which had been often used in previous versions such as Visual Basic 6.
Select the SerialPort component on the Visual Basic "Toolbox", left-click and drag and drop the component to paste it on the form. When the component is finished being pasted, the component appears pasted under the form.
Click the component that has appeared to switch to the component's properties window. With the "PortName" property, set the port number that will be used for communication.The initial value is "COM1".With the "BaudRate" property, set the transmission rate.The initial value is "9600". You can also configure the other properties such as whether to use RTS.
Click the event button on the property window to display a list of the component's events. Select and double-click the event to use and the corresponding event routine is added. Write the code for the processing when the event has occurred in this location.
For serial port programming, there is also a method that calls the Wind32 API (application programming interface). Extensive knowledge of the Win32 API and programming languages is required, but it makes more advanced and flexible programming possible.
The Win32 API is the feature (function) set that Windows provides as standard.It is provided as DLLs and any application that runs on Windows can share and use it. Windows provides its primary functionality as around 1000 types of API functions, and application software is built combining this functionality. Visual Basic and Visual C have many commands, but this does not mean that it can use all of the Windows functionality, so you should call and use the API functions as necessary. Use the following API functions to control serial ports.