首页 Soft PlugIn RAN乱 Dev开发 Info资料 English WAP 留言 登陆 注册
-
Posted by Yippee | 评论(2) | 引用(0) | 阅读8528次
VS.NET 2005串口System.IO.Ports编程体验

开发环境:Visual Web Developer 2005 速成版的测试版 2 Microsoft.NET\Framework\v2.0.50215 写了一段简单的代码:using System.IO.Ports;

Label1.Text = "OK!";
SerialPort sp = new SerialPort("COM2");
sp.Open();
sp.Write("ATI4r");
char    []read=new char[255];
//sp.Read(read, 0, 255);
//string str=new String(read);
string str = sp.ReadLine();     
string str2=sp.ReadLine();
Label1.Text = str;
Label2.Text = str2;
this.Response.Write(str);
sp.Close();       

感觉有点类似以前VB的MSCOMM控件。The System.IO.Ports namespace contains classes for controlling serial ports. The most important class, System.IO.Ports.SerialPort, provides a framework for synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties. It can be used to wrap a System.IO.Stream objects, allowing the serial port to be accessed by classes that use streams.

构造函数可以指定波特率之类的各种参数;Open Opens a new serial port connection. 打开串口Read Reads from the T:System.IO.Ports.SerialPort input buffer. 读取串口数据,可以CHAR数组或者BYTE数组
ReadByte Synchronously reads one byte from the T:System.IO.Ports.SerialPort input buffer.
ReadChar Synchronously reads one character from the T:System.IO.Ports.SerialPort input buffer. 读取一个charset或者BYTE
ReadExisting Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the T:System.IO.Ports.SerialPort object.
ReadLine Reads up to the P:System.IO.Ports.SerialPort.NewLine value in the input buffer.
ReadTo Reads a string up to the specified value in the input buffer.
ToStringComponent
Returns a System.String containing the name of the System.ComponentModel.Component, if any. This method should not be overridden.
 Write Writes data to the serial port output buffer.
WriteLine Writes the specified string and the P:System.IO.Ports.SerialPort.NewLine value to the output buffer.  写函数和写一行函数Close Closes the port connection, sets P:System.IO.Ports.SerialPort.IsOpen to false and disposes of the internal T:System.IO.Stream object. 关闭串口
 DiscardInBuffer Discards data from the serial driver's receive buffer. 清空接收/发送缓存区
DiscardOutBuffer Discards data from the serial driver's transmit buffer.

提供了三种事件:DataReceived Represents the method that will handle the serial received event of a T:System.IO.Ports.SerialPort object. 当有数据来到时参数事件DisposedComponent
Adds an event handler to listen to the System.ComponentModel.Component.Disposed event on the component. SerialData Enumeration Chars A character was received and placed in the input buffer. Eof The event character was received and placed in the input buffer.


 ErrorReceived Represents the method that handles the error event of a T:System.IO.Ports.SerialPort object. 当产生一个错误时 Frame The hardware detected a framing error. Overrun A character-buffer overrun has occurred. The next character is lost.
RXOver An input buffer overflow has occurred. There is either no room in the input buffer, or a character was received after the end-of-file (EOF) character. RXParity The hardware detected a parity error. TXFull The application tried to transmit a character, but the output buffer was full.  SerialError Enumeration

PinChanged Represents the method that will handle the serial pin changed event of a T:System.IO.Ports.SerialPort object. 当串口针脚电平变化 SerialPinChange Enumeration (System.IO.Ports) Break A break was detected on input.
CDChanged The Receive Line Signal Detect (RLSD) signal changed state.
CtsChanged The Clear to Send (CTS) signal changed state.
DsrChanged The Data Set Ready (DSR) signal changed state.
Ring A ring indicator was detected.

sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    //throw new Exception("The method or operation is not implemented.");
    MessageBox.Show("1"+sp.ReadLine()+"2");
}http://msdn2.microsoft.com/library/ms143556(en-us,vs.80).aspx


字体:

Permanant URI永久地址 http://www.shengfang.org/blog/p/vsnet2005SerialPort.php
Trackback URI引用地址 http://www.shengfang.org/blog/tb.php?tb_id=1118726006

2005年6月14日13:13星期二  [Info资料] 追踪此文的RSS
随风而逝在 2007年4月19日00:16星期四 评论:
我初学编程,最近想尽快编一个串口收发程序
你能否给我个更详细的例子?
非常感谢!
Yippee 回复于 2007年4月19日08:33
网络搜索吧,应该很多的。

qruntime在 2006年4月3日11:18星期一 评论:
看到你这篇文章, 有点不明白, 能否与你详细交流一下关于SerialPort 的编程呢?

我的QQ是:2166286 MSN:qruntime at 163 dot com
Yippee 回复于 2006年4月3日12:22
你好,我很少用IM软件,有什么问题请留言或者EMAIL,谢谢。

称呼:    登陆   注册
   不注册,但记住我的信息
邮件:
(非必须)
评论: [UBB代码帮助]
粗体 斜体 下划线 链接 水平线 引用



验证码: 请输入你看见的数字
关闭UBB      提交时自动将内容复制到剪贴板

公告
Fire and Motion!

统计信息
[Yippee]||[统计]||日志:1790
在线: 13||用户: 2562 [列表]
今日:1561||到访:2671060
Rss:838269||评论:1584

最新日志

最新评论

友情链接

日历

最新引用

搜索

归档

杂项
Get RSS Feed (Version 2.0)
Get Atom Feed (Version 0.3)
编码:  UTF-8