打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Communication on a serial port in NET 2.0

Introduction

A few days ago I said to myself that I wanted to know more about howto communicate via the serial port. When I first started searching theinternet about this subject I found out that it’s not many articlesthat are discussing this subject and those examples that I found wasmostly about the earlier VB6 MSComm control and wrappers for thiscontrol. Those matters concerning the MSComm control were not veryinteresting because I had read that in .NET 2.0 Microsoft had come upwith a new serial port control.

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

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

The task of this example is very simple. We want to send a textstring from one computer via the serial port to another computer. Firstof all you have to bee in the position that you have 2 computers andsecond you got to have a “null modem cable”. Another option is that youhave 2 serial ports on the computer and connecting them with a “nullmodem 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 SystemImports System.IO.PortsPublic Class Form1Dim 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.ClickPort.Open()Port.Write(txtSendText.Text & "!%")Port.Close()End SubEnd 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 secondcomputer. If you have one computer make this a separate project, andthen 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 onyour form on computer nr2. When clicking send on computer nr1 you willreceive it in the textbox1 control on computer nr 2. When the buffer is0 it will be added to the Listbox1 control, and ListBox1 is empty to receive the next incoming text string.

Imports SystemImports System.IO.PortsPublic Class Form1Dim 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.LoadCheckForIllegalCrossThreadCalls = FalseIf port.IsOpen = False Then port.Open()End SubPrivate Sub port_DataReceived(ByVal sender As Object, ByVal e As _System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceivedTextBox1.Text = (port.ReadTo("!%"))If port.ReadExisting.Length = 0 ThenListBox1.Items.Add(TextBox1.Text)TextBox1.Text = ""End IfEnd SubEnd 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 fromraising an error when a thread other than the creating thread of acontrol 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 youhave 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.

Sigurd Johansen


Click here to view The article of origin.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#批量操作控件
Serial Communication using C# and Whidbey
VBA 限制输入中文或只能输入数字
VB.NET获取系统当前精确时间(毫秒级)
USB转串口之minicom的设置
WPF 数据触发器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服