こださんの日記でVC NETでの232Cプログラミング
サンプルが無いと書いてあったので、C#のを翻訳しておきました。コンパイルは通るけれど、テストしていません。NETのマージドコードがまた進化して、本当にC++だかわかりません。個人的に言うと、こんな事をするぐらいならC#を覚えた方が早いと思います。
そういえば、サポートされるボーレートのリストはどうやって取るんだろう。無いのか?
// 232C.cpp : メイン プロジェクト ファイルです。 #include "stdafx.h" using namespace System; using namespace System::IO::Ports; using namespace System::Threading; public ref class PortChat { static bool volatile _continue; static SerialPort ^ volatile _serialPort; public: void Main() { String ^ name; String ^ message; StringComparer ^ StringComparer = StringComparer::OrdinalIgnoreCase; Thread^ readThread = gcnew Thread(gcnew ThreadStart(&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 (message == "QUIT") { _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: String ^ SetPortName(String ^ defaultPortName) { String ^ portName; Console::WriteLine("Available Ports:"); for each(String^ port in SerialPort::GetPortNames()) { Console::WriteLine(port); } 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:"); 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 *(dynamic_cast<Parity^> (Enum::Parse(Parity::typeid, 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:"); 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 *(dynamic_cast<StopBits ^>(Enum::Parse(StopBits::typeid, stopBits))); } public: 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("Stop Bits({0}):", defaultPortHandshake.ToString()); handshake = Console::ReadLine(); if (handshake == "") { handshake = defaultPortHandshake.ToString(); } return *(dynamic_cast<Handshake ^>(Enum::Parse(Handshake::typeid, handshake))); } }; int main(array<System::String ^> ^args) { PortChat ^ _port_chat = gcnew PortChat; _port_chat->Main(); return 0; }