c#用什么方法操作串口比较好 本人菜鸟,想写个操作串口的小程序,网上的方法很多,下个例程运行下,发现有的很慢,望高手赐教,推荐下方法或有实例之类的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 VS05之后有SerialPort之前可以用MSComm SerialPort 现有的类 SerialPort 类:此类用于控制串行端口文件资源。此类提供同步 I/O 和事件驱动的 I/O、对管脚和中断状态的访问以及对串行驱动程序属性的访问。另外,此类的功能可以包装在内部 Stream 对象中,可通过 BaseStream 属性访问,并且可以传递给包装或使用流的类。SerialPort 类支持以下编码:ASCIIEncoding、UTF8Encoding、UnicodeEncoding、UTF32Encoding 以及 mscorlib.dll 中定义的、代码页小于 50000 或者为 54936 的所有编码。您可以使用其他编码,但必须使用 ReadByte 或 Write 方法并自己执行编码。 下面的代码示例演示如何使用 SerialPort 类以允许两位用户分别在两台通过 NULL 调制解调器电缆连接的独立计算机上聊天。本示例中,在聊天之前将提示用户输入端口设置和用户名。这两台计算机必须同时执行该程序才能实现本示例的全部功能。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); }} 如何将list〈byte〉型的变量中的数值以16进制的数据类型传给一个字符串? 参加了项目管理培训,散分(送PPT) 请问如何修改arraylist里面的对象啊,请看代码 SQL SERVER 可更新游标問題~~~` 水晶报表打包--打包一个合并模块60多M,怎么可能啊 百度地图效率问题 请教一个关于框架的问题! 获取当前的日期 请大侠们提供一种途径实现在程序运行时对控件进行编辑,谢谢 SQL中distinct的使用问题 聊天软件怎样插入表情? 急。httprequest可不可以不通过解析直接访问一个域名下多服务器的其中某个指定ip地址?
之前可以用MSComm
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);
}
}