意思
当前位置:首页 > 其他范文 > 意思 > 列表页

serialport中的readline读到newline什么意思

小草范文网  发布于:2017-04-28  分类: 意思 手机版

篇一:在C#中使用SerialPort类实现串口通信

在C#中使用SerialPort类实现串口通信

在.NET Framework 2.0中提供了SerialPort类,该类主要实现串口数据通信等。本文章将本人在学习过程中从网络上搜集到的相关信息写出来供大家参考。

下面主要介绍该类的主要属性(表1)和方法(表.2)。

如果需要了解更多的信息请登http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport(VS.80).aspx查看。

相关文章

《使用System.IO.Ports读取COM口数据》

.DataReceived +=

System.IO.Ports.SerialDataReceivedEventHandler(this.OnDataReceived);

private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)

4.2使用线程接收

接收数据启动一个线程,使其接收。

在类的内部定义 new

Thread _readThread;

bool _keepReading;

打开串口后启动线程

_keepReading = true;

_readThread = new Thread(ReadPort);

_readThread.Start();

线程函数

view plaincopy to clipboardprint?

private void ReadPort()

{

while (_keepReading)

{

if (com.IsOpen)

{

byte[] readBuffer = new byte[com.ReadBufferSize + 1];

try

{

// If there are bytes available on the serial port,

// Read returns up to "count" bytes, but will not block (wait)

// for the remaining bytes. If there are no bytes available

// on the serial port, Read will block until at least one by(原文来自:wWw.xiaOcAofANweN.coM 小 草 范 文 网:serialport中的readline读到newline什么意思)te

// is available on the port, up until the ReadTimeout milliseconds // have elapsed, at which time a TimeoutException will be thrown. int count = com.Read(readBuffer, 0, com.ReadBufferSize);

String SerialIn = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count); if (count != 0)

//byteToHexStr(readBuffer);

ThreadFunction(byteToHexStr(readBuffer,count));

}

catch (TimeoutException) { }

}

else

{

TimeSpan waitTime = new TimeSpan(0, 0, 0, 0, 50);

Thread.Sleep(waitTime);

}

}

}

private void ReadPort()

篇二:SerialPort类的常用属性

SerialPort类的常用属性

续表

表13.2 SerialPort类的常用方法

篇三:SerialPort控件

SerialPort常用属性、方法和事件名称

Container

说明 获取 对象的基础 对象。 获取或设置串行波特率。 获取或设置中断信号状态。 获取接收缓冲区中数据的字节数。 获取发送缓冲区中数据的字节数。 获取端口的载波检测行的状态。 获取 IContainer,它包含 Component。(从 Component继承。) 获取“可以发送”行的状态。 获取或设置每个字节的标准数据位长度。 获取或设置一个值,该值指示 Null 字节在端口和接收缓冲区之间传输时是否被忽略。 获取数据设置就绪 (DSR) 信号的状态。 获取或设置一个值,该值在串行通信过程中启用数据终端就绪 (DTR) 信号。 获取或设置传输前后文本转换的字节编码。 获取或设置串行端口数据传输的握手协议。 获取一个值,该值指示 SerialPort 对象的打开或关闭状态。 获取或设置用于解释 和 方法调用结束的值。 获取或设置奇偶校验检查协议。 获取或设置一个字节,该字节在发生奇偶校验错误时替换

数据流中的无效字节。

获取或设置通信端口,包括但不限于所有可用的 COM 端

口。

获取或设置 SerialPort 输入缓冲区的大小。 获取或设置读取操作未完成时发生超时之前的毫秒数。获取或设置 事件发生前内部输入缓冲区

中的字节数。

获取或设置一个值,该值指示在串行通信中是否启用请求

发送 (RTS) 信号。

Site 获取或设置 Component 的 ISite。

(从 Component 继承。)

获取或设置每个字节的标准停止位数。

获取或设置串行端口输出缓冲区的大小。 获取或设置写入操作未完成时发生超时之前的毫秒数

代码例程

1、串口配置代码

Sub PortStart()

'SerialPort1.PortName = COMX'计算机串口设置 X,是串口号。可以使用下列列表框选择。

SerialPort1.BaudRate = 9600 ?波特率设置

SerialPort1.DataBits = 8'数据位设置

SerialPort1.StopBits = StopBits.One '停止位设置

SerialPort1.Encoding = Encoding.UTF8

SerialPort1.DtrEnable = True

SerialPort1.ReadTimeout = 500'超时时间

SerialPort1.NewLine = vbCrLf '行结束符合

End Sub

2、计算机串口读取

Sub GetSerialPortNames()

'计算机串口读取

For Each sp As String In My.Computer.Ports.SerialPortNames

CompList.Items.Add(sp) ?CompList是一个下列框控件,这里修改为你的下列框名称

Next

CompList.Text = CompList.Items(0)

End Sub

3、串口打开

Sub PortOpen()

Try

SerialPort1.Open()

Call PortStart()

Catch ex As UnauthorizedAccessException

MsgBox("串口被占用或串口错误!", MsgBoxStyle.Information, "提示!")

End Try

End Sub

4、串口关闭

Sub PortOpen()

Try

SerialPort1.Close()

Catch ex As Exception

MsgBox("串口未打开或串口异常!", MsgBoxStyle.Information, "提示!") End Try

End Sub

5、串口读取数据

Sub ComRec()

Dim Rxstr As String

Try

Rxstr = SerialPort1.ReadLine ?读取一个新行

Application.DoEvents()

ComTxT.AppendText(Rxstr) ?读取到的数据添加到文本框中显示Catch e As TimeoutException?当超时以后,读取串口所有的数据 Rxstr = SerialPort1.ReadExisting

ComTxT.AppendText(Rxstr)

Application.DoEvents()

End Try

End Sub

6、发送数据

SerialPort.Write,将数据写入串行端口输出缓冲区。

名称

说明 将参数字符串写入输出。

由 .NET Compact Framework 支持。

将指定数量的字节写入输出缓冲区中的指定偏移量处。

由 .NET Compact Framework 支持。

将指定数量的字符写入输出缓冲区中的指定偏移量处。

由 .NET Compact Framework 支持。

本文已影响