C#和232串口通信方法(SerialPort控件)(2)

2019-01-26 14:30

_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate) _serialPort.Parity = SetPortParity(_serialPort.Parity)

_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits) _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits) _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

' Set the read/write timeouts _serialPort.ReadTimeout = 500 _serialPort.WriteTimeout = 500

_serialPort.Open() _continue = True readThread.Start()

Console.Write(\) name = Console.ReadLine()

Console.WriteLine(\)

While (_continue)

message = Console.ReadLine()

If sComparer.Equals(\, message) Then

_continue = False Else

_serialPort.WriteLine( _ String.Format(\{1}\, name, message))

End If end while

readThread.Join() _serialPort.Close() End Sub

Public Shared Sub Read() While (_continue) Try

Dim message As String = _serialPort.ReadLine()

Console.WriteLine(message) Catch ex As TimeoutException ' Do nothing End Try End While End Sub

Public Shared Function SetPortName(ByVal defaultPortName As String) As String

Dim newPortName As String

Console.WriteLine(\)

Dim s As String

For Each s In SerialPort.GetPortNames() Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortName)

newPortName = Console.ReadLine()

If newPortName = \ Then

newPortName = defaultPortName End If

Return newPortName End Function

Public Shared Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer Dim newBaudRate As String

Console.Write(\, defaultPortBaudRate)

newBaudRate = Console.ReadLine()

If newBaudRate = \ Then newBaudRate = defaultPortBaudRate.ToString() End If

Return Integer.Parse(newBaudRate)

End Function

Public Shared Function SetPortParity(ByVal defaultPortParity As Parity) As Parity Dim newParity As String

Console.WriteLine(\options:\)

Dim s As String

For Each s In [Enum].GetNames(GetType(Parity)) Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortParity.ToString())

newparity = Console.ReadLine()

If newparity = \ Then newparity = defaultPortParity.ToString() End If

Return CType([Enum].Parse(GetType(Parity), newParity), Parity) End Function

Public Shared Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer Dim newDataBits As String

Console.Write(\, defaultPortDataBits)

newDataBits = Console.ReadLine()

If newDataBits = \ Then newDataBits = defaultPortDataBits.ToString() End If

Return Integer.Parse(newDataBits) End Function

Public Shared Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits Dim newStopBits As String

Console.WriteLine(\options:\)

Dim s As String For Each s In [Enum].GetNames(GetType(StopBits))

Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortStopBits.ToString())

newStopBits = Console.ReadLine()


C#和232串口通信方法(SerialPort控件)(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2011届高考生物果蝇实验专题专项训练3

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: