A few days ago I said to myself that I wanted to know more about how to communicate via the. 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 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 this article on the Code Project. It is an excellent article about communicating with mobile phones via the , and it is very clear when you fist know the basic.communication I want to recommend
The task of this example is very simple. We want to send a text string from one computer via theto 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 s 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, 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
btnSendTextand a textBox control called
txtSendTexton your form on computer nr1. Just type in some text in the
btnSendTextcontrol and Click Button send to send it to COM1.
System System.IO. s Form1 = _ ( , , Parity.None, , StopBits.One) btnSendText_Click( sender System. , _ e System.EventArgs) btnSendText.Click .Open() .Write(txtSendText.Text & ) .Close()
This is just how simple it is to send a text string to the.
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
TextBox1and a listbox control called
ListBox1on 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
ListBox1is empty to receive the next incoming text string.
System System.IO. s Form1 = _ System.IO. s. ( , , Parity.None, , StopBits.One) Form1_Load( sender , e _ System.EventArgs) .Load CheckForIllegalCrossThreadCalls = .IsOpen = .Open() _DataReceived( sender , e _ System.IO. s. DataReceivedEventArgs) .DataReceived TextBox1.Text = ( .ReadTo( )) .ReadExisting.Length = ListBox1.Items.Add(TextBox1.Text) TextBox1.Text =
The imant thing to notice is that you have to declare the like this “
Dim WithEvents” ...
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 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.
.Write(txtSendText.Text & ) .ReadTo( ).
This is because if I put some special characters in the write statement stream I can ask the
readTostatement to read everything until the special character and that is quiet convenient. Just test it.
There are many other options to thecommunication, and this is only one of them.
I hope it can be of any help to somebody.