| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5333 人关注过本帖, 2 人收藏
标题:[源代码]C#中串口通信编程 [zt]
只看楼主 加入收藏
立志成佛
Rank: 1
等 级:新手上路
威 望:2
帖 子:314
专家分:0
注 册:2006-11-1
收藏(2)
 问题点数:0 回复次数:10 
[源代码]C#中串口通信编程 [zt]
最近在论坛看到很多人对这个由兴趣,遇到一篇好文章,转给大家。

[简介]

本文将介绍如何在.NET平台下使用C#创建串口通信程序,.NET 2.0提供了串口通信的功能,其命名

空间是System.IO.Ports。这个新的框架不但可以访问计算机上的串口,还可以和串口设备进行通信。

我们将使用标准的RS 232 C 在PC间通信。它工作在全双工模式下,而且我们不打算使用任何的握手

或流控制器,而是使用无modem连接。

命名空间

System.IO.Ports命名空间中最重用的是SerialPort 类。

创建SerialPort 对象

通过创建SerialPort 对象,我们可以在程序中控制串口通信的全过程。
我们将要用到的SerialPort 类的方法:
ReadLine():从输入缓冲区读一新行的值,如果没有,会返回NULL
WriteLine(string):写入输出缓冲
Open():打开一个新的串口连接
Close():关闭

Code:
//create a Serial Port object
SerialPort sp = new SerialPort ();


默认情况下,DataBits 值是8,StopBits 是1,通信端口是COM1。这些都可以在下面的属性中重新设置



BaudRate:串口的波特率
StopBits:每个字节的停止位数量
ReadTimeout:当读操作没有完成时的停止时间。单位,毫秒

还有不少其它公共属性,自己查阅MSDN。

串口的硬件知识

在数据传输的时候,每个字节的数据通过单个的电缆线传输。包包括开始位,数据,结束为。一旦

开始位传出,后面就会传数据,可能是5,6,7或8位,就看你的设定了。发送和接收必须设定同样

的波特率和数据位数。

无猫模式

没有Modem模式的电缆只是简单地交叉传送和接收线。同样DTR & DSR, 和 RTS & CTS也需要交叉。

RS232针图




这里,我们三条线。互连2和3(一段的2pin连接3pin),连接两端的5pin。

[示例程序]

主程序



如果想使用默认属性,按“Save Status”按钮,如果想改变属性按“Property”。它会弹出下图:



设定好之后,可以通信了。





主窗口的代码

Code:
#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;
}
}
}
}


属性设置对话框代码:

Code:
#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();

}
}
}

搜索更多相关主题的帖子: 串口通信 源代码 控制器 计算机 Ports 
2007-08-22 15:37
随风云
Rank: 1
等 级:新手上路
威 望:1
帖 子:263
专家分:0
注 册:2007-6-28
收藏
得分:0 
谢谢了!收藏了,好东西耶!

真的想象风一样去流浪!
2007-08-22 17:39
一氧化碳
Rank: 5Rank: 5
等 级:职业侠客
威 望:1
帖 子:132
专家分:335
注 册:2007-8-13
收藏
得分:0 

不错..谢了!


2007-09-02 18:52
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
收藏
得分:0 
收藏了!

2007-09-03 08:17
wzg0319
Rank: 5Rank: 5
等 级:职业侠客
帖 子:68
专家分:305
注 册:2007-9-12
收藏
得分:0 

不错,不错。。支持一下。
虽然简单了点!呵呵

2007-10-30 11:25
wangchen223
Rank: 1
等 级:新手上路
帖 子:149
专家分:0
注 册:2006-7-1
收藏
得分:0 
哈哈,好东西.正需要

2007-11-10 10:36
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
呵,支持一下

飘过~~
2007-11-10 13:07
swc
Rank: 3Rank: 3
等 级:论坛游民
威 望:6
帖 子:394
专家分:83
注 册:2006-4-7
收藏
得分:0 

实践、学习、再实践、再学习......
2007-11-10 20:02
硕a英
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-4-26
收藏
得分:0 
为什么serialPort1.PortName = "COM1";中的 serialPort1不存在啊??
2010-05-11 19:19
cww4167
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2008-11-10
收藏
得分:0 
回复 9楼 硕a英
可定是你添加的serialport控件的名字改过了,不是叫serialport1
2010-08-07 14:26
快速回复:[源代码]C#中串口通信编程 [zt]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017192 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved