- 浏览: 70360 次
- 性别:
- 来自: 厦门
最新评论
-
BlueBing:
linyuliang 写道BlueBing 写道我想再做些修改 ...
IBatis Abator去除注释版 -
linyuliang:
BlueBing 写道我想再做些修改 可否提供下源码呢晚上我回 ...
IBatis Abator去除注释版 -
linyuliang:
xiaohu7924 写道为什么我用这个生成的名字不是你说的驼 ...
IBatis Abator去除注释版 -
xiaohu7924:
为什么我用这个生成的名字不是你说的驼锋名字,还是老样子
IBatis Abator去除注释版 -
BlueBing:
我想再做些修改 可否提供下源码呢
IBatis Abator去除注释版
原文作者:ivanx
转载自:http://bbs.msproject.cn/
[翻译]
Tapan Dantre.著Serial Communication using C# and Whidbey
[简介]
本文将介绍如何在.NET平台下使用C#创建串口通信程序,.NET 2.0提供了串口通信的功能,其命名
空间是System.IO.Ports。这个新的框架不但可以访问计算机上的串口,还可以和串口设备进行通信。
我们将使用标准的RS <!----><st1:chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="True" sourcevalue="232" unitname="C">232 C</st1:chmetcnv> 在PC间通信。它工作在全双工模式下,而且我们不打算使用任何的握手
或流控制器,而是使用无modem连接。
命名空间
System.IO.Ports命名空间中最重用的是SerialPort 类。
创建SerialPort 对象
通过创建SerialPort 对象,我们可以在程序中控制串口通信的全过程。
我们将要用到的SerialPort 类的方法:
ReadLine():从输入缓冲区读一新行的值,如果没有,会返回NULL
WriteLine(string):写入输出缓冲
Open():打开一个新的串口连接
Close():关闭<!----><o:p></o:p>
Code: <o:p></o:p>
//create a <st1:place w:st="on"><st1:placename w:st="on">Serial</st1:placename> <st1:placetype w:st="on">Port</st1:placetype></st1:place> object
SerialPort sp = new SerialPort ();<o:p></o:p>
默认情况下,DataBits 值是8,StopBits 是1,通信端口是COM1。这些都可以在下面的属性中重新设置
:
BaudRate:串口的波特率
StopBits:每个字节的停止位数量
ReadTimeout:当读操作没有完成时的停止时间。单位,毫秒
还有不少其它公共属性,自己查阅MSDN。
串口的硬件知识
在数据传输的时候,每个字节的数据通过单个的电缆线传输。包包括开始位,数据,结束为。一旦
开始位传出,后面就会传数据,可能是5,6,7或8位,就看你的设定了。发送和接收必须设定同样
的波特率和数据位数。
无猫模式
没有Modem模式的电缆只是简单地交叉传送和接收线。同样DTR & DSR, 和 RTS & CTS也需要交叉。
RS232针图
<!----><v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id="_x0000_i1027" style="WIDTH: 384pt; HEIGHT: 209.25pt" alt="RS232针图" type="#_x0000_t75"><v:imagedata o:href="http://bbs.msproject.cn/upload_image/8271b993-2871-4ba9-8baf-2966541307a7.jpg" src="file:///C:\DOCUME~1\WHITEL~1\LOCALS~1\Temp\msohtml1\01\clip_image001.jpg"></v:imagedata></v:shape>
这里,我们三条线。互连2和3(一段的2pin连接3pin),连接两端的5pin。
[示例程序]
主程序
<v:shape id="_x0000_i1028" style="WIDTH: 301.5pt; HEIGHT: 228.75pt" alt="主程序" type="#_x0000_t75"><v:imagedata o:href="http://bbs.msproject.cn/upload_image/d5b82699-bfc6-4a3d-84a3-240041ece64a.jpg" src="file:///C:\DOCUME~1\WHITEL~1\LOCALS~1\Temp\msohtml1\01\clip_image002.jpg"></v:imagedata></v:shape>
如果想使用默认属性,按“Save Status”按钮,如果想改变属性按“Property”。它会弹出下图:
<v:shape id="_x0000_i1025" style="WIDTH: 225pt; HEIGHT: 254.25pt" alt="image" type="#_x0000_t75"><v:imagedata o:href="http://bbs.msproject.cn/upload_image/00e44a01-d6e6-411c-a9a1-35b1ed0c0c0e.jpg" src="file:///C:\DOCUME~1\WHITEL~1\LOCALS~1\Temp\msohtml1\01\clip_image003.jpg"></v:imagedata></v:shape>
设定好之后,可以通信了。
<v:shape id="_x0000_i1026" style="WIDTH: 300.75pt; HEIGHT: 229.5pt" alt="image" type="#_x0000_t75"><v:imagedata o:href="http://bbs.msproject.cn/upload_image/32482017-a212-4023-aed3-544e423eb879.jpg" src="file:///C:\DOCUME~1\WHITEL~1\LOCALS~1\Temp\msohtml1\01\clip_image004.jpg"></v:imagedata></v:shape>
<v:shape id="_x0000_i1029" style="WIDTH: 301.5pt; HEIGHT: 228.75pt" alt="image" type="#_x0000_t75"><v:imagedata o:href="http://bbs.msproject.cn/upload_image/de6e558f-2406-41ab-8ae3-89ea7e4acd6d.jpg" src="file:///C:\DOCUME~1\WHITEL~1\LOCALS~1\Temp\msohtml1\01\clip_image005.jpg"></v:imagedata></v:shape>
主窗口的代码<o:p></o:p>
Code: <o:p></o:p>
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
#endregion
namespace Serialexpample
{
partial class Form1 : Form
{
//create instance of property page
//property page is used to set values for stop bits and
//baud rate
PropertyPage pp = new PropertyPage();
//create an Serial Port object
SerialPort sp = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void propertyButton_Click(object sender, EventArgs e)
{
//show property dialog
pp.ShowDialog();
propertyButton.Hide();
}
private void sendButton_Click(object sender, EventArgs e)
{
try
{
//write line to serial port
sp.WriteLine(textBox.Text);
//clear the text box
textBox.Text = "";
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
private void ReadButton_Click(object sender, EventArgs e)
{
try
{
//clear the text box
textBox.Text = "";
//read serial port and displayed the data in text box
textBox.Text = sp.ReadLine();
}
catch(System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}
private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
readButton.Show();
textBox.Show();
}
//when we want to save the status(value)
private void saveStatusButton_Click_1(object sender, EventArgs e)
{
//display values
//if no property is set the default values
if (pp.bRate == "" && pp.sBits == "")
{
dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();
readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();
}
else
{
dataBitLabel.Text = "BaudRate = " + pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
}
parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString();
if (propertyButton.Visible == true)
propertyButton.Hide();
saveStatusButton.Hide();
startCommButton.Show();
try
{
//open serial port
sp.Open();
//set read time out to 500 ms
sp.ReadTimeout = 500;
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
}
}<o:p></o:p>
属性设置对话框代码:<o:p></o:p>
Code: <o:p></o:p>
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
namespace Serialexpample
{
partial class PropertyPage : Form
{
//variables for storing values of baud rate and stop bits
private string baudR="";
private string stopB="";
//property for setting and getting baud rate and stop bits
public string bRate
{
get
{
return baudR;
}
set
{
baudR = value;
}
}
public string sBits
{
get
{
return stopB;
}
set
{
stopB = value;
}
}
public PropertyPage()
{
InitializeComponent();
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.bRate = "";
this.sBits = "";
//close form
this.Close();
}
private void okButton_Click_1(object sender, EventArgs e)
{
//here we set the value for stop bits and baud rate.
this.bRate = BaudRateComboBox.Text;
this.sBits = stopBitComboBox.Text;
//
this.Close();
}
}
}<o:p></o:p>
发表评论
-
实现GridView控件的删除多条记录功能系列(1)
2005-08-10 14:15 1380在Asp.Net 2.0中新增的控件GridView可 ... -
实现GridView控件的删除多条记录功能系列(2)
2005-08-10 14:55 1193在上一篇中,我们已经开发了需要的CheckBox控件, ... -
实现GridView控件的删除多条记录功能系列(3)
2005-08-11 12:18 1251本篇将讲述如何解决GridView控件中使用Check ... -
[转]C#正则表达式小结
2007-09-24 11:38 1138地址: http://www.cnblogs.co ... -
[转]全面剖析C#正则表达式
2007-09-24 11:41 1317地址:http://fineboy.cnblogs.com/ ... -
(转贴)VS.NET下水晶报表分发时的问题及解决
2007-10-18 17:27 1115一、载入报表时报错 ... -
(转贴)图解使用VS.NET部署含水晶报表的网站
2007-10-18 17:56 2017Crystal Report ,中文名称“水晶报表”,因为做报 ... -
[转]VS2005 web程序自定义安装包的制作
2007-10-23 10:28 2062利用VS2005的“Web安装项目”建立安装包很难对安装过程进 ... -
[转]使用C#进行点对点通讯和文件传输(通讯基类部分)
2007-10-24 10:59 857最近一个项目要用到点对点文件传输,俺就到处找资料写程序,最后终 ... -
[转]使用C#进行点对点通讯和文件传输(发送接收部分)
2007-10-24 11:00 1549上面介绍了通讯的基类,下面就是使用那个类进行发送和接收的部分: ... -
[转]C#多线程编程实例实战
2007-10-24 11:02 1594单个写入程序/多个阅读程序在.Net类库中其实已经提供了实现, ... -
[转]实现同时只允许运行一个程序实例
2007-10-24 11:06 1065方法一: /// <summary> /// 从这 ... -
[转]IIS虚拟目录控制类
2007-10-24 11:08 422using System;using System.Data; ... -
[转]C# 调用API,实现注销远程登录本机的用户
2007-10-24 11:08 1739using System;using System.Manag ... -
[转]C#的usb通讯编程
2007-10-24 11:10 2712using System;using System.Colle ... -
[转]IIS站点管理类
2007-10-24 11:10 872using System;using System.Colle ... -
[转]使用.NET实现断点续传
2007-10-24 11:11 1232断点续传的原理 在了解HTTP断点续传的原理之前,先来说说HT ... -
[转]Datagridview 实现二维表头
2007-10-26 16:42 2290最近把我们的b/s系统,增加智能客户端的功能。确实智能客户端是 ... -
[转]Web项目下NHibernate的Session管理的解决方案
2007-11-07 10:47 3351NHibernate的Session的管理一直是个问题,在系统 ... -
[转] NHibernate对像版本控制使用示例
2007-11-07 10:49 1190<version name="Version& ...
相关推荐
C#中串口通信编程,本文将介绍如何在.NET平台下使用C#创建串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports。这个新的框架不但可以访问计算机上的串口,还可以和串口设备进行通信。我们将...
C#中串口通信编程CSharp232C#中串口通信编程CSharp232C#中串口通信编程CSharp232
c#串口通信 c#串口编程连接串口代码 c#串口通信 c#串口编程连接串口代码
串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信编程大全串口通信...
C#中串口通信编程.pdf
c# 实现串口数据通信 这是本机测试版 将 串口 第一帧和第二针 用跳线连接
C#中串口通信编程,本文将介绍如何在.NET平台下使用C#创建串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports。这个新的框架不但可以访问计算机上的串口,还可以和串口设备进行通信。我们将...
C#版串口通信编程(接收和发送数据),非常实用!
本源码在VS2013平台上,使用C#编程,实现了多串口通信的数据接收处理,实用性强,代码简洁,内置使用说明
c# 串口通信 vs2005+c# 串口通信编程
C#版串口通信编程(接收和发送数据)
串口通信的实现,编程环境为C#,实现技术采用了多线程方式
C#串口通信编程实例,visual studio 2005下编译。 打开后先开启DropDownList中的端口,然后在下面输入数据后点Write,最后再Read。可以下载串口模拟器测试。
书名:《Visual C#.NET串口通信及测控应用典型实例》(电子工业出版社.李江全.邓红涛.刘巧.李伟) PDF格式扫描版,全书分为8章,共369页。2012年5月出版。 全书压缩打包成3部分,这是第3部分 内容简介 本书从工程...
003.C#串口通信:MSComm控件使用详解.doc 004.C#_.net常用控件(09软件).pdf 005.C#_.NET常用控件(3).doc 006.C#_窗体及常用控件、组件.ppt 007.C#_第3章_Windows窗体与控件.ppt 008.王牌2_C__控件查询手册.pdf 009....
C# 串口和USB通信编程 (串口和USB通信,U盘) 包括转串口、并口、usb打印口
001.C#串口通信编程类(修改版).doc 002.C#结合串口通信类实现串口通信源代码.doc 003.C_并口及串口通信.docx 004.C#_SerialPort通信详细介绍.pdf 005.SerialPort控件的使用.doc 006.C#与51单片机串口通信.doc 007.C_...
串口通信编程.rar串口通信编程.rar串口通信编程.rar串口通信编程.rar
C#串口通信程序.pdf 很详细的C#写的具体事例啊!