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

2019-01-26 14:30

If newStopBits = \ Then newStopBits = defaultPortStopBits.ToString() End If

Return

CType([Enum].Parse(GetType(StopBits), newStopBits), StopBits) End Function

Public Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake Dim newHandshake As String

Console.WriteLine(\options:\)

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

Console.WriteLine(\, s) Next s

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

newHandshake = Console.ReadLine()

If newHandshake = \ Then newHandshake = defaultPortHandshake.ToString() End If

Return CType([Enum].Parse(GetType(Handshake), newHandshake), Handshake) End Function End Class using System; using System.IO.Ports; using System.Threading; public class PortChat { static bool _continue; static SerialPort _serialPort; public static void Main() { string name; string message; StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; Thread readThread = new Thread(Read); // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties.

_serialPort.PortName = SetPortName(_serialPort.PortName); _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 (stringComparer.Equals(\, message))

{

_continue = false; } else {

_serialPort.WriteLine(

String.Format(\{1}\, name, message) ); } }

readThread.Join(); _serialPort.Close(); }

public static void Read() {

while (_continue) {

try {

string message = _serialPort.ReadLine();

Console.WriteLine(message);

}

catch (TimeoutException) { } } }

public static string SetPortName(string defaultPortName) {

string portName;

Console.WriteLine(\);

foreach (string s in SerialPort.GetPortNames()) {

Console.WriteLine(\, s); }

Console.Write(\, defaultPortName);

portName = Console.ReadLine();

if (portName == \) {

portName = defaultPortName; }

return portName; }

public static int SetPortBaudRate(int defaultPortBaudRate)


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

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

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

马上注册会员

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