VisualStudio2005中serialPort控件访问串口实例[转载]
来源:互联网 发布:淘宝上好吃的零食 编辑:程序博客网 时间:2024/05/01 22:23
点评:这个例子包括了这个控件几乎所有的操作,非常有参考价值.serialPort是在.net framework2.0中才有的东西,感觉这个东西和MSCOMM很相似.这里给出的例子是基于vb.net和CSHAPE
的,相应的可以在Cshape和c++中使用,基本上都是一样的.
的,相应的可以在Cshape和c++中使用,基本上都是一样的.
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class PortChatClass PortChat
Shared _continue As Boolean
Shared _serialPort As SerialPort
Public Shared Sub Main()Sub Main()
Dim name As String
Dim message As String
Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase
Dim readThread As Thread = New Thread(AddressOf 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: ")
name = Console.ReadLine()
Console.WriteLine("Type QUIT to exit")
While (_continue)
message = Console.ReadLine()
If sComparer.Equals("quit", message) Then
_continue = False
Else
_serialPort.WriteLine( _
String.Format("<{0}>: {1}", name, message))
End If
end while
readThread.Join()
_serialPort.Close()
End Sub
Public Shared Sub Read()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()Function SetPortName(ByVal defaultPortName As String) As String
Dim newPortName As String
Console.WriteLine("Available Ports:")
Dim s As String
For Each s In SerialPort.GetPortNames()
Console.WriteLine(" {0}", s)
Next s
Console.Write("COM port({0}): ", defaultPortName)
newPortName = Console.ReadLine()
If newPortName = "" Then
newPortName = defaultPortName
End If
Return newPortName
End Function
Public Shared Function SetPortBaudRate()Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer
Dim newBaudRate As String
Console.Write("Baud Rate({0}): ", defaultPortBaudRate)
newBaudRate = Console.ReadLine()
If newBaudRate = "" Then
newBaudRate = defaultPortBaudRate.ToString()
End If
Return Integer.Parse(newBaudRate)
End Function
Public Shared Function SetPortParity()Function SetPortParity(ByVal defaultPortParity As Parity) As Parity
Dim newParity As String
Console.WriteLine("Available Parity options:")
Dim s As String
For Each s In [Enum ]Enum].GetNames(GetType(Parity))
Console.WriteLine(" {0}", s)
Next s
Console.Write("Parity({0}):", defaultPortParity.ToString())
newparity = Console.ReadLine()
If newparity = "" Then
newparity = defaultPortParity.ToString()
End If
Return CType([Enum ]Enum].Parse(GetType(Parity), newParity), Parity)
End Function
Public Shared Function SetPortDataBits()Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer
Dim newDataBits As String
Console.Write("Data Bits({0}): ", defaultPortDataBits)
newDataBits = Console.ReadLine()
If newDataBits = "" Then
newDataBits = defaultPortDataBits.ToString()
End If
Return Integer.Parse(newDataBits)
End Function
Public Shared Function SetPortStopBits()Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits
Dim newStopBits As String
Console.WriteLine("Available Stop Bits options:")
Dim s As String
For Each s In [Enum ]Enum].GetNames(GetType(StopBits))
Console.WriteLine(" {0}", s)
Next s
Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString())
newStopBits = Console.ReadLine()
If newStopBits = "" Then
newStopBits = defaultPortStopBits.ToString()
End If
Return CType([Enum ]Enum].Parse(GetType(StopBits), newStopBits), StopBits)
End Function
Public Shared Function SetPortHandshake()Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake
Dim newHandshake As String
Console.WriteLine("Available Handshake options:")
Dim s As String
For Each s In [Enum ]Enum].GetNames(GetType(Handshake))
Console.WriteLine(" {0}", s)
Next s
Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString())
newHandshake = Console.ReadLine()
If newHandshake = "" Then
newHandshake = defaultPortHandshake.ToString()
End If
Return CType([Enum ]Enum].Parse(GetType(Handshake), newHandshake), Handshake)
End Function
End Class
Imports System.IO.Ports
Imports System.Threading
Public Class PortChatClass PortChat
Shared _continue As Boolean
Shared _serialPort As SerialPort
Public Shared Sub Main()Sub Main()
Dim name As String
Dim message As String
Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase
Dim readThread As Thread = New Thread(AddressOf 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: ")
name = Console.ReadLine()
Console.WriteLine("Type QUIT to exit")
While (_continue)
message = Console.ReadLine()
If sComparer.Equals("quit", message) Then
_continue = False
Else
_serialPort.WriteLine( _
String.Format("<{0}>: {1}", name, message))
End If
end while
readThread.Join()
_serialPort.Close()
End Sub
Public Shared Sub Read()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()Function SetPortName(ByVal defaultPortName As String) As String
Dim newPortName As String
Console.WriteLine("Available Ports:")
Dim s As String
For Each s In SerialPort.GetPortNames()
Console.WriteLine(" {0}", s)
Next s
Console.Write("COM port({0}): ", defaultPortName)
newPortName = Console.ReadLine()
If newPortName = "" Then
newPortName = defaultPortName
End If
Return newPortName
End Function
Public Shared Function SetPortBaudRate()Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer
Dim newBaudRate As String
Console.Write("Baud Rate({0}): ", defaultPortBaudRate)
newBaudRate = Console.ReadLine()
If newBaudRate = "" Then
newBaudRate = defaultPortBaudRate.ToString()
End If
Return Integer.Parse(newBaudRate)
End Function
Public Shared Function SetPortParity()Function SetPortParity(ByVal defaultPortParity As Parity) As Parity
Dim newParity As String
Console.WriteLine("Available Parity options:")
Dim s As String
For Each s In [Enum ]Enum].GetNames(GetType(Parity))
Console.WriteLine(" {0}", s)
Next s
Console.Write("Parity({0}):", defaultPortParity.ToString())
newparity = Console.ReadLine()
If newparity = "" Then
newparity = defaultPortParity.ToString()
End If
Return CType([Enum ]Enum].Parse(GetType(Parity), newParity), Parity)
End Function
Public Shared Function SetPortDataBits()Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer
Dim newDataBits As String
Console.Write("Data Bits({0}): ", defaultPortDataBits)
newDataBits = Console.ReadLine()
If newDataBits = "" Then
newDataBits = defaultPortDataBits.ToString()
End If
Return Integer.Parse(newDataBits)
End Function
Public Shared Function SetPortStopBits()Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits
Dim newStopBits As String
Console.WriteLine("Available Stop Bits options:")
Dim s As String
For Each s In [Enum ]Enum].GetNames(GetType(StopBits))
Console.WriteLine(" {0}", s)
Next s
Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString())
newStopBits = Console.ReadLine()
If newStopBits = "" Then
newStopBits = defaultPortStopBits.ToString()
End If
Return CType([Enum ]Enum].Parse(GetType(StopBits), newStopBits), StopBits)
End Function
Public Shared Function SetPortHandshake()Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake
Dim newHandshake As String
Console.WriteLine("Available Handshake options:")
Dim s As String
For Each s In [Enum ]Enum].GetNames(GetType(Handshake))
Console.WriteLine(" {0}", s)
Next s
Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString())
newHandshake = Console.ReadLine()
If newHandshake = "" Then
newHandshake = defaultPortHandshake.ToString()
End If
Return CType([Enum ]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: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
...{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
...{
_continue = false;
}
else
...{
_serialPort.WriteLine(
String.Format("<{0}>: {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("Available Ports:");
foreach (string s in SerialPort.GetPortNames())
...{
Console.WriteLine(" {0}", s);
}
Console.Write("COM port({0}): ", defaultPortName);
portName = Console.ReadLine();
if (portName == "")
...{
portName = defaultPortName;
}
return portName;
}
public static int SetPortBaudRate(int defaultPortBaudRate)
...{
string baudRate;
Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
baudRate = Console.ReadLine();
if (baudRate == "")
...{
baudRate = defaultPortBaudRate.ToString();
}
return int.Parse(baudRate);
}
public static Parity SetPortParity(Parity defaultPortParity)
...{
string parity;
Console.WriteLine("Available Parity options:");
foreach (string s in Enum.GetNames(typeof(Parity)))
...{
Console.WriteLine(" {0}", s);
}
Console.Write("Parity({0}):", defaultPortParity.ToString());
parity = Console.ReadLine();
if (parity == "")
...{
parity = defaultPortParity.ToString();
}
return (Parity)Enum.Parse(typeof(Parity), parity);
}
public static int SetPortDataBits(int defaultPortDataBits)
...{
string dataBits;
Console.Write("Data Bits({0}): ", defaultPortDataBits);
dataBits = Console.ReadLine();
if (dataBits == "")
...{
dataBits = defaultPortDataBits.ToString();
}
return int.Parse(dataBits);
}
public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
...{
string stopBits;
Console.WriteLine("Available Stop Bits options:");
foreach (string s in Enum.GetNames(typeof(StopBits)))
...{
Console.WriteLine(" {0}", s);
}
Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
stopBits = Console.ReadLine();
if (stopBits == "")
...{
stopBits = defaultPortStopBits.ToString();
}
return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
}
public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
...{
string handshake;
Console.WriteLine("Available Handshake options:");
foreach (string s in Enum.GetNames(typeof(Handshake)))
...{
Console.WriteLine(" {0}", s);
}
Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString());
handshake = Console.ReadLine();
if (handshake == "")
...{
handshake = defaultPortHandshake.ToString();
}
return (Handshake)Enum.Parse(typeof(Handshake), handshake);
}
}
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: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
...{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
...{
_continue = false;
}
else
...{
_serialPort.WriteLine(
String.Format("<{0}>: {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("Available Ports:");
foreach (string s in SerialPort.GetPortNames())
...{
Console.WriteLine(" {0}", s);
}
Console.Write("COM port({0}): ", defaultPortName);
portName = Console.ReadLine();
if (portName == "")
...{
portName = defaultPortName;
}
return portName;
}
public static int SetPortBaudRate(int defaultPortBaudRate)
...{
string baudRate;
Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
baudRate = Console.ReadLine();
if (baudRate == "")
...{
baudRate = defaultPortBaudRate.ToString();
}
return int.Parse(baudRate);
}
public static Parity SetPortParity(Parity defaultPortParity)
...{
string parity;
Console.WriteLine("Available Parity options:");
foreach (string s in Enum.GetNames(typeof(Parity)))
...{
Console.WriteLine(" {0}", s);
}
Console.Write("Parity({0}):", defaultPortParity.ToString());
parity = Console.ReadLine();
if (parity == "")
...{
parity = defaultPortParity.ToString();
}
return (Parity)Enum.Parse(typeof(Parity), parity);
}
public static int SetPortDataBits(int defaultPortDataBits)
...{
string dataBits;
Console.Write("Data Bits({0}): ", defaultPortDataBits);
dataBits = Console.ReadLine();
if (dataBits == "")
...{
dataBits = defaultPortDataBits.ToString();
}
return int.Parse(dataBits);
}
public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
...{
string stopBits;
Console.WriteLine("Available Stop Bits options:");
foreach (string s in Enum.GetNames(typeof(StopBits)))
...{
Console.WriteLine(" {0}", s);
}
Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
stopBits = Console.ReadLine();
if (stopBits == "")
...{
stopBits = defaultPortStopBits.ToString();
}
return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
}
public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
...{
string handshake;
Console.WriteLine("Available Handshake options:");
foreach (string s in Enum.GetNames(typeof(Handshake)))
...{
Console.WriteLine(" {0}", s);
}
Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString());
handshake = Console.ReadLine();
if (handshake == "")
...{
handshake = defaultPortHandshake.ToString();
}
return (Handshake)Enum.Parse(typeof(Handshake), handshake);
}
}
- VisualStudio2005中serialPort控件访问串口实例[转载]
- serialport 控件 串口通讯
- C#中SerialPort串口控件使用之字符的接收
- C#操作串口serialPort实例
- c# SerialPort控件串口通信
- C# SerialPort控件串口通信
- c# SerialPort控件串口通信
- c# SerialPort控件串口通信
- 【SerialPort】控件的使用实例
- VB.net使用SerialPort类访问串口
- VS2010 + QT5 SerialPort 串口应用实例总结
- vb.net串口通信(SerialPort控件)
- serialPort控件串口自动接收数据代码
- c# SerialPort控件串口通信 收藏
- C#自带的serialPort串口控件
- mscomm32串口控件 和 node serialport
- 使用.net的SerialPort串口监控控件接收串口数据
- C# 使用serialport 控件读写串口的大数据量解决方案 -- 以及与串口操作中常见的错误
- 深入浅出Win32多线程程序设计之线程通信
- cvsNT 2.0.58a+tortoisecvs配置点滴
- serialPort类数据采集代码。
- 一个非常好的CVS站点
- 第一周作业
- VisualStudio2005中serialPort控件访问串口实例[转载]
- ilogger的tiny wiki
- 在Vs.net中使用MSN即时通讯软件
- 使用SerialPort类设计串口通讯程序
- 改变一生的五句话
- Windows XP 快上几倍的三招
- 面向对象事件(Delegate)机制
- 我们来上海做什么?
- 创建Linked ValidationSummary, 深入理解ASP.NET的Validation