"Serial Communication"--a simple example

Source: Internet
Author: User

The last article to tell you about the serial port programming some of the basic knowledge. Today, we are going to introduce a simple example of serial port programming. This example can help you to have a general understanding of the basic knowledge of serial communication.

The first is to look at the interface:


A bit like a chat software, in fact you can think so. But the object you are facing may be a hardware.

And then let's look at the code:

Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Windows.Forms public Class Form1 public Sub New ( ) InitializeComponent () end Sub private com as System.IO.Ports.SerialPort private Sub Form1_Load (Byva 
        L sender as System.Object, ByVal e as System.EventArgs) Handles mybase.load com = New System.IO.Ports.SerialPort ()
        ' Get all the serial Dim pc as Microsoft.VisualBasic.Devices.Computer = New Microsoft.VisualBasic.Devices.Computer () Dim s as String for each s in PC. Ports.serialportnames Me.cbxPortName.Items.Add (s) Next End Sub Private Sub Button1_Click (Byva L sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click If isnothing (cbxportname.selectedite m) = False Then com. Close () com.
       PortName = cbxPortName.SelectedItem.ToString ()     Com. Open () If (COM. IsOpen) Then btnclose.enabled = True Lbstatus.text = "Serial port" + cbxPortName.SelectedItem.ToSt
            Ring () + "already Connected" Else MessageBox.Show ("Please choose serial port!") End If End If End Sub Private Sub btnClose_Click (ByVal sender as System.Object, ByVal e as System.eventar GS) Handles Btnclose.click If (com. IsOpen) Then com.
        WriteLine (Txtsendmsg.text) Else MessageBox.Show ("Please connect the serial port first!") End If End Sub Private sub Btnreceive_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles b Tnreceive.click Txtreceive.text = "" If (COM. IsOpen) Then Try com. Discardinbuffer () com. Discardoutbuffer () Txtreceive.text = com. ReadLine () Catch Throw end Try Else MessageBox.Show ("Please connect serial port first!") ) End If End SUB End Class
 

The code above can be copied and implemented.

Of course, I have said this is a very simple example. One of the things I used to sum up:

The core of the above code is a SerialPort class that represents a serial port resource. One of the most important of the two methods used to send data (write) and read data (read) Of course, you may still have questions, serial communication can only have the hardware to study. The answer is, of course, negative. You can use the virtual serial program to study. To provide you with a very good virtual serial Port program: VSPM. Download Address: http://download.csdn.net/detail/hy6688_/6705423

Basic steps to use;

After installation, the working mode selection should be: VSPM running in server mode, supporting the client mode device and then using the device detector resume virtual serial port. (If you choose to set up the default serial port, the software will automatically help you build four virtual serial port) after that is the new virtual serial port arbitrary selection (here you build that can be selected in the program), select the IP address listening: My own IP as 192.168.24.129, The client-mode device attempts to connect to this port: write a random one, generally do not and other conflicts on the line. 5000 after the general no problem. The default is to choose 6020 here.

After the establishment: Select this serial port, and then select the menu bar: Check this device connection, input just added when the selected IP address: 92.168.24.129, just select the port: 6,020 Click to reconnect, This window does not reflect: but the main window of the Port rightmost column: The last operation: Display serial port: COM2 did not open. Then let's write a good program to run up and click on the link. And then enter the data in the Send text box click Send To try our virtual string can receive data in the mouth:

Then select the menu bar-track serial data and click the Receive command for the small example we made. The Receive text box in our example receives the information we just wrote when we enter some characters in the Telnet admin form's display infobar and press ENTER. And in the tracking serial data has the display. As shown in figure:

To sum up: just a simple serial port programming small example of which there are many problems, if you really hands-on practice you will find the problem. Not even meet our basic chat function. These questions are left to the next article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.