Click here to Skip to main content
12,300,021 members (62,430 online)
Click here to Skip to main content
Add your own
alternative version


88 bookmarked

Communication on a serial port in NET 2.0

, 16 Jan 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple 'How To' to get you started with serial port communiction in ,NET 2.0


A few days ago I said to myself that I wanted to know more about how to communicate via the serial port. When I first started searching the internet about this subject I found out that it’s not many articles that are discussing this subject and those examples that I found was mostly about the earlier VB6 MSComm control and wrappers for this control. Those matters concerning the MSComm control were not very interesting because I had read that in .NET 2.0 Microsoft had come up with a new serial port control.

As a newbie I have been spending some hours of my time to come up with what I now share with you, but as you all know it’s worth every hour when you succeed. It’s actually not a big deal to do it; it’s just a few lines of code.

Fore those of you who are familiar with the serial port communication I want to recommend this article on the Code Project. It is an excellent article about communicating with mobile phones via the serial port, and it is very clear when you fist know the basic.

The task of this example is very simple. We want to send a text string from one computer via the serial port to another computer. First of all you have to bee in the position that you have 2 computers and second you got to have a “null modem cable”. Another option is that you have 2 serial ports on the computer and connecting them with a “null modem cable”

If you don’t know what a “null modem cable” is then search the internet to see how it is configured.

First of all we want to write to the serial port, and here is the basic.

If you have 2 computers have this one on the first computer. If you have one computer make this a separate project.

In this example you got to have a Button control called btnSendText and a textBox control called txtSendText on your form on computer nr1. Just type in some text in the btnSendText control and Click Button send to send it to COM1.

Imports System
Imports System.IO.Ports

Public Class Form1
    Dim WithEvents Port As SerialPort = _
                 New SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
    Private Sub btnSendText_Click(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnSendText.Click
        Port.Write(txtSendText.Text & "!%")
    End Sub
End Class

This is just how simple it is to send a text string to the serial port.

And now how to receive the text string on the other computer.

If you have 2 computers have the next example one on the second computer. If you have one computer make this a separate project, and then run both projects at the same time.

In this example you got to have a Text control called TextBox1 and a listbox control called ListBox1 on your form on computer nr2. When clicking send on computer nr1 you will receive it in the textbox1 control on computer nr 2. When the buffer is 0 it will be added to the Listbox1 control, and ListBox1 is empty to receive the next incoming text string.

Imports System
Imports System.IO.Ports

Public Class Form1
    Dim WithEvents port As SerialPort = New _ 
     System.IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
                           System.EventArgs) Handles Me.Load
       CheckForIllegalCrossThreadCalls = False
       If port.IsOpen = False Then port.Open()
    End Sub

    Private Sub port_DataReceived(ByVal sender As Object, ByVal e As _
       System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
       TextBox1.Text = (port.ReadTo("!%"))
       If port.ReadExisting.Length = 0 Then
           TextBox1.Text = ""
       End If
    End Sub
End Class

The important thing to notice is that you have to declare the port like this “Dim WithEvents port ...

You also got to have a “CheckForIllegalCrossThreadCalls = False” declaration in the in the form load procedure to prevent it from raising an error when a thread other than the creating thread of a control tries to access one of that control's methods or properties. You also have to check if the port is open, and if it’s not open you have to open it.

As you may see that I have some special characters in both the write and the read statement.

port.Write(txtSendText.Text & "!%") and port.ReadTo("!%").

This is because if I put some special characters in the write statement stream I can ask the readTo statement to read everything until the special character and that is quiet convenient. Just test it.

There are many other options to the serial port communication, and this is only one of them.

I hope it can be of any help to somebody.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sigurd Johansen
Web Developer
Norway Norway
No Biography provided

You may also be interested in...

Comments and Discussions

AnswerRe: Bluetooth instead of serial port? Pin
gpraceman10-Jul-08 3:43
membergpraceman10-Jul-08 3:43 
GeneralSerialPort Object Buggy Pin
gpraceman30-Jun-08 18:40
membergpraceman30-Jun-08 18:40 
GeneralMulti thread safe Pin
oferebert12-Jun-08 2:44
memberoferebert12-Jun-08 2:44 
GeneralNeither open nor closed Pin
MarceRos15-May-08 0:46
memberMarceRos15-May-08 0:46 
GeneralRe: Neither open nor closed Pin
Sigurd Johansen16-May-08 9:21
memberSigurd Johansen16-May-08 9:21 
QuestionASP.Net? Pin
Technoshaman2-Apr-08 0:57
memberTechnoshaman2-Apr-08 0:57 
AnswerRe: ASP.Net? Pin
Technoshaman6-Apr-08 5:24
memberTechnoshaman6-Apr-08 5:24 
GeneralRe: ASP.Net? Pin
Sigurd Johansen6-Apr-08 10:04
memberSigurd Johansen6-Apr-08 10:04 
GeneralI have a problem with your program Pin
linamavilla11-Feb-08 8:53
memberlinamavilla11-Feb-08 8:53 
GeneralRe: I have a problem with your program Pin
Sigurd Johansen12-Feb-08 5:09
memberSigurd Johansen12-Feb-08 5:09 
GeneralNeed help ,How To Connect serial with Cach Box Pin
Mujahid10-Feb-08 8:15
memberMujahid10-Feb-08 8:15 
GeneralRe: Need help ,How To Connect serial with Cach Box Pin
Sigurd Johansen10-Feb-08 10:51
memberSigurd Johansen10-Feb-08 10:51 
Generalneed help pls, reading from usb port Pin
adnanmn23-Jan-08 19:54
memberadnanmn23-Jan-08 19:54 
GeneralRequired Help Pin
kevinbhavasar10-Sep-07 0:45
memberkevinbhavasar10-Sep-07 0:45 
GeneralRe: Required Help Pin
Sigurd Johansen11-Sep-07 3:55
memberSigurd Johansen11-Sep-07 3:55 
Generalhelp me on transfer data between modems Pin
nkcspecial19-Aug-07 1:14
membernkcspecial19-Aug-07 1:14 
Generalserial port usb adapter Pin
rigidigi30-Jul-07 9:13
memberrigidigi30-Jul-07 9:13 
GeneralRe: serial port usb adapter Pin
Sigurd Johansen31-Jul-07 9:36
memberSigurd Johansen31-Jul-07 9:36 
GeneralRe: serial port usb adapter Pin
Torpedoke-be8-Mar-08 11:00
memberTorpedoke-be8-Mar-08 11:00 
GeneralRe: serial port usb adapter Pin
rigidigi2-Jul-09 8:41
memberrigidigi2-Jul-09 8:41 
GeneralRe: serial port usb adapter [modified] Pin
gpraceman30-Jun-08 18:27
membergpraceman30-Jun-08 18:27 
QuestionText files Pin
db@Jax11-Jul-07 8:21
memberdb@Jax11-Jul-07 8:21 
Questionc sharp Pin
Pushkar Joshi21-May-07 0:27
memberPushkar Joshi21-May-07 0:27 
AnswerRe: c sharp Pin
Sigurd Johansen21-May-07 1:25
memberSigurd Johansen21-May-07 1:25 
GeneralRe: c sharp Pin
Pushkar Joshi21-May-07 18:07
memberPushkar Joshi21-May-07 18:07 
Questionhow to dial a number with serialport? Pin
mehrdadsh10-May-07 3:27
membermehrdadsh10-May-07 3:27 
AnswerRe: how to dial a number with serialport? Pin
Sigurd Johansen14-May-07 20:24
memberSigurd Johansen14-May-07 20:24 
GeneralRe: how to dial a number with serialport? Pin
VachanC29-Apr-08 20:50
memberVachanC29-Apr-08 20:50 
Questionmessage arrived after port closed Pin
Taoge8-Apr-07 17:58
memberTaoge8-Apr-07 17:58 
AnswerRe: message arrived after port closed Pin
Sigurd Johansen9-Apr-07 3:47
memberSigurd Johansen9-Apr-07 3:47 
QuestionIn Web Pin
Panda197527-Feb-07 6:19
memberPanda197527-Feb-07 6:19 
GeneralThank you Pin
vmayzel24-Feb-07 9:42
membervmayzel24-Feb-07 9:42 
Generalevent object Pin
kuncung5-Feb-07 21:17
memberkuncung5-Feb-07 21:17 
GeneralRe: event object Pin
Sigurd Johansen6-Feb-07 10:58
memberSigurd Johansen6-Feb-07 10:58 
Generalerror in using System.IO.Ports Pin
Member #374662521-Jan-07 1:29
memberMember #374662521-Jan-07 1:29 
GeneralRe: error in using System.IO.Ports Pin
Syed Muhammad Kamran23-Jan-07 0:49
memberSyed Muhammad Kamran23-Jan-07 0:49 
GeneralCombine RD & TD to form a loop to test this. Pin
Syed Muhammad Kamran16-Jan-07 23:42
memberSyed Muhammad Kamran16-Jan-07 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 17 Jan 2007
Article Copyright 2007 by Sigurd Johansen
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid