开发环境: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 字体:大 中 小 |