VS2012 串口编程
2014-04-16 00:12
211 查看
见:http://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.90).aspx
Namespace: System.IO.Ports Assembly: System (in System.dll)
#using <System.dll>using namespace System;using namespace System::IO::Ports;using namespace System::Threading;public ref class PortChat{private: static bool _continue; static SerialPort^ _serialPort;public: static void Main() { String^ name; String^ message; StringComparer^ stringComparer = StringComparer::OrdinalIgnoreCase; Thread^ readThread = gcnew Thread(gcnew ThreadStart(PortChat::Read)); // Create a new SerialPort object with default settings. _serialPort = gcnew 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(); } static void Read() { while (_continue) { try { String^ message = _serialPort->ReadLine(); Console::WriteLine(message); } catch (TimeoutException ^) { } } } static String^ SetPortName(String^ defaultPortName) { String^ portName; Console::WriteLine("Available Ports:"); for each (String^ s in SerialPort::GetPortNames()) { Console::WriteLine(" {0}", s); } Console::Write("COM port({0}): ", defaultPortName); portName = Console::ReadLine(); if (portName == "") { portName = defaultPortName; } return portName; } static Int32 SetPortBaudRate(Int32 defaultPortBaudRate) { String^ baudRate; Console::Write("Baud Rate({0}): ", defaultPortBaudRate); baudRate = Console::ReadLine(); if (baudRate == "") { baudRate = defaultPortBaudRate.ToString(); } return Int32::Parse(baudRate); } static Parity SetPortParity(Parity defaultPortParity) { String^ parity; Console::WriteLine("Available Parity options:"); for each (String^ s in Enum::GetNames(Parity::typeid)) { Console::WriteLine(" {0}", s); } Console::Write("Parity({0}):", defaultPortParity.ToString()); parity = Console::ReadLine(); if (parity == "") { parity = defaultPortParity.ToString(); } return (Parity)Enum::Parse(Parity::typeid, parity); } static Int32 SetPortDataBits(Int32 defaultPortDataBits) { String^ dataBits; Console::Write("Data Bits({0}): ", defaultPortDataBits); dataBits = Console::ReadLine(); if (dataBits == "") { dataBits = defaultPortDataBits.ToString(); } return Int32::Parse(dataBits); } static StopBits SetPortStopBits(StopBits defaultPortStopBits) { String^ stopBits; Console::WriteLine("Available Stop Bits options:"); for each (String^ s in Enum::GetNames(StopBits::typeid)) { Console::WriteLine(" {0}", s); } Console::Write("Stop Bits({0}):", defaultPortStopBits.ToString()); stopBits = Console::ReadLine(); if (stopBits == "") { stopBits = defaultPortStopBits.ToString(); } return (StopBits)Enum::Parse(StopBits::typeid, stopBits); } static Handshake SetPortHandshake(Handshake defaultPortHandshake) { String^ handshake; Console::WriteLine("Available Handshake options:"); for each (String^ s in Enum::GetNames(Handshake::typeid)) { Console::WriteLine(" {0}", s); } Console::Write("Handshake({0}):", defaultPortHandshake.ToString()); handshake = Console::ReadLine(); if (handshake == "") { handshake = defaultPortHandshake.ToString(); } return (Handshake)Enum::Parse(Handshake::typeid, handshake); }};int main(){ PortChat::Main();}
相关文章推荐
- VS2010下MFC的串口编程
- Vs2012界面编程
- 20160426 VC++(VS2013)串口编程图文教程(MSComm的使用)
- Vs2015 com串口编程Mscomm控件导入(基于win7/win10 64位)
- VS2013 Windows API 串口通信编程--多线程异步方式
- windows phone 和 VS2012 的混合编程
- VS 2012开始第一个编程项目
- Windows下的Win32串口编程
- VS2012 + IIS Web 发布(二)——开启IIS
- 串口编程 tcflush()函数
- 串口编程(1)--MFC中的异步串口实现(包含实现源代码)
- vs2012自带打包工具进行部署安装
- S 串口编程 详解3 串口的初始化、打开/关闭
- Win32串口编程
- VS2012打包部署Winform程序
- BZOJ_P2875&Codevs_P1281 [NOI2012]随机数生成器(数论+矩阵乘法)
- VS 2012问题集锦
- linux串口编程代码及重要注释-调通
- VS2012及VS系列怎样屏蔽CMD窗口~
- VS2012 不兼容vs2010 C++代码,提示说fopen unsafe的解决方法