当前位置:文档之家› C#串口通讯编程

C#串口通讯编程

}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
}
}
}
属性设置对话框代码:
Code:
#region Using directives
using System;
using System.Collections.Generic;
}
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)
{
baudRatelLabel.Text = ex.Message;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString();
if (propertyButton.Visible == true)
2.串口的通讯参数
串口通讯最常用的参数就是通讯端口号及通讯格式(波特率、数据位、停止位和校验位),在MSComm中相关的属性是CommPort和Settings。SerialPort类与MSComm有一些区别:
a.通讯端口号
[PortName]属性获取或设置通信端口,包括但不限于所有可用的COM端口,请注意该属性返回类型为String,不是mPort的short类型。通常情况下,PortName正常返回的值为COM1、COM2……,SerialPort类最大支持的端口数突破了CommPort控件中CommPort属性不能超过16的限止,大大方便了用户串口设备的配置。
{
//here we set the value for stop bits and baud rate.
this.bRate = BaudRateComboBox.Text;
this.sBits = stopBitComboBox.Text;
//
this.Close();
}
}
}
一.概述
在Visual Studio 6.0中编写串口通讯程序,一般都使用Microsoft Communication Control(简称MSComm)的通讯控件,只要通过对此控件的属性和事件进行相应编程操作,就可以轻松地实现串口通讯。但在技术广泛应用的今天,Visual 没有将此控件加入控件库,所以人们采用了许多方法在Visual 来编写串口通讯程序:第一种方法是通过采用Visual Studio 6.0中原来的MSComm控件这是最简单的,最方便的方法,但需要注册;第二种方法是采用微软在.NET推出了一个串口控件,基于.NET的P/Invoke调用方法实现;第三种方法是自己用API写串口通信,虽然难度高,但可以方便实现自己想要的各种功能。
readButton.Show();
textBox.Show();
}
//when we want to save the status(value)
private void saveStatusButton_Click_1(object sender, EventArgs e)
{
//display values
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}
private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
{
//show property dialog
pp.ShowDialog();
propertyButton.Hide();
}
private void sendButton_Click(object sender, EventArgs e)
{
try
{
//write line to serial port
二.SerialPort常用属性、方法和事件
1.命名空间
System.IO.Ports命名空间包含了控制串口重要的SerialPort类,该类提供了同步I/O和事件驱动的I/O、对管脚和中断状态的访问以及对串行驱动程序属性的访问,所以在程序代码起始位置需加入Using System.IO.Ports。
}
else
{
dataBitLabel.Text = "BaudRate = " + pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
}
parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
或流控制器,而是使用无modem连接。
命名空间
System.IO.Ports命名空间中最重用的是SerialPort类。
创建SerialPort对象
通过创建SerialPort对象,我们可以在程序中控制串口通信的全过程。
我们将要用到的SerialPort类的方法:
ReadLine():从输入缓冲区读一新行的值,如果没有,会返回NULL
//baud rate
PropertyPagepp = new PropertyPage();
//create an Serial Port object
SerialPort sp = new SerialPort();
public Form1()
{
InitializeComponent();
}
private void propertyButton_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)
public string bRate
{
get
{return bFra bibliotekudR;}
set
{
baudR = value;
}
}
public string sBits
{
get
{
return stopB;
}
set
{
stopB = value;
}
}
public PropertyPage()
{
InitializeComponent();
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
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion
namespace Serialexpample
{
partial class PropertyPage : Form

BaudRate:串口的波特率
StopBits:每个字节的停止位数量
ReadTimeout:当读操作没有完成时的停止时间。单位,毫秒
还有不少其它公共属性,自己查阅MSDN。
串口的硬件知识
在数据传输的时候,每个字节的数据通过单个的电缆线传输。包包括开始位,数据,结束为。一旦
开始位传出,后面就会传数据,可能是5,6,7或8位,就看你的设定了。发送和接收必须设定同样
{
//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
propertyButton.Hide();
相关主题